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

时间:2015-06-23 06:40:25

标签: java arrays string duplicate-removal

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

我有3个数组,

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

如您所见,amy数组中有一个字符串name1。另外,我在接下来的两个数组中有mayamyyam等字符串。我需要的是,我需要一个不包含这些重复字符串的最终数组。我只需要出现一次:我需要删除最终数组中名称的所有排列。那是最后一个数组应该是:

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

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

到目前为止,我使用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);

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

4 个答案:

答案 0 :(得分:10)

您可以对字符串(str.toCharArray ())的字符数组进行排序,并从已排序的数组中创建一个新的字符串,以获得&#34;规范&#34;字符串的表示。

然后你可以将这些字符串添加到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>,因为数组不会覆盖hashCodeequals,因此HashSet<char[]>不会{0}工作。

答案 1 :(得分:8)

TreeSet允许我们给出比较器。看看这是否有帮助。为了保持计数,请使用TreeMap

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);
        }
    }
}

答案 2 :(得分:2)

符合kdm:

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

public class RemoveDuplicateString {

    private static boolean add(Set<String> keySet, String s){
        char[] sortCharacters = s.toCharArray();
        Arrays.sort(sortCharacters);
        return keySet.add(new String(sortCharacters));
    }

    private static void check(Set<String> keySet, String []names, List<String> result){
        for (String name : names) {
            if (add(keySet, name)){
                result.add(name);
            }
        }
    }

    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"};
        Set<String> keySet = new HashSet<String>();
        List<String> result = new ArrayList<String>();
        check(keySet, name1, result);
        check(keySet, name2, result);
        check(keySet, name3, result);
        System.out.println(result);
    }
}

答案 3 :(得分:1)

另一种Java 8解决方案。

1)创建一个标准化形式的Map<String, List<String>,然后创建所有看到的不同形式

public static Map<String, List<String>> groupNormalised(final String[]... input) {
    return Arrays.stream(input)
            .flatMap(Arrays::stream)
            .collect(Collectors.groupingBy(s -> {
                char[] c = s.toCharArray();
                Arrays.sort(c);
                return new String(c);
            }));
}

示例:

Map<String, List<String>> grouped = groupNormalised(name1, name2, name3);        
grouped.forEach((k, v) -> System.out.printf("%s appears as %s%n", k, v));

输出:

eejmry appears as [jeremy, jeremy]
aceil appears as [alice]
eehln appears as [helen]
ejos appears as [jose]
adel appears as [adel]
aeilx appears as [alexi]
acikprt appears as [patrick]
aejms appears as [james]
amy appears as [amy, may, amy, yam]
anor appears as [aron]
aaln appears as [alan]

2)处理Map以提取您想要的数据

现在您可以选择,您可以创建标准化表单的Set

final Set<String> normalisedForms = grouped.keySet();

或者您可以创建第一次遇到的Set

final Set<String> first = grouped.values().stream()
        .map(c -> c.iterator().next())
        .collect(toSet());

或作为数组:

final String[] first = grouped.values().stream()
        .map(c -> c.iterator().next())
        .toArray(String[]::new);