programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# how to combine between two arraylist if has same value

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
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

Rancher
Posts: 1776
• Number of slices to send:
Optional 'thank-you' note:
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...

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
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.

Mobio Dev
Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
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);
}
}
}