Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to combine between two arraylist if has same value

 
Mobio Dev
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic