前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >List Stream 的常规用法

List Stream 的常规用法

原创
作者头像
小马哥学JAVA
发布2022-11-21 13:35:24
4220
发布2022-11-21 13:35:24
举报
文章被收录于专栏:JAVA开发专栏

1. 常规元素去重

// 遍历后判断赋给另一个List集合,保持原来顺序 public static void ridRepeat1(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(); for (String str : list) { if (!listNew.contains(str)) { listNew.add(str); } } System.out.println("listNew = [" + listNew + "]"); }

// Set集合去重,保持原来顺序 public static void ridRepeat2(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(); Set set = new HashSet(); for (String str : list) { if (set.add(str)) { listNew.add(str); } } System.out.println("listNew = [" + listNew + "]"); }

// Set去重 由于Set(HashSet)的无序性,不会保持原来顺序 public static void ridRepeat3(List<String> list) { System.out.println("list = [" + list + "]"); Set set = new HashSet(); List<String> listNew = new ArrayList<String>(); set.addAll(list); listNew.addAll(set); System.out.println("listNew = [" + listNew + "]"); }

// Set通过HashSet去重(将ridRepeat3方法缩减为一行) 无序 public static void ridRepeat4(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new HashSet(list)); System.out.println("listNew = [" + listNew + "]"); }

// Set通过TreeSet去重 会按字典顺序重排序 public static void ridRepeat5(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new TreeSet<String>(list)); System.out.println("listNew = [" + listNew + "]"); }

// Set通过LinkedHashSet去重 保持原来顺序 public static void ridRepeat6(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new LinkedHashSet<String>(list)); System.out.println("listNew = [" + listNew + "]"); }

使用JDK1.8的去重

代码语言:javascript
复制
 //利用java8的stream去重
  List uniqueList = list.stream().distinct().collect(Collectors.toList());
  System.out.println(uniqueList.toString());

2. 对象去重

解决对象去重,可以利用for循环遍历的方式进行判断去重,但今天我不准备探究这种方法,要使用的是如下两种:

//根据name属性去重 List<User> unique1 = userList.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new));

System.out.println(unique1.toString());

//根据name,age属性去重 List<User> unique2 = userList.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new) );

System.out.println(unique2.toString());

2.2 对象中重写equals()方法和hashCode()方法

//重写equals方法 @Override public boolean equals(Object obj) { User user = (User) obj; return name.equals(user.getName()) && (age==user.getAge()); }

//重写hashCode方法 @Override public int hashCode() { String str = name + age; return str.hashCode(); }

3. equals()方法和hashCode()方法探究

/** * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code * String} object that represents the same sequence of characters as this * object. * * @param anObject * The object to compare this {@code String} against * * @return {@code true} if the given object represents a {@code String} * equivalent to this string, {@code false} otherwise * * @see #compareTo(String) * @see #equalsIgnoreCase(String) */ public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 常规元素去重
  • 2. 对象去重
    • 2.2 对象中重写equals()方法和hashCode()方法
    • 3. equals()方法和hashCode()方法探究
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档