This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
ArrayList myList ; Set set; ArrayList uniqueList; myList = new ArrayList(); myList.add((String)childHash.get("ADMD_TO_SRVR_IP_ADRS")); //in this list i have duplicate 'ADMD_TO_SRVR_IP_ADRS' set = new HashSet(); set.addAll(myList); set.iterator(); uniqueList = new ArrayList(set); System.out.println("uniqueList "+uniqueList); System.out.println("uniqueList "+uniqueList.size());
Now again it is showing me duplicate 'ADMD_TO_SRVR_IP_ADRS'
Please help me...
Thanks in advance vivek S.
author and iconoclast
In the code you're showing me here, myList contains only one item -- the String returned from that "get" call. So the set, and ultimately uniqueList, will also contain only one item.
In any event, if ArrayList contains Strings, then the procedure I've shown you will indeed produce a list of the unique Strings. If it contains some other kind of object, then it will work as long as that class has proper equals() and hashCode() methods that correctly implement your notion of equality for that class.
Note that the iterator() method returns an Iterator over the Set; it's one way to look at the contents of the Set. If you're not going to use the Iterator, there's no need to call iterator()!
I told you the solution. If it's not working for you, then you'll need to give us more details about how you're applying that solution. For example, is io_address a class? If so, does it define a proper equals() and hashCode() method? Or are you just putting Strings in the list? If so, what evidence do you have that there are duplicates in the Set?