网站开发怎么自学,游戏网站开发什么意思,企业网站 app,汤阴县seo快速排名有哪家好Java中使用流将两个集合根据某个字段进行过滤去重?
在Java中#xff0c;您可以使用流(Stream)来过滤和去重两个集合。下面是一个示例代码#xff0c;展示如何根据对象的某个字段进行过滤和去重操作#xff1a;
import java.util.ArrayList;
import java.util.List;
impor…Java中使用流将两个集合根据某个字段进行过滤去重?
在Java中您可以使用流(Stream)来过滤和去重两个集合。下面是一个示例代码展示如何根据对象的某个字段进行过滤和去重操作
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;public class Main {public static void main(String[] args) {// 创建一个对象集合ListPerson persons new ArrayList();persons.add(new Person(Alice, 25));persons.add(new Person(Bob, 30));persons.add(new Person(Alice, 25));persons.add(new Person(Charlie, 35));persons.add(new Person(Alice, 40));persons.add(new Person(Bob, 25));// 根据 name 字段进行过滤和去重ListPerson filteredAndDistinctPersons persons.stream().filter(person - person.getName().equals(Alice)).distinct().collect(Collectors.toList());// 打印结果filteredAndDistinctPersons.forEach(System.out::println);}
}class Person {private String name;private int age;public Person(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public int getAge() {return age;}Overridepublic String toString() {returnPerson [name name , age age ];}
}在这个示例中我们创建了一个 Person 类表示人员信息包含 name 和 age 两个字段。我们使用流的 filter 方法来过滤出 name 字段为 “Alice” 的对象然后使用 distinct 方法来去除重复的对象。最后我们使用 collect 方法将结果收集到一个新的集合中。
运行示例代码会输出以下结果
Person [nameAlice, age25]
Person [nameAlice, age40]这两个结果是根据 name 字段为 “Alice” 进行过滤和去重后的结果。
现在有两个集合list。如何将一个list根据另外一个list中的某个属性字段去重产生不包含的结果?
要根据一个集合中的某个属性字段将另一个集合中不包含该属性字段的元素去重您可以使用 Java 8 中的流(Stream)和过滤操作。下面是一个示例代码展示如何实现
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;public class Main {public static void main(String[] args) {// 创建两个对象集合ListPerson list1 new ArrayList();list1.add(new Person(Alice, 25));list1.add(new Person(Bob, 30));list1.add(new Person(Charlie, 35));ListPerson list2 new ArrayList();list2.add(new Person(Alice, 25));list2.add(new Person(David, 40));list2.add(new Person(Eve, 45));// 根据 list2 中的 name 字段将 list1 中不包含该字段的元素去重ListPerson distinctPersons list1.stream().filter(person - list2.stream().noneMatch(p - p.getName().equals(person.getName()))).distinct().collect(Collectors.toList());// 打印结果distinctPersons.forEach(System.out::println);}
}class Person {private String name;private int age;public Person(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public int getAge() {return age;}Overridepublic String toString() {returnPerson [name name , age age ];}
}在这个示例中我们创建了两个集合 list1 和 list2分别表示两个对象集合。我们使用流的 filter 方法来过滤出 list1 中不包含 list2 中 name 字段的元素然后使用 distinct 方法去重。最后我们使用 collect 方法将结果收集到一个新的集合中。
运行示例代码会输出以下结果
Person [nameBob, age30]
Person [nameCharlie, age35]这两个结果是根据 list2 中的 name 字段将 list1 中不包含该字段的元素进行了去重。 文章转载自: http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.bpmtq.cn.gov.cn.bpmtq.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.pkdng.cn.gov.cn.pkdng.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.cniedu.com.gov.cn.cniedu.com http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn