Removing duplicates from an Array of Strings, with

2019-09-25 15:41发布

What would be the best means of going about this without explicit comparison?

2条回答
霸刀☆藐视天下
2楼-- · 2019-09-25 16:42

- 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
查看更多
爷的心禁止访问
3楼-- · 2019-09-25 16:45

If List Of Object contains Strings , You need to Add them to Set.

If the list contains Custom object , override equals meth in the custom class and those object to Set

查看更多
登录 后发表回答