删除字符串数组中具有相同字符的字符串

2022-09-02 21:45:36

我现在面临一个问题。在我的一个程序中,我需要从数组中删除具有相同字符的字符串。例如。假设

我有3个数组,比如,

String[] name1 = {"amy", "jose", "jeremy", "alice", "patrick"};
String[] name2 = {"alan", "may", "jeremy", "helen", "alexi"};
String[] name3 = {"adel", "aron", "amy", "james", "yam"};

如您所见,数组中有一个字符串。另外,我有类似 的字符串,在接下来的两个数组中。我需要的是,我需要一个不包含这些重复字符串的最终数组。我只需要一个实例:我需要删除最终数组中名称的所有排列。这是最终的数组应该是:amyname1mayamyyam

String[] finalArray={"amy", "jose", "alice", "patrick","alan", "jeremy", "helen", "alexi","adel", "aron", "james"}

(上面的数组删除了山药,可能,只包括艾米)。

到目前为止,我尝试了什么,使用,如下所示HashSet

String[] name1 = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
String[] name2 = {"Alan", "mAy", "Jeremy", "Helen", "Alexi"};
String[] name3 = {"Adel", "Aaron", "Amy", "James", "Alice"};
Set<String> letter = new HashSet<String>();
for (int i = 0; i < name1.length; i++) {
    letter.add(name1[i]);
}
for (int j = 0; j < name2.length; j++) {
    letter.add(name2[j]);
}
for (int k = 0; k < name3.length; k++) {
    letter.add(name3[k]);
}
System.out.println(letter.size() + " letters must be sent to: " + letter);

但是,此代码的问题在于,它只是删除了同一字符串的多次出现。还有其他选择吗?任何帮助都非常感谢。


答案 1

您可以对 String () 的字符数组进行排序,并从排序的数组中创建新的 String,以获取 String 的“规范”表示形式。str.toCharArray ()

然后,您可以将这些字符串添加到 中,并检查每个字符串的规范表示形式是否已在 Set 中。Set

Set<String> letter = new HashSet<String>();
for (int i = 0; i < name1.length; i++) {
    char[] chars = name1[i].toCharArray();
    Arrays.sort(chars);
    letter.add(new String(chars));
}
for (int j = 0; j < name2.length; j++) {
    char[] chars = name2[j].toCharArray();
    Arrays.sort(chars);
    letter.add(new String(chars));
}
for (int k = 0; k < name3.length; k++) {
    char[] chars = name3[k].toCharArray();
    Arrays.sort(chars);
    letter.add(new String(chars));
}

编辑:我改成了,因为数组不会覆盖和,所以不起作用。Set<char[]>Set<String>hashCodeequalsHashSet<char[]>


答案 2

TreeSet允许我们给出一个比较器。看看这是否有帮助。要保持计数,请使用树状图

package empty;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class RemoveDuplicateStrings {

    public static void main(String[] args) {
        String[] name1 = { "amy", "jose", "jeremy", "alice", "patrick" };
        String[] name2 = { "alan", "may", "jeremy", "helen", "alexi" };
        String[] name3 = { "adel", "aron", "amy", "james", "yam" };

        Comparator<String> comparator = new Comparator<String>() {
            @Override public int compare(String o1, String o2) {
                System.out.println("Compare(" + o1 + "," + o2 + ")");
                char[] a1 = o1.toCharArray();
                Arrays.sort(a1);
                char[] a2 = o2.toCharArray();
                Arrays.sort(a2);
                return new String(a1).compareTo(new String(a2));
            }
        };
        Set<String> set = new TreeSet<String>(comparator);

        for (String name : name1) {
            set.add(name);
        }
        for (String name : name2) {
            set.add(name);
        }
        for (String name : name3) {
            set.add(name);
        }

        String[] result = set.toArray(new String[set.size()]);
        System.out.println(Arrays.asList(result));

        // Using TreeMap to keep the count.

        TreeMap<String, Integer> map = new TreeMap<String, Integer>(comparator);

        addAll(name1, map);
        addAll(name2, map);
        addAll(name3, map);

        System.out.println(map);
    }

    private static void addAll(String[] names, TreeMap<String, Integer> map) {
        for (String name : names) {
            if (map.containsKey(name)) {
                int n = map.get(name);
                map.put(name, n + 1);
            } else
                map.put(name, 1);
        }
    }
}