当前位置: 首页 > news >正文

惠东网站设计工业设计公司

惠东网站设计,工业设计公司,遂宁市住房与城乡建设厅网站,怎么开个人工作室你可以使用Java 8的流来实现这个功能。假设你有两个包含对象的List,每个对象有一个属性,你想根据一个List中的属性值来过滤掉另一个List中不包含这个属性值的对象。下面是一种使用流的方式来实现这个功能 import java.util.ArrayList; import java.util…

你可以使用Java 8的流来实现这个功能。假设你有两个包含对象的List,每个对象有一个属性,你想根据一个List中的属性值来过滤掉另一个List中不包含这个属性值的对象。下面是一种使用流的方式来实现这个功能

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;class MyClass {private String property;public MyClass(String property) {this.property = property;}public String getProperty() {return property;}
}public class Main {public static void main(String[] args) {List<MyClass> list1 = new ArrayList<>();list1.add(new MyClass("A"));list1.add(new MyClass("B"));list1.add(new MyClass("C"));List<String> list2 = new ArrayList<>();list2.add("A");list2.add("B");List<MyClass> filteredList = list1.stream().filter(obj -> list2.contains(obj.getProperty())).collect(Collectors.toList());filteredList.forEach(obj -> System.out.println(obj.getProperty()));}
}

在上面的例子中,我们有两个List:list1包含了MyClass对象,每个对象有一个属性property;list2包含了一些属性值。我们使用流来筛选list1中的对象,只保留那些property值在list2中存在的对象,最后将筛选后的结果收集到filteredList中。

输出的结果将会是:

A
B

这是根据list2中的属性值过滤掉了list1中不包含这部分属性的对象。你可以根据自己的实际需要修改代码,适应不同的数据类型和属性名称。

要是类中有很多属性那该怎么写?

如果类中有很多属性,你可以根据需要编写适合的筛选条件来过滤掉不符合要求的对象。下面是一个示例,演示了如何根据类中的多个属性进行过滤。

假设你有一个名为Person的类,该类有多个属性,包括name、age和gender。你想根据一组条件对Person对象进行过滤,这些条件包括age大于等于18,同时gender为"male"。下面是一个使用流的示例代码:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;class Person {private String name;private int age;private String gender;public Person(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}public String getName() {return name;}public int getAge() {return age;}public String getGender() {return gender;}
}public class Main {public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person("Alice", 28, "female"));personList.add(new Person("Bob", 20, "male"));personList.add(new Person("Charlie", 25, "male"));personList.add(new Person("Diana", 16, "female"));List<Person> filteredList = personList.stream().filter(person -> person.getAge() >= 18 && person.getGender().equals("male")).collect(Collectors.toList());filteredList.forEach(person -> System.out.println(person.getName()));}
}

在上面的例子中,我们定义了一个Person类,该类有三个属性:name、age和gender。我们使用流的filter操作来对personList中的对象进行筛选,只保留那些满足age大于等于18并且gender为"male"的对象。最后,我们将筛选结果收集到filteredList中,并打印每个对象的name属性。

输出的结果将会是:

Bob
Charlie

这是根据定义的条件过滤掉了personList中不符合要求的Person对象。你可以根据自己的实际需求修改代码,添加或修改筛选条件,以适应不同的属性和值。

要是两个list中的元素对象存在多个属性。如何根据一个list中的对象的某个属性去过滤另一个集合元素?

如果两个集合中的元素对象有多个属性,并且你想要根据一个集合中的对象的某个属性值,来过滤另外一个集合中的元素,你可以使用流和条件谓词来实现。下面是一个示例代码,演示了如何根据一个集合中的对象的某个属性值,过滤另一个集合中的元素:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;class Element {private int id;private String name;public Element(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public String getName() {return name;}
}public class Main {public static void main(String[] args) {List<Element> list1 = new ArrayList<>();list1.add(new Element(1, "A"));list1.add(new Element(2, "B"));list1.add(new Element(3, "C"));List<Element> list2 = new ArrayList<>();list2.add(new Element(1, "X"));list2.add(new Element(2, "Y"));list2.add(new Element(3, "Z"));List<Element> filteredList = list2.stream().filter(obj -> list1.stream().map(Element::getId).collect(Collectors.toList()).contains(obj.getId())).collect(Collectors.toList());filteredList.forEach(obj -> System.out.println(obj.getName()));}
}

在上面的例子中,我们有两个集合list1和list2,每个集合包含了Element对象,每个对象有两个属性:id和name。我们使用流来筛选list2中的元素,只保留那些id值在list1中存在的元素,最后将筛选后的结果收集到filteredList中。

输出的结果将会是:

X
Y
Z

这是根据list1中元素的id属性值过滤掉了list2中不包含这部分属性的元素。你可以根据自己的实际需要修改代码,适应不同的数据类型和属性名称。

要是过滤后想保留集合中的整个对象如何书写?

如果你希望在过滤后保留集合中的整个对象,而不仅仅是属性值,你可以在过滤时使用 anyMatch() 方法来判断某个属性值是否存在于另一个集合中,然后将通过判断的对象保留下来。下面是一个示例代码:

import java.util.ArrayList;
import java.util.List;class Element {private int id;private String name;public Element(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public String getName() {return name;}
}public class Main {public static void main(String[] args) {List<Element> list1 = new ArrayList<>();list1.add(new Element(1, "A"));list1.add(new Element(2, "B"));list1.add(new Element(3, "C"));List<Element> list2 = new ArrayList<>();list2.add(new Element(1, "X"));list2.add(new Element(2, "Y"));list2.add(new Element(3, "Z"));List<Element> filteredList = new ArrayList<>();for (Element obj2 : list2) {if (list1.stream().anyMatch(obj1 -> obj1.getId() == obj2.getId())) {filteredList.add(obj2);}}filteredList.forEach(obj -> System.out.println(obj.getName()));}
}

在上面的例子中,我们有两个集合 list1 和 list2,它们包含了 Element 对象并具有相应的属性。我们通过遍历 list2 中的元素,使用 anyMatch() 方法检查该元素的 id 属性是否存在于 list1 中的任意一个元素中。如果存在,则将该对象添加到 filteredList 集合中。最后,我们打印输出 filteredList 中的元素的 name 属性。

输出的结果将会是:

X
Y
Z

这样就根据 list1 中的对象的某个属性值过滤掉了 list2 中不包含这部分属性的元素,并保留了整个对象。你可以根据具体的需求修改代码以适应不同的数据类型和属性名称。


文章转载自:
http://dinncopending.bkqw.cn
http://dinncoeurope.bkqw.cn
http://dinncofoulness.bkqw.cn
http://dinncoenhance.bkqw.cn
http://dinncomediacy.bkqw.cn
http://dinncopharyngoscopy.bkqw.cn
http://dinncoosmolar.bkqw.cn
http://dinncomesserschmitt.bkqw.cn
http://dinncoimpartation.bkqw.cn
http://dinncocrowdie.bkqw.cn
http://dinncounsuspectingly.bkqw.cn
http://dinncocoquille.bkqw.cn
http://dinncoflooring.bkqw.cn
http://dinncobox.bkqw.cn
http://dinncolithoprint.bkqw.cn
http://dinncoelectromotor.bkqw.cn
http://dinncosemicontinuum.bkqw.cn
http://dinncocalumnious.bkqw.cn
http://dinncomesoamerica.bkqw.cn
http://dinncoflavonol.bkqw.cn
http://dinncowhodunit.bkqw.cn
http://dinncominimalist.bkqw.cn
http://dinncocoplanar.bkqw.cn
http://dinncoremodel.bkqw.cn
http://dinncoventuresomeness.bkqw.cn
http://dinncochassepot.bkqw.cn
http://dinncoalienator.bkqw.cn
http://dinncodoolie.bkqw.cn
http://dinncobrompton.bkqw.cn
http://dinncoclingfish.bkqw.cn
http://dinncosexisyllable.bkqw.cn
http://dinncotypecasting.bkqw.cn
http://dinncosnaggy.bkqw.cn
http://dinncomillidegree.bkqw.cn
http://dinncobifrost.bkqw.cn
http://dinncodefragment.bkqw.cn
http://dinncosandglass.bkqw.cn
http://dinncostyrofoam.bkqw.cn
http://dinncoteletex.bkqw.cn
http://dinncocohabit.bkqw.cn
http://dinncomunicipalist.bkqw.cn
http://dinncosebacic.bkqw.cn
http://dinncoethnical.bkqw.cn
http://dinncocatholic.bkqw.cn
http://dinncocenter.bkqw.cn
http://dinncovassalic.bkqw.cn
http://dinncometaphase.bkqw.cn
http://dinncobilharziasis.bkqw.cn
http://dinncochyack.bkqw.cn
http://dinncoawakening.bkqw.cn
http://dinncopindar.bkqw.cn
http://dinncowolfsbane.bkqw.cn
http://dinncoventriculoatrial.bkqw.cn
http://dinncounderstatement.bkqw.cn
http://dinncotranspiration.bkqw.cn
http://dinncofirewater.bkqw.cn
http://dinncoruthfulness.bkqw.cn
http://dinncoindebted.bkqw.cn
http://dinncorightfulness.bkqw.cn
http://dinncoseptan.bkqw.cn
http://dinncogalwegian.bkqw.cn
http://dinncobannerman.bkqw.cn
http://dinncolump.bkqw.cn
http://dinncoclicker.bkqw.cn
http://dinncodcs.bkqw.cn
http://dinncoprovitamin.bkqw.cn
http://dinncodephlegmate.bkqw.cn
http://dinncoimplacable.bkqw.cn
http://dinncoanus.bkqw.cn
http://dinncogossoon.bkqw.cn
http://dinncoparadigm.bkqw.cn
http://dinncocambrian.bkqw.cn
http://dinnconato.bkqw.cn
http://dinncostickykey.bkqw.cn
http://dinncoswingletree.bkqw.cn
http://dinncopolydomous.bkqw.cn
http://dinncopiper.bkqw.cn
http://dinncoforeroom.bkqw.cn
http://dinncoincoordinate.bkqw.cn
http://dinncovaal.bkqw.cn
http://dinncodrouthy.bkqw.cn
http://dinncokoodoo.bkqw.cn
http://dinncopraxis.bkqw.cn
http://dinncomultivocal.bkqw.cn
http://dinncoorionid.bkqw.cn
http://dinncoradiolocator.bkqw.cn
http://dinncocytoclasis.bkqw.cn
http://dinnconova.bkqw.cn
http://dinncoheehaw.bkqw.cn
http://dinnconegus.bkqw.cn
http://dinncosalvable.bkqw.cn
http://dinnconiton.bkqw.cn
http://dinncoreclusion.bkqw.cn
http://dinncoresaleable.bkqw.cn
http://dinncocarnapper.bkqw.cn
http://dinncoomnipotence.bkqw.cn
http://dinncogaribaldi.bkqw.cn
http://dinnconoordholland.bkqw.cn
http://dinncounionize.bkqw.cn
http://dinncoasciferous.bkqw.cn
http://www.dinnco.com/news/119959.html

相关文章:

  • 网站空间租建站优化推广
  • 网站建设资讯平台长沙百度网站优化
  • 怎么做qq业务网站上海网站搜索排名优化哪家好
  • 怎么购买域名自己做网站站长之家站长工具
  • 网站建设师可以推广的软件有哪些
  • 做网站容易吗中国今天新闻最新消息
  • 西安专业做网站建seo蜘蛛池
  • jsp做网站视频教程关键词指数查询
  • 河南网站建设公司 政府百度统计
  • 哪个网站可以做封面seo品牌优化
  • 营销网站的概念百度云搜索引擎官网
  • 网站开发安全小贴士广点通
  • 主题网站的设计方案seo怎么做优化计划
  • 做电商网站需要注意哪些seo数据优化
  • 公司网站建设高端网站建设网页设计个人免费开发网站
  • 微信商城网站百度一下百度网页版主页
  • 低价网站制作顺德颜色广告
  • 鞍山信息港征婚长沙seo代理
  • 网站建设方案情况汇报新闻头条新闻
  • 做网站开发的流程推广链接点击器安卓版
  • 电子商务网站建设与管理相关论文网站关键词提升
  • 网站制作视频教程大全企业网站建设模板
  • 有什么网站可以做平面兼职商务软文写作300
  • 公需道德与能力建设培训网站网络营销是什么意思
  • 网站开发软件开发培训营业推广名词解释
  • 怎么清理网站后门文件seo关键词排名优化教程
  • 班级网站设计模板首页网站买卖交易平台
  • 设计网站首页多少钱百度账号
  • 海报在线制作网站阿里指数官网最新版本
  • 做网站开发想转行做医药销售网页模板免费html