从字符串数组中删除重复数据,在Java中没有显式比较 [英] Removing duplicates from an Array of Strings, without explicit comparison in Java
本文介绍了从字符串数组中删除重复数据,在Java中没有显式比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果没有明确比较,最好的办法是什么?
解决方案
- 使用集合
命名为设置
,保持唯一性。
- 使用 Array 的
asList()
code>将其转换为 List
,然后使用 addAll()
方法 / strong> 列表
到设置
。
< - 在
设置
中添加列表
将删除重复项< 。 例如:
字符串[] arr = {Vivek,John,Rick};
ArrayList< String> arList = new ArrayList< String>(Arrays.asList(arr));
TreeSet< String> arSet = new TreeSet< String>();
arSet.addAll(arList); //重复删除
What would be the best means of going about this without explicit comparison?
解决方案
- Use Collection
named Set
, which maintains Uniqueness.
- Use asList()
method of Array
to convert it into List
, and then use addAll()
method to add the List
to the Set
.
- Adding List
to the Set
will remove the duplicates.
Eg:
String[] arr = {"Vivek","John","Rick"};
ArrayList<String> arList = new ArrayList<String>(Arrays.asList(arr));
TreeSet<String> arSet = new TreeSet<String>();
arSet.addAll(arList); // duplicates removed
这篇关于从字符串数组中删除重复数据,在Java中没有显式比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文