在字符串数组中如何删除具有相同字符的字符串?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (58)

我有三个数组,

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

我需要的是,我需要一个不包含这些重复字符串的最终数组。最后的数组应该是:

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

但是,这段代码的问题是,它只是删除了同一字符串的多次出现。还有别的选择吗?

提问于
用户回答回答于

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);
        }
    }
}
用户回答回答于

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

然后,可以将这些字符串添加到一个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));
}

所属标签

可能回答问题的人

  • 西风

    renzha.net · 站长 (已认证)

    7 粉丝1 提问12 回答
  • 四无君

    0 粉丝0 提问3 回答
  • HKC

    红客学院 · 创始人 (已认证)

    26 粉丝7 提问2 回答
  • o o

    2 粉丝490 提问2 回答

扫码关注云+社区

领取腾讯云代金券