有以下几种方法可以去重一个字符串数组:
String[] array = {"a", "b", "c", "a", "b"};
Set<String> set = new HashSet<>(Arrays.asList(array));
String[] result = set.toArray(new String[set.size()]);
String[] array = {"a", "b", "c", "a", "b"};
Set<String> set = new LinkedHashSet<>(Arrays.asList(array));
String[] result = set.toArray(new String[set.size()]);
String[] array = {"a", "b", "c", "a", "b"};
String[] result = Arrays.stream(array).distinct().toArray(String[]::new);
String[] array = {"a", "b", "c", "a", "b"};
String[] result = new String[array.length];
int index = 0;
for (String s : array) {
boolean isDuplicate = false;
for (int i = 0; i < index; i++) {
if (s.equals(result[i])) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
result[index++] = s;
}
}
String[] finalResult = Arrays.copyOf(result, index);