在列表中查找重复的字符串并使其唯一

use*_*474 8 java list unique duplicates

我有一个ArrayList重复的字符串值,并希望通过附加计数使重复项唯一.

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("b");
    list.add("c");
    list.add("a");
    list.add("a");
    list.add("a");

    HashSet<String> set = new HashSet<String>();
    List<String> duplicateList = new ArrayList<String>();

    for (String item : list) {
        // If String is not in set, add it to the list and the set.
        if (!set.contains(item)) {              
            set.add(item);
        } else {
            duplicateList.add(item);
        }
    }

    for (String element : duplicateList) {
        System.out.println(element);
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法使列表像:

a
b
c
d
b1
c1
a1
a2
a3
Run Code Online (Sandbox Code Playgroud)

Nic*_*tto 11

假设你使用Java 8,如果你想获得你的每个值的重复总量List,你可以这样做,这要归功于Stream API按值分组然后计算每个值的出现次数:

Map<String, Long> counter = list.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(counter);
Run Code Online (Sandbox Code Playgroud)

输出:

{a=4, b=2, c=2, d=1}
Run Code Online (Sandbox Code Playgroud)

如果要通过在原始文件末尾添加计数器来防止重复String,可以使用Elliott FrischLinkedHashSet建议的保留值的排序.

Elliott Frisch的方法略有不同:

List<String> list = Arrays.asList("a", "b", "c", "d", "b", "c", "a", "a", "a");
Set<String> set = new LinkedHashSet<>();
for (String str : list) {
    String value = str;
    // Iterate as long as you can't add the value indicating that we have
    // already the value in the set
    for (int i = 1; !set.add(value); i++) {
        value = str + i;
    }
}
System.out.println(set);
Run Code Online (Sandbox Code Playgroud)

输出:

[a, b, c, d, b1, c1, a1, a2, a3]
Run Code Online (Sandbox Code Playgroud)


Mur*_*nik 9

看起来你有正确的想法.您只需要使用a Map并实际计算遇到的字符串,而不是仅仅注意到遇到它们:

Map<String, Integer> counter = new HashMap<>();
List<String> duplicateList = new ArrayList<>();

for (String item : list) {

    // If String is not in set, add it to the list and the set, and 
    // note this is the first time it's encountered
    if (!counter.containsKey(item)) {
        duplicateList.add(item);
        counter.put(item, 1);
    } else {
        Integer count = counter.get(item);
        duplicateList.add(item + count);
        item.put(item, count + 1);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ell*_*sch 5

您可以使用 a LinkedHashSet,您可以使用它Arrays.asList(T...)来初始化您的List. 首先,检查集合是否包含来自list. 如果是,则迭代值,直到找到尚未出现的值。就像是,

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", 
        "b", "c", "a", "a", "a"));
Set<String> mySet = new LinkedHashSet<>();
for (String str : list) {
    if (mySet.contains(str)) {
        int i = 1;
        while (mySet.contains(str + i)) {
            i++;
        }
        str = str + i;
    }
    mySet.add(str);
}
System.out.println(mySet);
Run Code Online (Sandbox Code Playgroud)

哪些输出(根据要求)

[a, b, c, d, b1, c1, a1, a2, a3]
Run Code Online (Sandbox Code Playgroud)