您当前的位置: 首页 > 

喜欢猪猪

暂无认证

  • 1浏览

    0关注

    228博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

List Stream 的常规用法

喜欢猪猪 发布时间:2020-03-16 10:29:35 ,浏览量:1

1. 常规元素去重

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

    // Set集合去重,保持原来顺序     public static void ridRepeat2(List list) {         System.out.println("list = [" + list + "]");         List listNew = new ArrayList();         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 list) {         System.out.println("list = [" + list + "]");         Set set = new HashSet();         List listNew = new ArrayList();         set.addAll(list);         listNew.addAll(set);         System.out.println("listNew = [" + listNew + "]");     }

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

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

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

 

使用JDK1.8的去重

 //利用java8的stream去重
  List uniqueList = list.stream().distinct().collect(Collectors.toList());
  System.out.println(uniqueList.toString());
2. 对象去重

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

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

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

        //根据name,age属性去重         List 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;     }

关注
打赏
1648795721
查看更多评论
立即登录/注册

微信扫码登录

0.2277s