I have one array list having duplicate elemnts but now i want it will show me only unique list ..so what to do ?? shall i put this list in another arraylist? how? please with code.. very urgent
K Riaz
Ranch Hand
Joined: Jan 08, 2005
Posts: 375
posted
0
Wrong choice of data structure. You should have used a HashMap, which does not allow duplicates and automatically replaces any items already in the Map.
vicky baba
Greenhorn
Joined: Mar 04, 2004
Posts: 13
posted
0
please send me code haow to put that hashtable value into hashmap so that i cant get duplicate record...urgent..
Thanks in advance
Ernest Friedman-Hill
author and iconoclast
Marshal
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.
Ernest Friedman-Hill
author and iconoclast
Marshal
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()!
Alexandru Popescu
Ranch Hand
Joined: Jul 12, 2004
Posts: 995
posted
0
Another example of our previous talk (Ernest). Sometimes the API is completely ignored, and I am asking why?
Originally posted by David Harkness: Because it's "urgent."
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Originally posted by Ali Pope: Another example of our previous talk (Ernest). Sometimes the API is completely ignored, and I am asking why?
-- :alex |.::the_mindstorm::.
It is said, isn't it? However, I try my best to help people. Usually I make the assumption that people don't know about the documentation that is available and provide a link to it. :roll:
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?
if (server_ip.size() > 0) { enumHash = server_ip.elements(); while (enumHash.hasMoreElements()) { childHash = (java.util.Hashtable)enumHash.nextElement(); project_code_var = (String) childHash.get("PROJECT"); apl_code_var = (String) childHash.get("APL_CODE"); admd_to_srvr_ip_adrs_var = (String)childHash.get("ADMD_TO_SRVR_IP_ADRS"); myList.add((String)childHash.get("ADMD_TO_SRVR_IP_ADRS")); %> <SCRIPT LANGUAGE="JavaScript"> <!-- aplCodeIP[<%=iCount%>] = new getAplCodeIP('<%=project_code_var%>','<%=apl_code_var%>', '<%=admd_to_srvr_ip_adrs_var%>'); //above i want to disply uniqe ' admd_to_srvr_ip_adrs_var'which is duplicate in the list becaze it is depending on project_code_var and apl_code_var,but i want all record(including duplicate 'admd_to_srvr_ip_adrs_var')but i want to show in list only unique list of 'admd_to_srvr_ip_adrs_var'
You should firstly add all the admd_to_srvr_ip_adrs_var to a Set and only than traverse the Set and display.
-- :alex |.::the_mindstorm::.
James Carman
Ranch Hand
Joined: Feb 20, 2001
Posts: 580
posted
0
Originally posted by Kashif Riaz: Wrong choice of data structure. You should have used a HashMap, which does not allow duplicates and automatically replaces any items already in the Map.
Why not use a Set rather than a Map?
James Carman, President<br />Carman Consulting, Inc.