This works fine only if you add the list to the set, duplicate items between the list and the set are added only once.
Say, for example, ArrayList arr has elements 2,3,4, and the HashSet now has elements 2,5,7, if you do set.addAll (arr), and then the set still includes 2,5,7,3, 4.
Also imagine where you have installed ArrayList arr and HashSet, where T is a common class containing several parameters, then the common elements in the final set will be deleted by the method of the overridden equals method in the class T, and the element added to set will be saved in the final set over an element in an arraylist.
source share