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

五大类型网站输入文字生成图片app

五大类型网站,输入文字生成图片app,百度极速版下载安装最新版,wordpress 显示小工具JAVA实体类集合该如何去重#xff1f; 最近在工作中经常遇到需要去重的需求#xff0c;所以特意系统的来梳理一下 有目录#xff0c;不迷路 JAVA实体类集合该如何去重#xff1f;单元素去重方法一#xff1a;利用Set去重方法二#xff1a;利用java 8的stream写法#xf…JAVA实体类集合该如何去重 最近在工作中经常遇到需要去重的需求所以特意系统的来梳理一下 有目录不迷路 JAVA实体类集合该如何去重单元素去重方法一利用Set去重方法二利用java 8的stream写法方便优雅快捷高效 实体类对象去重单属性去重方法一利用map去重方法二利用map去重java 8语法方法三利用Set去重方法四 利用Set去重java 8写法方法五定义过滤器补充 多属性去重方法一利用map根据姓名、年龄去重方法二利用map根据姓名、年龄去重java 8写法方法三利用Set根据姓名、年龄去重java 8写法重写equals()和hashCode()方法方法四利用java 8 的distinct()最推荐方法五通过contains()方法来调用equals()方法来对比方法六重写方法后通过Set去重 单元素去重 方法一利用Set去重 public static void main(String[] args) {// 生成含重复元素的集合并打印ListInteger list new ArrayList();list.add(1);list.add(2);list.add(2);list.add(3);System.out.println(去重前 list);// 利用HashSet去重后并打印HashSetInteger hashSet new HashSet(list);list.clear();list.addAll(hashSet);System.out.println(去重后 list);}执行 方法二利用java 8的stream写法方便优雅快捷高效 public static void main(String[] args) {// 生成含重复元素的集合并打印ListInteger list new ArrayList();list.add(2);list.add(3);list.add(3);list.add(4);System.out.println(去重前 list);// 利用java 8的stream写法list list.stream().distinct().collect(Collectors.toList());System.out.println(去重后 list);}执行 实体类对象去重 先新建实体类 /*** Author: guqueyue* Description: 学生类* Date: 2023/12/12**/ public class Student {/** 姓名 */private String name;/** 年龄 */private Integer age;public Student() {}public Student(String name, Integer age) {this.name name;this.age age;}Overridepublic String toString() {return Student{ name name \ , age age };}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;} }单属性去重 方法一利用map去重 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 22));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用map根据姓名去重MapString, Student map new HashMap();for (Student student : studentList) {map.put(student.getName(), student);}studentList new ArrayList(map.values());System.out.println(去重后: studentList);}执行得 方法二利用map去重java 8语法 比方法一代码看似简洁了但实际上好似更加复杂了 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用map根据姓名去重java 8语法studentList studentList.stream().collect(Collectors.toMap(student - student.getName(), Function.identity(), (o, n) - n)).values().stream().collect(Collectors.toList());System.out.println(去重后: studentList);}执行 方法三利用Set去重 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用Set去重 // SetStudent set new TreeSet(((o1, o2) - o1.getName().compareTo(o2.getName())));SetStudent set new TreeSet((Comparator.comparing(Student::getName)));set.addAll(studentList);studentList.clear();studentList new ArrayList(set);System.out.println(去重后: studentList);}执行 方法四 利用Set去重java 8写法 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用Set去重java 8写法studentList studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() - new TreeSet(Comparator.comparing(Student::getName))),ArrayList::new));System.out.println(去重后: studentList);}执行 方法五定义过滤器 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 定义过滤器studentList studentList.stream().filter(distinctKey(Student::getName)).collect(Collectors.toList());System.out.println(去重后: studentList);}/*** Description 自定义过滤器* Param [keyExtractor]* return java.util.function.PredicateT**/public static T PredicateT distinctKey(Function? super T, Object keyExtractor) {MapObject, Boolean map new ConcurrentHashMap();return o - map.putIfAbsent(keyExtractor.apply(o), Boolean.TRUE) null;}执行 补充 补充一点如何提取去重后的单元素集合 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 提取去重后的姓名ListString nameList studentList.stream().map(Student::getName).distinct().collect(Collectors.toList());System.out.println(去重后: nameList);}执行 多属性去重 方法一利用map根据姓名、年龄去重 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用map根据姓名、年龄去重MapString, Student map new HashMap();for (Student student : studentList) {map.put(student.getName() _ student.getAge(), student);}studentList new ArrayList(map.values());System.out.println(去重后: studentList);}执行 方法二利用map根据姓名、年龄去重java 8写法 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用map根据姓名、年龄去重java 8写法studentList studentList.stream().collect(Collectors.toMap(s - s.getName() _ s.getAge(), Function.identity(), (o, n) - n)).values().stream().collect(Collectors.toList());System.out.println(去重后: studentList);}执行 方法三利用Set根据姓名、年龄去重java 8写法 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用Set根据姓名、年龄去重java 8写法studentList studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() - new TreeSet(Comparator.comparing(o - o.getName() _ o.getAge()))), ArrayList::new));System.out.println(去重后: studentList);}执行 重写equals()和hashCode()方法 下面的几种方法首先需要在实体类中重写equals()和hashCode()方法 Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;return Objects.equals(name, student.name) Objects.equals(age, student.age);}Overridepublic int hashCode() {return Objects.hash(name, age);}方法四利用java 8 的distinct()最推荐 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 利用java 8 的distinct()根据姓名和年龄去重studentList studentList.stream().distinct().collect(Collectors.toList());System.out.println(去重后: studentList);}执行 方法五通过contains()方法来调用equals()方法来对比 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 通过contains()方法来调用equals()方法来对比根据姓名和年龄去重ListStudent newStudentList new ArrayList();for (Student student : studentList) {if (!newStudentList.contains(student)) {newStudentList.add(student);}}System.out.println(去重后: newStudentList);}执行 方法六重写方法后通过Set去重 public static void main(String[] args) {// 生成学生集合ListStudent studentList new ArrayList();studentList.add(new Student(张三, 18));studentList.add(new Student(张三, 19));studentList.add(new Student(张三, 18));studentList.add(new Student(李四, 18));System.out.println(去重前: studentList);// 通过Set根据姓名和年龄去重SetStudent set new HashSet(studentList);studentList.clear();studentList.addAll(set);System.out.println(去重后: studentList);}执行 完结撒花
http://www.hkea.cn/news/14268351/

相关文章:

  • 浙江省住房和城乡建设厅网站查询wordpress personal主题
  • 网站开发应如何入账网站后台管理的超链接怎么做
  • 永清县建设局网站网上哪里可以注册公司
  • 安徽做网站哪家好seo入门书籍推荐
  • 安徽省建设干校网站做商品网站数据库有哪些内容
  • 网站设计创意方案郫都区规划建设局网站
  • 网站集约化后如何建设网站负责人核验照
  • 车牌照损坏在网站做的能用吗开发公司竣工员工奖励计划
  • 手机网站有什么不同某企业集团网站建设方案论文
  • 免费网站如何赚钱西安口碑较好的财务公司
  • 新闻文章网站源码中国十大证券公司排名
  • wordpress注册提示404新网站怎么做seo 风享
  • 淮安新港建设有限公司网站免费做代理
  • 河北邯郸手机网站建设时事新闻2022最新10月
  • 南宁市优化网站公司曲周网站建设
  • 湘西建设监理协会网站手机制作ppt
  • 怎么修改自己的网站品牌设计公司介绍
  • 河南省工程建设业协会网站绥化建设网站
  • wordpress自带功能大连seo外包
  • 中英 网站模板 带手机版定制一个企业网站多少钱
  • 湛江网站制作推广做网站要通过网信办备案吗
  • 2013影响网站百度搜索排名的关键因素统计win2008 r2 搭建网站
  • 外贸商做英文网站的目的下载百度卫星导航
  • 网络公司网站源码 网络建设工作室网站模板 织梦广告设计公司源码单机版网页制作软件
  • 购物网站的设计思路黑马程序员广州校区
  • 哈尔滨专业建网站方案重庆做网站那里好
  • 网站建设招标公示企业咨询管理公司
  • wordpress一站式开发百度网站推广怎么做
  • 网站后台补丁如何做游戏网站开发什么意思
  • 网站开发销售提成成都企业网站制作哪家好