Java GroupBy Arraylist键作为map和marge值
我想将GroupByMap键作为arraylist,将marge值作为map的值 映射是{[L1,L10]=[t1],[L1,L7]=[t2],[L1,L9]=[t3],[L1,L8]=[t4]} 要转换的内容应为: {[L1]=[t1,t2,t3,t4],[L10]=[t1],[L7]=[t2],[L9]=[t3],[L8]=[t4]}Java GroupBy Arraylist键作为map和marge值,java,java-8,Java,Java 8,我想将GroupByMap键作为arraylist,将marge值作为map的值 映射是{[L1,L10]=[t1],[L1,L7]=[t2],[L1,L9]=[t3],[L1,L8]=[t4]} 要转换的内容应为: {[L1]=[t1,t2,t3,t4],[L10]=[t1],[L7]=[t2],[L9]=[t3],[L8]=[t4]} import java.util.ArrayList; import java.util.List; import java.util.Map; import
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import com.google.common.collect.Sets;
public class AdvisoryCache {
public static void main(String[] args) {
TestObject t1=new TestObject("test1", "c1", "1");
TestObject t2=new TestObject("test2", "c2", "2");
TestObject t3=new TestObject("test3", "c1", "3");
TestObject t4=new TestObject("test4", "c3", "4");
TestObject t5=new TestObject("test5", "c1", "5");
TestObject t6=new TestObject("test6", "c4", "6");
ArrayList<TestObject> testList=new ArrayList<>();
testList.add(t1);
testList.add(t2);
testList.add(t3);
testList.add(t4);
testList.add(t5);
testList.add(t6);
ConcurrentHashMap<Object, Object> s=testList
.parallelStream().collect(Collectors.groupingBy(TestObject::getClientId))
.entrySet().stream()
.collect(Collectors.toConcurrentMap(k->getArrayValue(k.getKey()), v->getValue(v.getValue()), (k, v)->v,ConcurrentHashMap::new));
System.out.println(s);
}
public static TestObject getValue(List<TestObject> v) {
return v.get(0);
}
public static ArrayList getArrayValue(String k) {
if(k.equalsIgnoreCase("c1")) {
ArrayList a1 = new ArrayList<>();
a1.add("L1");
a1.add("L7");
return a1;
}else if(k.equalsIgnoreCase("c2")) {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L8");
return a1;
}else if(k.equalsIgnoreCase("c3")) {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L9");
return a1;
}else {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L10");
return a1;
}
}
}
class TestObject {
String name;
String clientId;
String differentValue;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the clientId
*/
public String getClientId() {
return clientId;
}
/**
* @param clientId the clientId to set
*/
public void setClientId(String clientId) {
this.clientId = clientId;
}
/**
* @return the differentValue
*/
public String getDifferentValue() {
return differentValue;
}
/**
* @param differentValue the differentValue to set
*/
public void setDifferentValue(String differentValue) {
this.differentValue = differentValue;
}
public TestObject(String name, String clientId, String differentValue) {
super();
this.name = name;
this.clientId = clientId;
this.differentValue = differentValue;
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入java.util.concurrent.ConcurrentHashMap;
导入java.util.concurrent.ConcurrentMap;
导入java.util.stream.collector;
导入com.google.common.collect.set;
公共类AdvisoryCache{
公共静态void main(字符串[]args){
TestObject t1=新的TestObject(“test1”、“c1”、“1”);
TestObject t2=新的TestObject(“test2”、“c2”、“2”);
TestObject t3=新的TestObject(“test3”、“c1”、“3”);
TestObject t4=新的TestObject(“test4”、“c3”、“4”);
TestObject t5=新的TestObject(“test5”、“c1”、“5”);
TestObject t6=新的TestObject(“test6”、“c4”、“6”);
ArrayList testList=新的ArrayList();
testList.add(t1);
testList.add(t2);
testList.add(t3);
测试列表。添加(t4);
测试列表。添加(t5);
添加测试列表(t6);
ConcurrentHashMap s=testList
.parallelStream().collect(收集器.groupingBy(TestObject::getClientId))
.entrySet().stream()
.collect(Collectors.toConcurrentMap(k->getArrayValue(k.getKey()),v->getValue(v.getValue()),(k,v)->v,ConcurrentHashMap::new));
系统输出打印项次;
}
公共静态TestObject getValue(列表v){
返回v.get(0);
}
公共静态ArrayList getArrayValue(字符串k){
if(k.等效信号情况(“c1”)){
ArrayList a1=新的ArrayList();
a1.添加(“L1”);
a1.添加(“L7”);
返回a1;
}否则,如果(k.equalsIgnoreCase(“c2”)){
ArrayList a1=新的ArrayList();
a1.添加(“L1”);
a1.添加(“L8”);
返回a1;
}else if(k.等效信号情况(“c3”)){
ArrayList a1=新的ArrayList();
a1.添加(“L1”);
a1.添加(“L9”);
返回a1;
}否则{
ArrayList a1=新的ArrayList();
a1.添加(“L1”);
a1.添加(“L10”);
返回a1;
}
}
}
类TestObject{
字符串名;
字符串clientId;
字符串差分值;
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@returntheclientid
*/
公共字符串getClientId(){
返回客户ID;
}
/**
*@param clientId要设置的clientId
*/
public void setClientId(字符串clientId){
this.clientId=clientId;
}
/**
*@返回不同的值
*/
公共字符串getDifferentivValue(){
返回不同的值;
}
/**
*@param differentivvalue要设置的differentivvalue
*/
public void setDifferentitValue(字符串DifferentitValue){
this.differentValue=differentValue;
}
公共测试对象(字符串名称、字符串clientId、字符串差异值){
超级();
this.name=名称;
this.clientId=clientId;
this.differentValue=differentValue;
}
}
映射是{[L1,L10]=[t1],[L1,L7]=[t2],[L1,L9]=[t3],[L1,L8]=[t4]}
要转换的内容应为:
{[L1]=[t1,t2,t3,t4],[L10]=[t1],[L7]=[t2],[L9]=[t3],[L8]=[t4]}你的
s
映射实际上是一个ConcurrentHashMap
,所以不要使用ConcurrentHashMap
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import com.google.common.collect.Sets;
public class AdvisoryCache {
public static void main(String[] args) {
TestObject t1=new TestObject("test1", "c1", "1");
TestObject t2=new TestObject("test2", "c2", "2");
TestObject t3=new TestObject("test3", "c1", "3");
TestObject t4=new TestObject("test4", "c3", "4");
TestObject t5=new TestObject("test5", "c1", "5");
TestObject t6=new TestObject("test6", "c4", "6");
ArrayList<TestObject> testList=new ArrayList<>();
testList.add(t1);
testList.add(t2);
testList.add(t3);
testList.add(t4);
testList.add(t5);
testList.add(t6);
ConcurrentHashMap<Object, Object> s=testList
.parallelStream().collect(Collectors.groupingBy(TestObject::getClientId))
.entrySet().stream()
.collect(Collectors.toConcurrentMap(k->getArrayValue(k.getKey()), v->getValue(v.getValue()), (k, v)->v,ConcurrentHashMap::new));
System.out.println(s);
}
public static TestObject getValue(List<TestObject> v) {
return v.get(0);
}
public static ArrayList getArrayValue(String k) {
if(k.equalsIgnoreCase("c1")) {
ArrayList a1 = new ArrayList<>();
a1.add("L1");
a1.add("L7");
return a1;
}else if(k.equalsIgnoreCase("c2")) {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L8");
return a1;
}else if(k.equalsIgnoreCase("c3")) {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L9");
return a1;
}else {
ArrayList a1=new ArrayList<>();
a1.add("L1");
a1.add("L10");
return a1;
}
}
}
class TestObject {
String name;
String clientId;
String differentValue;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the clientId
*/
public String getClientId() {
return clientId;
}
/**
* @param clientId the clientId to set
*/
public void setClientId(String clientId) {
this.clientId = clientId;
}
/**
* @return the differentValue
*/
public String getDifferentValue() {
return differentValue;
}
/**
* @param differentValue the differentValue to set
*/
public void setDifferentValue(String differentValue) {
this.differentValue = differentValue;
}
public TestObject(String name, String clientId, String differentValue) {
super();
this.name = name;
this.clientId = clientId;
this.differentValue = differentValue;
}
}
为了获得所需的映射
,我处理了s
映射
的条目,使用平面映射
生成所有对(“L”
,测试对象
),并使用收集(Collectors.groupingBy)
按“L”
对它们进行分组:
以下是流管道:
Map<String,List<TestObject>> out =
s.entrySet ()
.stream ()
.flatMap (e -> e.getKey ()
.stream ()
.map (k -> new SimpleEntry<>(k,e.getValue())))
.collect (Collectors.groupingBy (Map.Entry::getKey,
Collectors.mapping (Map.Entry::getValue,
Collectors.toList())));
为了查看返回了哪些TestObject
实例,让我们按如下方式重写TestObject
的toString()
,以便它返回一个与相应的t
变量名相匹配的String
:
public String toString ()
{
return "t" + differentValue;
}
现在,输出Map
打印为:
{L1=[t6, t1, t4, t2], L10=[t6], L7=[t1], L8=[t2], L9=[t4]}
这不是您期望的确切输出(分组是相同的,但TestObject
实例不是),但它是与您生成的s
Map
匹配的输出。源映射和目标映射的类型是什么。我们如何知道L1、L2、t1和t2等是什么?请查看如何提供一个最小且可重复的示例:ConcurrentHashMap,其中key作为arraylist,value也作为arraylist。{[L1,L10]=[t1],[L1,L7]=[t2],[L1,L9]=[t3],[L1,L8]=[t4]}要将键转换为字符串,将值转换为带边框的ArrayList。{“L1”=[t1,t2,t3,t4],“L10”=[t1],[L7]=[t2],“L9”=[t3],[L8]=[t4]}