aspose 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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://aspose.com/file-tools
 
subject: how to combine between two arraylist if has same value