如何从ArrayList获取重复值?

时间:2022-09-15 16:30:03

I have a ArrayList: ArrayList<String> buffer = new ArrayList<String>();

我有一个ArrayList:ArrayList buffer = new ArrayList ();

How can I take duplicated values from ArrayList?

如何从ArrayList中获取重复值?

Example:

例:

fsfs.txt
erwre.txt
wery.txt
wtrtr.txt
erwre.txt
qweq.txt

My attempts:

我的尝试:

With cycles:

有周期:

for(int i = 0; i < buffer.size(); i++) {
    for(int j = i + 1; j < buffer.size(); j++) {
        if( buffer.get(i).equals(buffer.get(j)) ) {
            bufferTemp.add(j, buffer.toString() ); 
            j--;
        }
    }
 }

With iterator:
Iterator<String> i = buffer.iterator();
Iterator<String> j = buffer.iterator();
j.next();
     while(i.hasNext() && j.hasNext()) {
        if( i.next().equals(j.next() )
        System.out.println(i.next());
     }

Also I try to use Comparable, Comparator and other ways but it don't work.

此外,我尝试使用Comparable,Comparator和其他方法,但它不起作用。

2 个解决方案

#1


0  

If I understand you correctly, you use Java and want to get the duplicates of an ArrayList.

如果我理解正确,您使用Java并希望得到ArrayList的副本。

If you use Strings, then you can just sort the List and compare in a loop the previous with the current element. Look at this (Java 7):

如果你使用字符串,那么你可以只对List进行排序,并在循环中比较前一个与当前元素。看看这个(Java 7):

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

list.add("fsfs.txt");
list.add("erwre.txt");
list.add("wery.txt");
list.add("wtrtr.txt");
list.add("erwre.txt");
list.add("qweq.txt");

// Sort the list
Collections.sort(list);

// Test if List was sorted correctly
for(String s : list)
{
    System.out.println(s);
}

System.out.println("\n*** Now try to get duplicates ***\n");

Iterator<String> listIt = list.iterator();

String prev = "";
boolean foundDuplicate = false;
while(listIt.hasNext())
{
    String current = listIt.next();

    if(current.equals(prev))
        foundDuplicate = true;
    else
        foundDuplicate = false;

    if(foundDuplicate)
    {
        // Duplicate found!
        System.out.println(current);
    }

    prev = current;
}

Output should be:

输出应该是:

erwre.txt
erwre.txt
fsfs.txt
qweq.txt
wery.txt
wtrtr.txt

*** Now try to get duplicates ***

erwre.txt

#2


1  

You can create a Set passing the your list as a argument. Set will take care of duplicates.

您可以创建一个Set,将列表作为参数传递。 Set将负责重复。

private static List<String> list = new ArrayList<String>();

public static void main(String[] args) {

    list.add("aaa.txt");
    list.add("aaa.txt");
    list.add("aaa.txt");
    list.add("bbb.txt");
    list.add("ccc.txt");
    list.add("ccc.txt");

    Set<String> set = new HashSet<String>(list);

    System.out.println(set);
}

#1


0  

If I understand you correctly, you use Java and want to get the duplicates of an ArrayList.

如果我理解正确,您使用Java并希望得到ArrayList的副本。

If you use Strings, then you can just sort the List and compare in a loop the previous with the current element. Look at this (Java 7):

如果你使用字符串,那么你可以只对List进行排序,并在循环中比较前一个与当前元素。看看这个(Java 7):

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

list.add("fsfs.txt");
list.add("erwre.txt");
list.add("wery.txt");
list.add("wtrtr.txt");
list.add("erwre.txt");
list.add("qweq.txt");

// Sort the list
Collections.sort(list);

// Test if List was sorted correctly
for(String s : list)
{
    System.out.println(s);
}

System.out.println("\n*** Now try to get duplicates ***\n");

Iterator<String> listIt = list.iterator();

String prev = "";
boolean foundDuplicate = false;
while(listIt.hasNext())
{
    String current = listIt.next();

    if(current.equals(prev))
        foundDuplicate = true;
    else
        foundDuplicate = false;

    if(foundDuplicate)
    {
        // Duplicate found!
        System.out.println(current);
    }

    prev = current;
}

Output should be:

输出应该是:

erwre.txt
erwre.txt
fsfs.txt
qweq.txt
wery.txt
wtrtr.txt

*** Now try to get duplicates ***

erwre.txt

#2


1  

You can create a Set passing the your list as a argument. Set will take care of duplicates.

您可以创建一个Set,将列表作为参数传递。 Set将负责重复。

private static List<String> list = new ArrayList<String>();

public static void main(String[] args) {

    list.add("aaa.txt");
    list.add("aaa.txt");
    list.add("aaa.txt");
    list.add("bbb.txt");
    list.add("ccc.txt");
    list.add("ccc.txt");

    Set<String> set = new HashSet<String>(list);

    System.out.println(set);
}