我想删除String
中的重复String[]
值,但我不确定该怎么做。
以下是一个例子:
public static void main(String[] args){
String[] listofWords = new String [5];
listofWords[0] = "BMW";
listofWords[1] = "Audi";
listofWords[2] = "Mercedes";
listofWords[3] = "Audi";
listofWords[4] = "BMW";
for(int i = 0; i<listofWords.length-1; i++){
system.out.println(listofWords[i]);
}
}
我如何删除此array
中的重复项并且只有每个副本中的一个?
答案 0 :(得分:6)
您可以将String[]
转换为Set
:
String [] listofWords = new String [5];
listofWords [0] = "BMW";
listofWords [1] = "Audi";
listofWords [2] = "Mercedes";
listofWords [3] = "Audi";
listofWords [4] = "BMW";
Set<String> set = new HashSet<String>(Arrays.asList(listofWords));
System.out.println(set); //prints [Audi, Mercedes, BMW]
您可以将Set
转换回String[]
,如下所示:
listofWords = set.toArray(new String[set.size()]);
答案 1 :(得分:1)
如果内存不是问题,请转换为一个集合并返回到数组,例如:
Set<String> mySet = new HashSet<String>(Arrays.asList(listofWords));
listofWords = mySet.toArray(new String[mySet.size()]);
如果您有内存限制,那么您应该对数组进行排序:
Arrays.sort(listofWords);
然后删除重复项,如下所示:
public static int removeDuplicates(String[] A) {
int length=A.length;
if(length==0 || length==1) return length;
int i=1;
for(int j=1; j<length; j++){
if(!A[j].equals(A[j-1])){
A[i]=A[j];
i++;
}
}
if(i<length) A[i]=null;
return i;
}
此方法将删除重复项并返回新的数组大小。
例如
public static void main(String[] args) {
String[] listofWords = new String [5];
listofWords[0] = "BMW";
listofWords[1] = "Audi";
listofWords[2] = "Mercedes";
listofWords[3] = "Audi";
listofWords[4] = "BMW";
Arrays.sort(listofWords);
int n=removeDuplicates(listofWords);
for(int i = 0; i<n; i++){
System.out.println(listofWords[i]);
}
}
答案 2 :(得分:1)
我建议使用Java 8流
来解决这个问题String[] result = Arrays.stream(listofWords).distinct().toArray(s -> new String[s]);
这也解决了您在评论中表达的内存问题。它只会创建一个足够大小的数组来存储不同的值并将结果存储在那里。
作为旁注,您可以更轻松地初始化listofWords
String[] listofWords = {"BMW", "Audi", "Mercedes", "Audi", "BMW"};
答案 3 :(得分:0)
如果您不需要保留数组中的外观顺序,就可以直接执行:首先对数组进行排序,然后使用两个指针迭代数组:
Arrays.sort(listofWords);
int i = 0, j = 0;
while (j < listofWords.length) {
// Copy the element from the j-th position to the i-th position.
listofWords[i] = listofWords[j];
// Increment j while the word at position j equals the word at position i.
while (j < listofWords.length && listofWords[j].equals(listofWords[i])) {
++j;
}
// Increment i, to be the next unique element index.
++i;
}
// i currently points to the element after the last non-duplicate element, so
// ideally we would throw away elements [i..listofWords.length)
// However, you can't resize an array in Java.
// This loop just nulls out the remaining elements, since their contents are all
// already in the array.
while (i < listofWords.length) {
listofWords[i++] = null;
}
答案 4 :(得分:0)
您可以尝试以下解决方案:
public static void main(String [] args){
String strArray[] = new String[]{"bmw","audi","bmw","honda","Yamaha","Yamaha","maruti","hyundai","hyundai"};
HashSet setOfStrings= new HashSet<String>();
for(int i=0;i<strArray.length;i++){
boolean result=setOfStrings.add(strArray[i]);
if(!result){
System.out.println("duplicate elements include...."+strArray[i]);
}
}
Object[] newString= new Object[strArray.length];
newString=setOfStrings.toArray();
System.out.println("Array without Duplicates......."+Arrays.toString(newString));
}
答案 5 :(得分:0)
//JAVA-8 - 删除重复项
List li1 = new ArrayList<>(Arrays.asList(10, 22, 10, 20, 11, 22));
System.out.println("Duplicate elements: " + li1);
// Create new list from elements of original list
List li2 = (List) li1.stream().distinct().collect(Collectors.toList());
// ArrayList with duplicates removed
System.out.println("Non-duplicate elements: " + li2);