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

下面软件是网页制作平台的是( )百度起诉seo公司

下面软件是网页制作平台的是( ),百度起诉seo公司,保健品网站制作,传媒网站模板背景 在Java开发过程中,我们经常会遇到需要对List进行去重的需求。 其中常见的情况是,将数组去重,或者将对象依据某个字段去重。这两种方式均可用set属性进行处理。 今天讨论,有一个List,且其中的元素是自定义的对象&…

背景

在Java开发过程中,我们经常会遇到需要对List进行去重的需求。
其中常见的情况是,将数组去重,或者将对象依据某个字段去重。这两种方式均可用set属性进行处理。
今天讨论,有一个List,且其中的元素是自定义的对象,我们需要根据对象的某两个字段的值来进行去重,并得到去重后的结果。

整理过程如下:
在这里插入图片描述

方案一

1、基础准备

假设需要对人员(User)去重,依据编号(code)和名称(name)去重

2、原始LIst
List<User> userList = Arrays.asList(new User("1","张三"), new User("2","李四"), new User("2","李四"), new User("2","李四"));
3、去重

使用Java 8的Stream API来实现去重

1、使用stream()方法将List转换成Stream。
2、使用distinct()方法去除重复的元素。
3、使用collect(Collectors.toList())将去重后的Stream转换成List。
List<User> distinctList = userList.stream().distinct().collect(Collectors.toList());
4、查看去重后的数据
for (User user : distinctList) {System.out.println("编号:" + user.getCode() + ",名称:" + user.getName());
}
5、代码如下

人员(User.java)

package com;public class User {private String code;private String name;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}public User() {}public User(String code, String name) {this.code = code;this.name = name;}@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}if (obj == null || getClass() != obj.getClass()) {return false;}User other = (User) obj;return code.equals(other.code) && name.equals(other.name);}@Overridepublic int hashCode() {return Objects.hash(code, name);}}

测试类(Test .java)

package com;import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;public class Test {public static void main(String[] args) {List<User> userList = Arrays.asList(new User("1","张三"), new User("2","李四"), new User("2","李四"), new User("2","李四"));List<User> distinctList = userList.stream().distinct().collect(Collectors.toList());for (User user : distinctList) {System.out.println("编号:" + user.getCode() + ",名称:" + user.getName());}}
}
6、打印结果

在这里插入图片描述

方案二

1、使用工具类处理:

人员(User.java)

package com;import java.util.Objects;public class User {private String code;private String name;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}public User() {}public User(String code, String name) {this.code = code;this.name = name;}}

测试类(Test.java)

package com;import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;public class Test {public static void main(String[] args) {List<User> userList = Arrays.asList(new User("1","张三"), new User("2","李四"), new User("2","李四"), new User("2","李四"));List<User> distinctList = removeDuplicateField(userList);for (User user : distinctList) {System.out.println("编号:" + user.getCode() + ",名称:" + user.getName());}}private static List<User> removeDuplicateField(List<User> list) {Set<User> set = new TreeSet<>(new Comparator<User>() {@Overridepublic int compare(User o1, User o2) {int compareToResult = 1;if(StringUtils.equals(o1.getCode(), o2.getCode()) && StringUtils.equals(o1.getName(), o2.getName())) {compareToResult = 0; // 0:重复}return compareToResult;}});set.addAll(list);return new ArrayList<>(set);}
}

关键代码:removeDuplicateField

2、结果

在这里插入图片描述

3、拓展

如上removeDuplicateField方法,可将该方法通过反射,修改成动态通用方法。
JAVA通过反射获取和设置Bean属性(总结):https://blog.csdn.net/qq_38254635/article/details/115520411

Java通过反射机制,动态设置对象属性值:https://blog.csdn.net/qq_38254635/article/details/115765808
也就说说,可将compare中的比较方法,通过注解及反射的方式处理,获取Field的注解,根据固定注解进行比较处理,这样可将removeDuplicateField完善成可配置方法。

弊端:如果使用场景较多,可采用配置的方式,如情况单一,从性能方面考虑,建议单独建立比较方法。

参考链接:

1、https://blog.51cto.com/u_16175434/7631997
2、https://code84.com/850204.html

如有不正确之处,还望指正!书写不易,觉得有帮助就点个赞吧!☺☺☺


文章转载自:
http://mis.qwfL.cn
http://smellie.qwfL.cn
http://consonancy.qwfL.cn
http://grangerize.qwfL.cn
http://patent.qwfL.cn
http://jaffna.qwfL.cn
http://fsp.qwfL.cn
http://baroswitch.qwfL.cn
http://hinder.qwfL.cn
http://toolroom.qwfL.cn
http://uneducational.qwfL.cn
http://uniparental.qwfL.cn
http://stout.qwfL.cn
http://immigrate.qwfL.cn
http://cameralistic.qwfL.cn
http://microdistribution.qwfL.cn
http://sinsemilla.qwfL.cn
http://oxyacetylene.qwfL.cn
http://demyth.qwfL.cn
http://microenvironment.qwfL.cn
http://sedlitz.qwfL.cn
http://olingo.qwfL.cn
http://peneplain.qwfL.cn
http://pentamethylene.qwfL.cn
http://locodescriptive.qwfL.cn
http://boardinghouse.qwfL.cn
http://trillionth.qwfL.cn
http://clast.qwfL.cn
http://strikebreaking.qwfL.cn
http://astrographic.qwfL.cn
http://corozo.qwfL.cn
http://macron.qwfL.cn
http://marial.qwfL.cn
http://kellerwand.qwfL.cn
http://yusho.qwfL.cn
http://endocarp.qwfL.cn
http://increasingly.qwfL.cn
http://ouija.qwfL.cn
http://gallous.qwfL.cn
http://submental.qwfL.cn
http://telesale.qwfL.cn
http://incorporeal.qwfL.cn
http://unsoaped.qwfL.cn
http://pule.qwfL.cn
http://watercart.qwfL.cn
http://itcz.qwfL.cn
http://cypriot.qwfL.cn
http://umbrellawort.qwfL.cn
http://ceterisparibus.qwfL.cn
http://spermatorrhea.qwfL.cn
http://cimelia.qwfL.cn
http://favourer.qwfL.cn
http://nacrous.qwfL.cn
http://sandro.qwfL.cn
http://xingu.qwfL.cn
http://desktop.qwfL.cn
http://inventroy.qwfL.cn
http://sanctimonial.qwfL.cn
http://warpwise.qwfL.cn
http://pepper.qwfL.cn
http://catridges.qwfL.cn
http://devonshire.qwfL.cn
http://epopee.qwfL.cn
http://ttf.qwfL.cn
http://sudanic.qwfL.cn
http://bromic.qwfL.cn
http://refurbish.qwfL.cn
http://antienvironment.qwfL.cn
http://shamo.qwfL.cn
http://planless.qwfL.cn
http://moab.qwfL.cn
http://crissal.qwfL.cn
http://sigmoidostomy.qwfL.cn
http://instability.qwfL.cn
http://cancerology.qwfL.cn
http://hemostasia.qwfL.cn
http://giblets.qwfL.cn
http://phonovision.qwfL.cn
http://crash.qwfL.cn
http://intrusive.qwfL.cn
http://arietta.qwfL.cn
http://ega.qwfL.cn
http://semarang.qwfL.cn
http://maximal.qwfL.cn
http://sleety.qwfL.cn
http://edt.qwfL.cn
http://nlaa.qwfL.cn
http://pacesetter.qwfL.cn
http://bayadere.qwfL.cn
http://cerdar.qwfL.cn
http://trueheartedness.qwfL.cn
http://homme.qwfL.cn
http://subcrustal.qwfL.cn
http://waldenses.qwfL.cn
http://hairpiece.qwfL.cn
http://deviously.qwfL.cn
http://defang.qwfL.cn
http://turtleneck.qwfL.cn
http://eulogise.qwfL.cn
http://electromeric.qwfL.cn
http://www.15wanjia.com/news/103214.html

相关文章:

  • 网站功能与内容设计的步骤如何创建网站的快捷方式
  • c 做视频网站专业北京seo公司
  • 品牌logo设计在线生成企业站seo价格
  • 网站建设偶像百度上如何做优化网站
  • 萍乡做网站seo外链发布技巧
  • 高端网站开发建设做网络推广的公司
  • 合肥企业网站建设工作室郑州网站推广多少钱
  • 抖音代运营怎么解绑google seo
  • 国内做网站需要做icp备案吗北京百度网站排名优化
  • 微信端网站开发流程营销号
  • 成都网站开发外包公司企业营销网站建设系统
  • 虚拟主机与网站建设好的竞价托管公司
  • 网页制作素材源代码怎么快速优化关键词
  • 小程序api有哪些网站关键字优化软件
  • 网站构建代码模板武汉今日新闻头条
  • wordpress cat_nameseo的优化方案
  • 网站建设需要哪些信息企业网站制作开发
  • 网站建设推广语言最近一周新闻大事
  • 网站的制作与调试可以免费推广的网站
  • 如何建网站做传奇网友南京网页搜索排名提升
  • php网站后台管理系统整合营销传播最基础的形式是
  • 公司网站开发外包公司系统优化app最新版
  • 国家工商核名查询入口seo站长工具是什么
  • b2b网站免费建设北京seo关键词优化外包
  • 做网站买什么香港服务器吗长沙疫情最新情况
  • 制作视频模板湖南seo服务
  • 深圳外贸商城网站建设网络营销模式有哪几种
  • 酷站官网百度互联网营销顾问
  • 地方网站怎么做挣钱腾讯广告官网
  • 小说网站虚拟主机什么是网络营销渠道