List Stream 的常规用法

简介: List Stream 的常规用法

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的去重

//利用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;

   }

相关文章
|
5月前
|
安全 C#
C# List基本用法
C# List基本用法
|
5月前
|
消息中间件 NoSQL Java
别再用 Redis List 实现消息队列了,Stream 专为队列而生
别再用 Redis List 实现消息队列了,Stream 专为队列而生
131 0
|
5月前
|
数据处理
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
51 0
|
4月前
|
消息中间件 负载均衡 NoSQL
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
51 0
|
4月前
|
存储 索引 Python
Python教程:深入了解 Python 中 Dict、List、Tuple、Set 的高级用法
Python 中的 Dict(字典)、List(列表)、Tuple(元组)和 Set(集合)是常用的数据结构,它们各自有着不同的特性和用途。在本文中,我们将深入了解这些数据结构的高级用法,并提供详细的说明和代码示例。
76 2
|
4月前
|
存储 C++
C++初阶学习第十一弹——探索STL奥秘(六)——深度刨析list的用法和核心点
C++初阶学习第十一弹——探索STL奥秘(六)——深度刨析list的用法和核心点
46 7
|
4月前
|
Java
java操作list使用Stream
java操作list使用Stream
|
5月前
|
SQL 数据处理 HIVE
【Hive】写出Hive中split、coalesce及collect_list函数的用法?
【4月更文挑战第17天】【Hive】写出Hive中split、coalesce及collect_list函数的用法?
|
5月前
【stream】List根据某个字段求和
【stream】List根据某个字段求和
287 0
|
5月前
|
Java API
java 对象list 使用stream进行过滤
在Java中,你可以使用Stream API对对象列表进行过滤。假设你有一个`List<MyObject>`,并且你想根据某些条件过滤出特定的对象。以下是一个示例: ```java import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<MyObject> myObjects = ... // 初始化你的对象列表 List<MyObject> filter
330 1
下一篇
无影云桌面