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

广州市用工备案在哪个网站做制作网站需要什么

广州市用工备案在哪个网站做,制作网站需要什么,合肥网页设计,wordpress地址表单JAVA实体类集合该如何去重? 最近在工作中经常遇到需要去重的需求,所以特意系统的来梳理一下 有目录,不迷路 JAVA实体类集合该如何去重?单元素去重方法一:利用Set去重方法二:利用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) {// 生成含重复元素的集合并打印List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(2);list.add(3);System.out.println("去重前:" + list);// 利用HashSet去重后并打印HashSet<Integer> hashSet = new HashSet<>(list);list.clear();list.addAll(hashSet);System.out.println("去重后:" + list);}

执行:

在这里插入图片描述

方法二:利用java 8的stream写法,方便优雅快捷高效

 public static void main(String[] args) {// 生成含重复元素的集合并打印List<Integer> 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) {// 生成学生集合List<Student> 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根据姓名去重Map<String, 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) {// 生成学生集合List<Student> 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) {// 生成学生集合List<Student> 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去重
//        Set<Student> set = new TreeSet<>(((o1, o2) -> o1.getName().compareTo(o2.getName())));Set<Student> 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) {// 生成学生集合List<Student> 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) {// 生成学生集合List<Student> 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.Predicate<T>**/public static <T> Predicate<T> distinctKey(Function<? super T, Object> keyExtractor) {Map<Object, Boolean> map = new ConcurrentHashMap<>();return o -> map.putIfAbsent(keyExtractor.apply(o), Boolean.TRUE) == null;}

执行:

在这里插入图片描述

补充

补充一点,如何提取去重后的单元素集合:

public static void main(String[] args) {// 生成学生集合List<Student> 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);// 提取去重后的姓名List<String> nameList = studentList.stream().map(Student::getName).distinct().collect(Collectors.toList());System.out.println("去重后: " + nameList);}

执行:

在这里插入图片描述

多属性去重

方法一:利用map根据姓名、年龄去重
public static void main(String[] args) {// 生成学生集合List<Student> 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根据姓名、年龄去重Map<String, 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) {// 生成学生集合List<Student> 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) {// 生成学生集合List<Student> 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) {// 生成学生集合List<Student> 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) {// 生成学生集合List<Student> 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()方法来对比,根据姓名和年龄去重List<Student> 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) {// 生成学生集合List<Student> 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,根据姓名和年龄去重Set<Student> set = new HashSet<>(studentList);studentList.clear();studentList.addAll(set);System.out.println("去重后: " + studentList);}

执行:

在这里插入图片描述
完结撒花!!!
在这里插入图片描述

http://www.tj-hxxt.cn/news/13604.html

相关文章:

  • 丰台区社会建设网站互联网网站
  • 建设商务网站的步骤搜索引擎优化不包括
  • vue做普通网站页面跳转谷歌推广哪家公司好
  • 设计师的网站十大最靠谱教育培训机构
  • 如何查询公司网站顶级域名网站开发流程图
  • 做网站设计需要学会哪些最新网络营销方式
  • 上海市城乡建设管理委员会网站windows优化大师
  • 软件技术毕业设计郑州seo优化大师
  • 网页美工设计说明书成都最好的seo外包
  • 徐州专业网站建设公司廊坊seo
  • 做网站一定要买服务器么短链接生成
  • 邢台网站制作多少钱寰宇seo
  • 临猗县 保障住房和建设住建网站站长之家点击进入
  • 网站建设化妆品的目录给我免费播放片高清在线观看
  • wordpress 快递公司seo搜索引擎优化价格
  • 做网站工作百度权重提升
  • 广州网站建设信科便宜上海最近3天疫情情况
  • 微信网站开发视频教程如何注册网站平台
  • 企业做网站需要租服务器吗网站一键生成
  • 网页微信下载360优化大师旧版
  • 免费推广网站入口2023无锡seo培训
  • 广州制作网站公司互联网推广运营是做什么的
  • 聚美优品的网站建设外贸电商平台哪个网站最好
  • 精品网站建设需要多少钱泰安seo培训
  • 上海新闻最新消息广州网页seo排名
  • 受欢迎的免费网站建设长沙网站包年优化
  • 新乡做网站的全渠道营销案例
  • 互联网与网站有哪些人民日报新闻消息
  • 公司注册资金实缴可以取出来吗重庆百度快照优化
  • 网站建设国际深圳广东疫情防控措施