List<User> userList = new ArrayList<>;
for (int 1= 0;i<100;i++) {
User user = new User();
user.setUserName("xiaoming"+i);
user.setUserPassword(123456+i);
if (i < 20) {
user.setSex("man");
} else if (i < 50) {
user.setSex("woman");
}else{
user.setSex("N/A");
}
user.setDesc("this is a group test");
}
上面的list 可以自己构造或者查表获得;
根据名字分组
Map<String,List<User>> listMap = new HashMap<>;
for (User user :userList) {
String key = user.getUserName();
if (listMap.contains(key)) {
listMap.get(key).add(user);
}else {
List<User> users = new ArrayList<>;
users.add(user);
listMap.put(key,users);
}
}
另外方法
public
static
void
main(String[] args) {
/*1、准备数据**/
SkuVo sku1 = new SkuVo(1L,"p1",100L);
SkuVo sku2 = new SkuVo(2L,"p2",101L);
SkuVo sku3 = new SkuVo(3L,"p3",102L);
SkuVo sku4 = new SkuVo(3L,"p4",103L);
SkuVo sku5 = new SkuVo(2L,"p5",100L);
SkuVo sku6 = new SkuVo(5L,"p6",100L);
List<SkuVo> skuVoList = Arrays.asList(new SkuVo [] {sku1,sku2,sku3,sku4,sku5,sku6});
/*2、分组算法**/
Map<Long, List<SkuVo>> skuIdMap = new HashMap<>();
for (SkuVo skuVo : skuVoList) {
List<SkuVo> tempList = skuIdMap.get(skuVo.getSkuId());
/*如果取不到数据,那么直接new一个空的ArrayList**/
if (tempList == null) {
tempList = new ArrayList<>();
tempList.add(skuVo);
skuIdMap.put(skuVo.getSkuId(), tempList);
}
else {
/*某个sku之前已经存放过了,则直接追加数据到原来的List里**/
tempList.add(skuVo);
}
}
/*3、遍历map,验证结果**/
for
(Long skuId : skuIdMap.keySet()){
System.out.println(skuIdMap.get(skuId));
}
}
至此分组成功;
如果根据 userName 和 sex 两个字段进行分组,该如何做呢?
思路可以把 sexName 和sex 拼接成一个字符串,然后在拆分,有兴趣的可以写下自己的答案和交流
lamda表达式进行分组
package
test;
import
com.alibaba.fastjson.JSON;
import
com.alibaba.fastjson.serializer.SerializerFeature;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Map;
import
java.util.stream.Collectors;
public
class
ListGroupTest {
public
static
void
main(String[] args) {
List<Coupon> couponList =
new
ArrayList<>();
Coupon coupon1 =
new
Coupon(
1
,
100
,
"优惠券1"
);
Coupon coupon2 =
new
Coupon(
2
,
200
,
"优惠券2"
);
Coupon coupon3 =
new
Coupon(
3
,
300
,
"优惠券3"
);
Coupon coupon4 =
new
Coupon(
3
,
400
,
"优惠券4"
);
couponList.add(coupon1);
couponList.add(coupon2);
couponList.add(coupon3);
couponList.add(coupon4);
Map<Integer, List<Coupon>> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId));
System.out.println(JSON.toJSONString(resultList, SerializerFeature.PrettyFormat));
}
}
对象的属性进行分组
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListGroupTest2 {
public static void main(String[] args) {
List<Coupon> couponList = new ArrayList<>();
Coupon coupon1 = new Coupon(1,100,"优惠券1");
Coupon coupon2 = new Coupon(2,200,"优惠券2");
Coupon coupon3 = new Coupon(3,300,"优惠券3");
Coupon coupon4 = new Coupon(3,400,"优惠券4");
couponList.add(coupon1);
couponList.add(coupon2);
couponList.add(coupon3);
couponList.add(coupon4);
Map<Integer, List<String>> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId,Collectors.mapping(Coupon::getName,Collectors.toList())));
System.out.println(JSON.toJSONString(resultList, SerializerFeature.PrettyFormat));
}
}
List<Person> persons = new ArrayList<>(); persons.add(new Person("Brandon", 15, 5000));
persons.add(new Person("Braney", 15, 15000)); persons.add(new Person("Jack", 10, 5000));
persons.add(new Person("Robin", 10, 500000)); persons.add(new Person("Tony", 10, 1400000)); /
/ 只对 age 分组 Map<Integer,List<Person>> map = persons.stream().collect(Collectors.groupingBy(Person::getAge));
for (Map.Entry<Integer, List<Person>> entry : map.entrySet()) { System.out.println("分组" +entry); } System.out.println("=======================");
// 对age分组 然后取所有的聚合结果集 Map<Integer, DoubleSummaryStatistics> collect = persons.stream().collect( Collectors.groupingBy(Person::getAge, Collectors.summarizingDouble(Person::getSalary)));
for (Map.Entry<Integer, DoubleSummaryStatistics> entry : collect.entrySet()) { DoubleSummaryStatistics longSummaryStatistics = entry.getValue(); System.out.println("----------------key----------------" + entry.getKey());
System.out.println("求和:" + longSummaryStatistics.getSum()); System.out.println("求平均" + longSummaryStatistics.getAverage());
System.out.println("求最大:" + longSummaryStatistics.getMax()); System.out.println("求最小:" + longSummaryStatistics.getMin());
System.out.println("求总数:" + longSummaryStatistics.getCount()); } System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// 对age分组 然后取求和结果集
Map<Integer, Double> collect1 = persons.stream().collect( Collectors.groupingBy(Person::getAge, Collectors.summingDouble(Person::getSalary)));
System.out.println(collect1); System.out.println("1");