In Java, how do I convert an array of strings to a array of unique values?
在Java中,如何将字符串数组转换为唯一值数组?
If I have this array of Strings:
如果我有这个字符串数组:
String[] test = {"1","1","1","2"}
And I want to end up with:
我想最终得到:
String[] uq = {"1","2"}
10 个解决方案
#1
2
If you're going with the HashSet
-approach (which seems pretty handy) you should use a LinkedHashSet
instead of a HashSet
if you want to maintain the array's order!
如果你要使用HashSet方法(这看起来非常方便),你应该使用LinkedHashSet而不是HashSet,如果你想维护数组的顺序!
Set<String> temp = new LinkedHashSet<String>( Arrays.asList( array ) );
String[] result = temp.toArray( new String[temp.size()] );
#2
13
Quick but somewhat inefficient way would be:
快速但有些低效的方式是:
Set<String> temp = new HashSet<String>(Arrays.asList(test));
String[] uq = temp.toArray(new String[temp.size()]);
#3
2
An alternative to the HashSet approach would be to:
HashSet方法的替代方法是:
-
Sort the input array
对输入数组进行排序
-
Count the number of non-duplicate values in the sorted array
计算已排序数组中的非重复值的数量
-
Allocate the output array
分配输出数组
-
Iterate over the sorted array, copying the non-duplicate values to it.
迭代排序的数组,将非重复值复制到它。
The HashSet approach is O(N)
on average assuming that 1) you preallocate the HashSet with the right size and 2) the (non-duplicate) values in the input array hash roughly evenly. (But if the value hashing is pathological, the worst case is O(N**2)
!)
HashSet方法平均为O(N),假设1)您使用正确的大小预先分配HashSet,并且2)输入数组中的(非重复)值大致均匀地散列。 (但如果值散列是病态的,最坏的情况是O(N ** 2)!)
The sorting approach is O(NlogN)
on average.
排序方法平均为O(NlogN)。
The HashSet approach takes more memory on average.
HashSet方法平均占用更多内存。
If you are doing this infrequently OR for really large "well behaved" input arrays, the HashSet approach is probably better. Otherwise, it could be a toss-up which approach is better.
如果您不经常这样做或者对于非常大的“表现良好”的输入数组,HashSet方法可能更好。否则,它可能是一种折腾,哪种方法更好。
#4
2
String[] test = {"1","1","1","2"};
java.util.Set result = new java.util.HashSet(java.util.Arrays.asList(test));
System.out.println(result);
#5
2
I tried all the answers on this page and none worked as-is. So, here is how I solved it, inspired by the answers from Taig and akuhn :
我在这个页面上尝试了所有的答案,没有一个按原样运作。所以,这是我如何解决它,灵感来自Taig和akuhn的答案:
import groovy.io.*;
def arr = ["5", "5", "7", "6", "7", "8", "0"]
List<String> uniqueList = new ArrayList<String>(
new LinkedHashSet<String>( arr.asList() ).sort() );
System.out.println( uniqueList )
#6
1
An easy way is to create a set, add each element in the array to it, and then convert the set to an array.
一种简单的方法是创建一个集合,将数组中的每个元素添加到它,然后将该集合转换为数组。
#7
1
List list = Arrays.asList(test);
Set set = new HashSet(list);
String[] uq = set.toArray();
#8
0
here is my solution:
这是我的解决方案:
int[] A = {2, 1, 2, 0, 1};
Arrays.sort(A);
ArrayList<Integer> B = new ArrayList<Integer>();
for (int i = 0; i < A.length; i++) {
if (i == A.length-1) {
B.add(A[i]);
}
else if (A[i] != A[i+1]) {
B.add(A[i]);
}
}
#9
0
String[] getDistinctElementsArray(String[] arr){
StringBuilder distStrings = new StringBuilder();
distStrings.append(arr[0] + " ");
for(int i=1;i<arr.length;i++){
if( arr[i].equals(arr[i-1])){}
else{
distStrings.append(arr[i] + " ");
}
}
return distStrings.toString().split(" ");
}
#10
0
Just found a nicer way in Java 8 :
刚刚在Java 8中找到了一个更好的方法:
Arrays.stream(aList).distinct().toArray(String[]::new)
#1
2
If you're going with the HashSet
-approach (which seems pretty handy) you should use a LinkedHashSet
instead of a HashSet
if you want to maintain the array's order!
如果你要使用HashSet方法(这看起来非常方便),你应该使用LinkedHashSet而不是HashSet,如果你想维护数组的顺序!
Set<String> temp = new LinkedHashSet<String>( Arrays.asList( array ) );
String[] result = temp.toArray( new String[temp.size()] );
#2
13
Quick but somewhat inefficient way would be:
快速但有些低效的方式是:
Set<String> temp = new HashSet<String>(Arrays.asList(test));
String[] uq = temp.toArray(new String[temp.size()]);
#3
2
An alternative to the HashSet approach would be to:
HashSet方法的替代方法是:
-
Sort the input array
对输入数组进行排序
-
Count the number of non-duplicate values in the sorted array
计算已排序数组中的非重复值的数量
-
Allocate the output array
分配输出数组
-
Iterate over the sorted array, copying the non-duplicate values to it.
迭代排序的数组,将非重复值复制到它。
The HashSet approach is O(N)
on average assuming that 1) you preallocate the HashSet with the right size and 2) the (non-duplicate) values in the input array hash roughly evenly. (But if the value hashing is pathological, the worst case is O(N**2)
!)
HashSet方法平均为O(N),假设1)您使用正确的大小预先分配HashSet,并且2)输入数组中的(非重复)值大致均匀地散列。 (但如果值散列是病态的,最坏的情况是O(N ** 2)!)
The sorting approach is O(NlogN)
on average.
排序方法平均为O(NlogN)。
The HashSet approach takes more memory on average.
HashSet方法平均占用更多内存。
If you are doing this infrequently OR for really large "well behaved" input arrays, the HashSet approach is probably better. Otherwise, it could be a toss-up which approach is better.
如果您不经常这样做或者对于非常大的“表现良好”的输入数组,HashSet方法可能更好。否则,它可能是一种折腾,哪种方法更好。
#4
2
String[] test = {"1","1","1","2"};
java.util.Set result = new java.util.HashSet(java.util.Arrays.asList(test));
System.out.println(result);
#5
2
I tried all the answers on this page and none worked as-is. So, here is how I solved it, inspired by the answers from Taig and akuhn :
我在这个页面上尝试了所有的答案,没有一个按原样运作。所以,这是我如何解决它,灵感来自Taig和akuhn的答案:
import groovy.io.*;
def arr = ["5", "5", "7", "6", "7", "8", "0"]
List<String> uniqueList = new ArrayList<String>(
new LinkedHashSet<String>( arr.asList() ).sort() );
System.out.println( uniqueList )
#6
1
An easy way is to create a set, add each element in the array to it, and then convert the set to an array.
一种简单的方法是创建一个集合,将数组中的每个元素添加到它,然后将该集合转换为数组。
#7
1
List list = Arrays.asList(test);
Set set = new HashSet(list);
String[] uq = set.toArray();
#8
0
here is my solution:
这是我的解决方案:
int[] A = {2, 1, 2, 0, 1};
Arrays.sort(A);
ArrayList<Integer> B = new ArrayList<Integer>();
for (int i = 0; i < A.length; i++) {
if (i == A.length-1) {
B.add(A[i]);
}
else if (A[i] != A[i+1]) {
B.add(A[i]);
}
}
#9
0
String[] getDistinctElementsArray(String[] arr){
StringBuilder distStrings = new StringBuilder();
distStrings.append(arr[0] + " ");
for(int i=1;i<arr.length;i++){
if( arr[i].equals(arr[i-1])){}
else{
distStrings.append(arr[i] + " ");
}
}
return distStrings.toString().split(" ");
}
#10
0
Just found a nicer way in Java 8 :
刚刚在Java 8中找到了一个更好的方法:
Arrays.stream(aList).distinct().toArray(String[]::new)