栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java8过滤两个list中相同属性值的数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java8过滤两个list中相同属性值的数据

从list1集合中过滤list2中age属性值相同的数据 

package com.gisquest.platform.rest.api.rolemgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class test {

	public static void main(String[] args) {
		
		List> list1 = new ArrayList>();
		Map map1 = new HashMap();
		map1.put("name", "111");
		map1.put("age", "21");
		
		Map map2 = new HashMap();
		map2.put("name", "222");
		map2.put("age", "22");
		
		Map map3 = new HashMap();
		map3.put("name", "333");
		map3.put("age", "23");
		
		
		Map map4 = new HashMap();
		map4.put("name", "444");
		map4.put("age", "24");
		
		list1.add(map1);
		list1.add(map2);
		list1.add(map3);
		list1.add(map4);
		List> list2 = new ArrayList>();
		
		Map map11 = new HashMap();
		map11.put("name", "111");
		map11.put("age", "21");
		
		Map map22 = new HashMap();
		map22.put("name", "222");
		map22.put("age", "22");

		list2.add(map11);
		list2.add(map22);

		
		//过滤两个list中相同的属性值的数据
		List> list3 = list1.stream().parallel().filter(a -> list2.stream()
	                        .noneMatch(b-> Objects.equals(a.get("age"), b.get("age")))).collect(Collectors.toList());

	   System.out.println(list3);
	}
	
	
	

}

测试结果:list1集合中和list2集合中相同年龄的数据被过滤了

[{name=333, age=23}, {name=444, age=24}]

从list1集合中过滤list2中age属性值不相同的数据 

package com.gisquest.platform.rest.api.rolemgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class test {

	public static void main(String[] args) {
		
		List> list1 = new ArrayList>();
		Map map1 = new HashMap();
		map1.put("name", "111");
		map1.put("age", "21");
		
		Map map2 = new HashMap();
		map2.put("name", "222");
		map2.put("age", "22");
		
		Map map3 = new HashMap();
		map3.put("name", "333");
		map3.put("age", "23");
		
		
		Map map4 = new HashMap();
		map4.put("name", "444");
		map4.put("age", "24");
		
		list1.add(map1);
		list1.add(map2);
		list1.add(map3);
		list1.add(map4);
		List> list2 = new ArrayList>();
		
		Map map11 = new HashMap();
		map11.put("name", "111");
		map11.put("age", "21");
		
		Map map22 = new HashMap();
		map22.put("name", "222");
		map22.put("age", "22");

		list2.add(map11);
		list2.add(map22);

		
		//过滤两个list中相同的属性值的数据
		List> list3 = list1.stream().parallel().filter(a -> list2.stream()
	                        .anyMatch(b-> Objects.equals(a.get("age"), b.get("age")))).collect(Collectors.toList());

	   System.out.println(list3);
	}
	
	
	

}

 测试结果:list1中age和list2中age不相同的数据被过滤

[{name=111, age=21}, {name=222, age=22}]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号