Java 如何在列表中获取唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26152000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:54:49  来源:igfitidea点击:

How to get unique values in List

javalistunique-values

提问by Junior Fulcrum

I have 2 text files with data. I am reading these files with BufferReaderand putting the data of one column per file in a List<String>.

我有 2 个带有数据的文本文件。我正在阅读这些文件BufferReader并将每个文件的一列数据放入List<String>.

I have duplicated data in each one, but I need to have unique data in the first Listto confront with the duplicated data in the second List.

我在每个文件中都有重复的数据,但我需要在第一个中有唯一的数据List来面对第二个中的重复数据List

How can I get unique values from a List?

如何从 a 中获取唯一值List

回答by Robby Cornelissen

Convert the ArrayListto a HashSet.

将 转换ArrayListHashSet

List<String> listWithDuplicates; // Your list containing duplicates
Set<String> setWithUniqueValues = new HashSet<>(listWithDuplicates);

If for some reason, you want to convert the set back to a list afterwards, you can, but most likely there will be no need.

如果出于某种原因,您想在之后将集合转换回列表,则可以,但很可能不需要。

List<String> listWithUniqueValues = new ArrayList<>(setWithUniqueValues);

回答by Junior Fulcrum

i just realize a solution may be it can be helpful for other persons. first will be populated with duplicated values from BufferReader.

我只是意识到一个解决方案可能对其他人有帮助。首先将填充来自 BufferReader 的重复值。

ArrayList<String> first = new ArrayList<String>();  

To extract Unique values i just create a new ArrayList like down:

要提取唯一值,我只需创建一个新的 ArrayList,如下所示:

ArrayList<String> otherList = new ArrayList<>();

    for(String s : first) {
        if(!otherList.contains(s))
            otherList.add(s);
    }

A lot of post in internet are all speaking to assign my Arraylist to a List , Set , HashTable or TreeSet. Can anyone explain the difference in theory and whitch one is the best tu use in practice ? thnks for your time guys.

互联网上的很多帖子都在谈论将我的 Arraylist 分配给 List 、 Set 、 HashTable 或 TreeSet 。任何人都可以解释理论上的差异,哪个是最好的实践?谢谢你们的时间。

回答by Bohemian

It can be done one one line by using an intermediate Set:

可以通过使用中间件一行一行地完成Set

List<String> list = new ArrayList<>(new HashSet<>(list));

In java 8, use distinct()on a stream:

在 java 8 中,distinct()在流上使用:

List<String> list = list.stream().distinct().collect(Collectors.toList());

Alternatively, don't use a List at all; just use a Set (like HashSet) from the start for the collection you only want to hold unique values.

或者,根本不要使用 List;只需从一开始就为您只想保存唯一值的集合使用 Set(如 HashSet)。

回答by Jorciney

In Java 8:

在 Java 8 中

     // List with duplicates
     List<String> listAll = Arrays.asList("A", "A", "B", "C", "D", "D");

     // filter the distinct 
     List<String> distinctList = listAll.stream()
                     .distinct()
                     .collect(Collectors.toList());

    System.out.println(distinctList);// prints out: [A, B, C, D]

this will also work with objects, but you will probably have to adapt your equals method.

这也适用于对象,但您可能需要调整您的 equals 方法。