Making third List from two Lists for common names in Java -
i have 2 java lists. first list contains name column. second list contains name , status columns.
first list contains: david; david
second list contains: david, 3; david, 1; david, 2 ;john, 1; john 3;
for each common name 2 lists, should make third list in following 1 row.
name status1 status2 status3
david 1 2 3
john 1 3
how can achieve using java list collection ?
my example: created 3 classes holds 3 lists. //first class
public class firstclass {
private string name;
public firstclass(string name) { super(); this.name = name; }
// setters , getters here }
// second class
public class secondclass {
private string name; private int status; public secondclass(string name, int status) { super(); this.name = name; this.status = status; } // setters , getters here
}
// thirdclass
public class thirdclass {
private string name; private int status1; private int status2; private int status3; public thirdclass(string name, int status1, int status2, int status3) { super(); this.name = name; this.status1 = status1; this.status2 = status2; this.status3 = status3; } public thirdclass() { // todo auto-generated constructor stub } // setters , getters here
}
// execution class
import java.util.*;
public class clientexecute {
public static void main(string[] args) { list<firstclass> a1 = new arraylist<firstclass>(); a1.add(new firstclass("david")); a1.add(new firstclass("john")); a1.add(new firstclass("david")); list<secondclass> a2 = new arraylist<secondclass>(); a2.add(new secondclass ("john", 2)); a2.add(new secondclass ("david", 2)); a2.add(new secondclass ("david", 3)); a2.add(new secondclass ("david", 1)); a2.add(new secondclass ("john", 3)); thirdclass third = null; list<thirdclass> a3 =null; for(firstclass first: a1) { third = new thirdclass(); a3 = new arraylist<thirdclass>(); for( secondclass second: a2) { if(first.getname().equalsignorecase(second.getname())) { third.setname(second.getname()); if(second.getstatus() ==1){ third.setstatus1(second.getstatus()); } else if(second.getstatus() ==2){ third.setstatus2(second.getstatus()); } else if(second.getstatus() ==3){ third.setstatus3(second.getstatus()); } } a3.add(third); } } printthirdlist(a3); } private static void printthirdlist(list<thirdclass> a3) { for(thirdclass list: a3){ system.out.println(list.getname()+list.getstatus1()+list.getstatus2()+list.getstatus3()); } }
}
i think, doing wrong in iterating 2 lists while comparing. getting following wrong output:
david 1 2 3; david 1 2 3; david 1 2 3; david 1 2 3; david 1 2 3; expected output: david 1 2 3; john 1 3;
first, move arraylist of third class outside first loop.
second, remove a3.add second loop , put in first loop since adding same instance every value of second class. causing multiple output.
list<thirdclass> a3 = new arraylist<thirdclass>(); for(firstclass first: a1) { thirdclass third = new thirdclass(); for( secondclass second: a2) { if(first.getname().equalsignorecase(second.getname())) { third.setname(second.getname()); if(second.getstatus() ==1){ third.setstatus1(second.getstatus()); } else if(second.getstatus() ==2){ third.setstatus2(second.getstatus()); } else if(second.getstatus() ==3){ third.setstatus3(second.getstatus()); } } } a3.add(third); }
Comments
Post a Comment