File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to combine between two arraylist if has same value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to combine between two arraylist if has same value" Watch "how to combine between two arraylist if has same value" New topic
Author

how to combine between two arraylist if has same value

Mobio Dev
Greenhorn

Joined: Nov 03, 2010
Posts: 6
hi, i have 2 arraylist as below

al1 - [Consignment, Bank, Custodian, Rejected, Bank]
al2 - [[2, 0, 0, 0, 0, 0, 0], [6, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0]]

so my case is i have to check al1 there is any duplicate value, if have have combine the value in al2

so expected result is

al1 - [Consignment, Bank, Custodian, Rejected]
al2 - [[2, 0, 0, 0, 0, 0, 0], [6, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0]]

i'm trying to get solution by googling, anyhow how this can be done

thanks in advance
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Lists should allow duplicates. You should use Set if you want to remove duplicates.

One way of removing the duplicates is to form a Set object from your List and converting it back to List like below...

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9940
    
    6

maintaining data in two parallel arrays is a nightmare. You'd do better by making a single object that can store both the String and the array associated with it. Then you could possibly store them in a map, using the String as the key. First check to see if it is already in the collection, and if so, 'combine' your arrays. If not, simply add it in.


Never ascribe to malice that which can be adequately explained by stupidity.
Mobio Dev
Greenhorn

Joined: Nov 03, 2010
Posts: 6
i found solution as below

for(int i=0;i<al1.size();i++){
for(int j = al1.size()-1; j > i; j--) {
if(al1.get(i).equals(al1.get(j))){
ArrayList temp1 = (ArrayList)al4.get(i);
ArrayList temp2 = (ArrayList)al4.get(j);
for(int k=0;k<temp1.size();k++){
if(!temp2.get(k).equals("0")){
temp1.set(k, temp2.get(k));
}
}
al1.remove(j);
al4.remove(j);
al4.set(i, temp1);
}
}
}
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to combine between two arraylist if has same value
 
Similar Threads
clone() again
problem with collections
Eliminating Duplicate in Arraylist and moving the dups to another arraylist.
request attribute set in the jsp cannot be retrieved in the servlet
Remove duplicates in arraylist