aspose file tools*
The moose likes Java in General and the fly likes Compare two arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compare two arraylist" Watch "Compare two arraylist" New topic
Author

Compare two arraylist

vicky baba
Greenhorn

Joined: Mar 04, 2004
Posts: 13
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
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
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

A Map isn't right either; you want to use a Set.

ArrayList myList = ...
Set set = new HashSet();
set.addAll(myList);

Now set.iterator() will give you a list of the unique elements, of you can say

ArrayList uniqueList = new ArrayList(set);

to get an ArrayList of them.


[Jess in Action][AskingGoodQuestions]
vicky baba
Greenhorn

Joined: Mar 04, 2004
Posts: 13
No sir , I am showing u my code

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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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
Another example of our previous talk (Ernest). Sometimes the API is completely ignored, and I am asking why?

--
:alex |.::the_mindstorm::.


blog - InfoQ.com
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Ali Pope:
Sometimes the API is completely ignored, and I am asking why?
Because it's "urgent."
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by David Harkness:
Because it's "urgent."


Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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:

Layne


Java API Documentation
The Java Tutorial
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Probably it should be bundled inside the distro, maybe this will at least make you ask why the download is so large.

--
:alex |.::the_mindstorm::.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
i'm already asking why the download is so large.
vicky baba
Greenhorn

Joined: Mar 04, 2004
Posts: 13
so what is solution for that i want list of unique io_address in arraylist
pleasse help

Thanks in advance
Vivek s.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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?
vicky baba
Greenhorn

Joined: Mar 04, 2004
Posts: 13
//my code
String apl_code_var = null;
String admd_to_srvr_ip_adrs_var = null;
String project_code_var = null;
if (request.getAttribute("server_ip") != null){
iCount = 0;
server_ip = (java.util.Hashtable)request.getAttribute("server_ip");

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'

//-->
</SCRIPT>

<%
iCount++;
childHash.clear();
childHash = null;
}

}

enumHash = null;
server_ip.clear();
server_ip = null;
}

please help

Vivek s.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare two arraylist
 
Similar Threads
Generics - Chapter 7 Question 8 page 653
Error in HTML escaping
passing object from one jsp to another using session
can we add primitive variables in ArrayList or just objects?
Collections.copy method for lists ? how it works