如何从数组中获取唯一值

我有一个数组,我想从中删除重复项。

for(int data1=startpos;data1<=lastrow;data1++) { String movie_soundtrk=cells.getCell(data1,Mmovie_sndtrk_cl).getValue().toString(); al.add(movie_soundtrk); } String commaSeparated=al.toString(); String [] items = commaSeparated.split(","); String[] trimmedArray = new String[items.length]; for (int i = 0; i < items.length; i++) { trimmedArray[i] = items[i].trim(); } Set set = new HashSet(); Collections.addAll(set, trimmedArray); System.out.println(set); 

但这并没有给我Array的独特价值。

我的arrays: – {英文,法文,日文,俄文,中文字幕,英文,法文,日文,俄文,中文字幕}

输出: – [日语,俄语,法语,中文字幕],中文字幕,[英语,英语]

你可以在java 7中的一行中完成它:

 String[] unique = new HashSet(Arrays.asList(array)).toArray(new String[0]); 

在Java 8中更简单,更简单:

 String[] unique = Arrays.stream(array).distinct().toArray(String[]::new); 

HashSet将完成这项工作。

你可以试试这个:

 List newList = new ArrayList(new HashSet(oldList)); 

使用Java 8的Stream API,这是一个具有通用Array类型的解决方案:

 public static  T[] makeUnique(T... values) { return Arrays.stream(values).distinct().toArray(new IntFunction() { @Override public T[] apply(int length) { return (T[]) Array.newInstance(values.getClass().getComponentType(), length); } }); } 

它适用于任何Object类型数组,但不适用于原始数组。

对于原始数组,它看起来像这样:

 public static int[] makeUnique(int... values) { return Arrays.stream(values).distinct().toArray(); } 

最后这是一个小unit testing:

 @Test public void testMakeUnique() { assertArrayEquals(new String[] { "a", "b", "c" }, makeUnique("a", "b", "c", "b", "a")); assertArrayEquals(new Object[] { "a", "b", "c" }, makeUnique(new Object[] { "a", "b", "c", "b", "a" })); assertArrayEquals(new Integer[] { 1, 2, 3, 4, 5 }, makeUnique(new Integer[] { 1, 2, 2, 3, 3, 3, 1, 4, 5, 5, 5, 1 })); assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, makeUnique(new int[] { 1, 2, 2, 3, 3, 3, 1, 4, 5, 5, 5, 1 })); } 

您可以获得两组,一组包含所有字幕,另一组包含重复项

 String[] trimmedArray = new String[items.length]; Set subtitles = new HashSet(); Set duplicatedSubtitles = new HashSet(); foreach(String subtitle : trimmedArray){ subtitle = subtitle.trim(); if(subtitles.contains(subtitle)){ duplicatedSubtitles.add(subtitle); } subtitles.add(subtitle); } 

尝试而不是这个

 Set set = new HashSet(); 

打电话给这个

 set.addAll(trimmedArray); 

为什么首先将项添加到数组中然后将其转换为字符串? 只需遍历tha数组并将它们复制到Set.Then打印新创建的包含唯一值的集合。

 Set set = new HashSet(); for (int i = 0; i < al.length; i++) { set.add(al[i]); } for (String str : set) { System.out.println(str); } 

此代码将计算数组中的不同元素,然后查找它们的出现。 并计算百分比并将其保存到hashmap。

 int _occurrence = 0; String[] _fruits = new String[] {"apple","apple","banana","mango","orange","orange","mango","mango","banana","banana","banana","banana","banana"}; List _initialList = Arrays.asList(_fruits); Set treesetList = new TreeSet(_initialList); String[] _distinct = (String[]) treesetList.toArray(new String[0]); HashMap _map = new HashMap(); int _totalElement = _fruits.length; for(int x=0;x<_distinct.length;x++){ for(int i=0;i<_fruits.length;i++){ if(_distinct[x].equals(_fruits[i])){ ++_occurrence; } } double _calPercentage = Math.round((((double)_occurrence/(double)_totalElement)*100)); _map.put(_distinct[x], String.valueOf(_calPercentage+"%")); _occurrence = 0; } System.out.println(_map);