InterviewSolution
| 1. |
How To Remove Duplicate Elements From An Arraylist? |
|
Answer» We can USE a HashSet to do the job of removing the DUPLICATE elements. HashSet only stores UNIQUE elements and we'll use that FEATURE of HashSet to remove duplicates. If you have a LIST called cityList you can create a HashSet using this list - Set<String> citySet = new HashSet<String>(cityList); That will remove all the duplicate elements from the given list. Note that insertion order won't be retained if a HashSet is used. In case insertion order is to be retained use LinkedHashSet. We can use a HashSet to do the job of removing the duplicate elements. HashSet only stores unique elements and we'll use that feature of HashSet to remove duplicates. If you have a List called cityList you can create a HashSet using this list - Set<String> citySet = new HashSet<String>(cityList); That will remove all the duplicate elements from the given list. Note that insertion order won't be retained if a HashSet is used. In case insertion order is to be retained use LinkedHashSet. |
|