数据处理合集,字符串分组、按指定大小分割、去重等···

1. 将数组拼接成带引号包裹的字符串

 	List<String> result = list.stream().map(FileItem::getFileName).collect(Collectors.toList());
  - 将result转为String字符串,并用逗号连接
	String str = result..map(String::valueOf).collect(Collectors.joining(","));

2. 将数组拼接成带引号包裹的字符串

 - 单引号
 	String str = list.stream().map(s->"'" + s +"'").collect(Collectors.joining(","));
 - 双引号
 	String str = list.stream().map(s->"\""+ s +"\"").collect(Collectors.joining(","));

3. 过滤数据并分组

Map<String, List<Student>> collect = list.stream().filter(s -> (StringUtils.isNotEmpty(s.getId())))
											.collect(Collectors.groupingBy(Student::getId));                

4. 将(3)中分组后的list数据去重

collect.forEach((k, v) ->
            collect.put(k,v.stream().collect(
                    Collectors.collectingAndThen(Collectors.toCollection(
                            () -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new)))
        );

5. 对list进行两层分组

List<Map<String,String>> data = new ArrayList<>();
        data.add(new HashMap<String,String>(){{put("id","1");put("major","通信");put("warehouse","西安");put("name","张三");}});
        data.add(new HashMap<String,String>(){{put("id","2");put("major","通信");put("warehouse","西安");put("name","李四");}});
        data.add(new HashMap<String,String>(){{put("id","3");put("major","通信");put("warehouse","北京");put("name","王五");}});
        data.add(new HashMap<String,String>(){{put("id","4");put("major","通信");put("warehouse","北京");put("name","马六");}});
        data.add(new HashMap<String,String>(){{put("id","5");put("major","软件");put("warehouse","西安");put("name","田七");}});
        data.add(new HashMap<String,String>(){{put("id","6");put("major","软件");put("warehouse","北京");put("name","老八");}});
        data.add(new HashMap<String,String>(){{put("id","7");put("major","软件");put("warehouse","北京");put("name","老九");}});
        data.add(new HashMap<String,String>(){{put("id","8");put("major","软件");put("warehouse","北京");put("name","老爹");}});

        //case1
        Map<String, Map<String, List<Map<String, String>>>> result1 = data.stream().collect(Collectors.groupingBy(m -> m.get("major"),                                                                                 Collectors.groupingBy(m -> m.get("warehouse"))));

        //case2
        Map<String, Map<String, Map<String, String>>> result2 = data.stream().collect(Collectors.groupingBy(m -> m.get("major"),
                Collectors.toMap(m -> m.get("warehouse"), Function.identity(),(k, v) -> k)));
		/*结果集*/
		{
		"软件": {
			"西安": [{
				"major": "软件",
				"name": "田七",
				"id": "5",
				"warehouse": "西安"
			}],
			"北京": [{
				"major": "软件",
				"name": "老八",
				"id": "6",
				"warehouse": "北京"
			},
			{
				"major": "软件",
				"name": "老九",
				"id": "7",
				"warehouse": "北京"
			},
			{
				"major": "软件",
				"name": "老爹",
				"id": "8",
				"warehouse": "北京"
			}]
		},
		"通信": {
			"西安": [{
				"major": "通信",
				"name": "张三",
				"id": "1",
				"warehouse": "西安"
			},
			{
				"major": "通信",
				"name": "李四",
				"id": "2",
				"warehouse": "西安"
			}],
			"北京": [{
				"major": "通信",
				"name": "王五",
				"id": "3",
				"warehouse": "北京"
			},
			{
				"major": "通信",
				"name": "马六",
				"id": "4",
				"warehouse": "北京"
			}]
		}
}

6. 按指定大小对list进行分割

	// 指定大小
	int maxNum = 999;
	// 切分次数
	int step = (lineId.size() + maxNum - 1) / maxNum;
	List<List<String>> list = Stream.iterate(0, n -> n + 1)
						            .limit(step)
						            .parallel()
						            .map(a -> lineId.stream()
						                          .skip(a * maxNum)
						                          .limit(maxNum)
						                          .parallel()
						                          .collect(Collectors.toList()))
						            .collect(Collectors.toList());

持续更新ing~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值