如何从list <String>中删除重复的元素?

时间:2019-07-03 01:21:21

标签: java arrays collections java-8

列表列表= [“ f1,f2”,“ f2,f3”,“ f4,f5”,“ f2,f1”,“ f5,f4”];

输出列表将是 清单uniqueList = [“ f1,f2”,“ f2,f3”,“ f4,f5”]

4 个答案:

答案 0 :(得分:1)

使用其他课程:

    static class Pair {
    String a, b;

    Pair(String s) {
        String[] arr = s.split(",");
        this.a = arr[0];
        this.b = arr[1];
    }

    static String pairToString(Pair p) {
        return p.a + "," + p.b;
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b) + Objects.hash(b, a);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pair p = (Pair) o;
        return (p.a.equals(a) && p.b.equals(b)) || (p.b.equals(a) && p.a.equals(b));
    }

}

现在您可以使用:

 public static void main(String[] args) {
        List<String> list = Arrays.asList("f1,f2", "f2,f3", "f4,f5", "f2,f1", "f5,f4");
        List<String> strings = list
                .stream()
                .map(Pair::new)
                .distinct()
                .map(Pair::pairToString)
                .collect(Collectors.toList());
    }

答案 1 :(得分:1)

我还有另一种解决方案。如果您不想准备另一个类来比较List中的值。您可以用逗号分隔每个值并对这些数据进行排序。之后,您可以再次将它们转换为字符串集

public static void main(String[] args) {
    List<String> stringList = Arrays.asList("f1,f2", "f2,f3", "f4,f5", "f2,f1", "f5,f4");
    Set<String> result = new HashSet<>();
    for (String s : stringList) {
        String[] elements = s.split(",");
        Arrays.sort(elements);
        result.add(Arrays.toString(elements));
    }

    for (String e : result){
        System.out.println(e);
    }
}

答案 2 :(得分:0)

我创建了一个类来对配对进行建模,并覆盖了equals方法,将“ f1,f2”和“ f2,f1”视为等号,然后使用HashSet找出重复项。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class so1 {
    public static void main(String[] args) {

        List<String> list = Arrays.asList(new String[] {"f1,f2","f2,f3","f4,f5","f2,f1","f5,f4"});

        List<pair> plist = new ArrayList<pair>();
        for (int i = 0; i < list.size(); i++) {
            plist.add(new pair(list.get(i)));
        }

        HashSet<pair> hs = new HashSet<pair>(); 
        for (int i = 0; i < plist.size(); i++) {
            if(!hs.add(plist.get(i))){
                System.out.println("Found duplicate "+plist.get(i).toString());
            }
        }

        List<String> uniqueItems = new ArrayList<String>();
        for (Iterator iterator = hs.iterator(); iterator.hasNext();) {
            pair pair = (pair) iterator.next();
            uniqueItems.add(pair.toString());
        }

        System.out.println(uniqueItems);


    }
}

class pair{
    pair(String inp){
        String[] tokens = inp.split(",");
        Arrays.sort(tokens);
        for(String t: tokens){
            elements.add(t);
        }
    }
    List<String> elements = new ArrayList<>();

    @Override
    public String toString() {
        return ""+elements;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((elements == null) ? 0 : elements.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        pair other = (pair) obj;
        if (elements == null) {
            if (other.elements != null)
                return false;
        } else if (!elements.equals(other.elements))
            return false;
        return true;
    }
}

答案 3 :(得分:-6)

这是https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/的几个答案

Java 7

    // Create a new ArrayList 
    ArrayList<String> uniqueList = new ArrayList<String>(); 

    // Traverse through the first list 
    for ( element : list) { 

        // If this element is not present in uniqueList
        // then add it 
        if (!uniqueList.contains(element)) { 

            uniqueList.add(element); 
        } 
    } 

Java 8

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