package com.tydic.portal;
import com.tydic.portal.sysmgr.po.User;
import com.tydic.portal.sysmgr.po.User2;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
public class TestMap {
@Test
public void testGroup(){
ArrayList arrayList1= new ArrayList<Integer>();
arrayList1.add(20);
arrayList1.add(19);
arrayList1.add(18);
ArrayList arrayList2= new ArrayList<Integer>();
arrayList2.add(20);
ArrayList arrayList3= new ArrayList<Integer>();
arrayList3.add(19);
arrayList3.add(18);
List<User2> users2 =Arrays.asList(new User2(arrayList1,"张三"),new User2(arrayList2,"李四"),new User2(arrayList3,"王五"));
Map<Integer,List<User2>> userMap = new HashMap<>();
users2.forEach(user->{
List<Integer> ages = user.getAge();
for(Integer age:ages){
List<User2> users=userMap.get(age);
if(users==null){
users = new ArrayList<>();
users.add(user);
userMap.put(age,users);
}else{
users.add(user);
userMap.put(age,users);
}
}
});
userMap.forEach((x,y)->{
System.out.println(x+":"+y);
});
}
}
|