I want to Separate similar value from array In java -
I want to Separate similar value from array In java -
see below code , please allow me know correction. have written expected result.. please help me.
seek { list<string> associds= new arraylist<string>(); list<string> actionids= new arraylist<string>(); associds.add("1"); associds.add("2"); associds.add("3"); associds.add("5"); actionids.add("2"); actionids.add("3"); actionids.add("7"); actionids.add("4"); actionids.add("6"); list<string> matchfromfirstarray= new arraylist<string>(); list<string> notmatchfromfisrtarray= new arraylist<string>(); list<string> notmatchfromsecondarray= new arraylist<string>(); for(int j=0;j<associds.size();j++) { for(int i=0;i<actionids.size();i++) { if(associds.get(j).equalsignorecase(actionids.get(i))) { matchfromfirstarray.add(associds.get(j)); }else { notmatchfromfisrtarray.add(associds.get(j)); notmatchfromsecondarray.add(actionids.get(i)); } } } system.out.println("match first array : "+matchfromfirstarray.tostring()); system.out.println("not match fisrt array : "+notmatchfromfisrtarray.tostring()); system.out.println("unmatch form sec array : "+notmatchfromsecondarray.tostring()); }catch (exception e) { system.out.println("error :"+e); } }
my expected result:
match first array : [2, 3]
not match fisrt array : [1, 5]
unmatch form sec array : [7, 4, 6]
my output:
match first array : [2, 3]
not match fisrt array : [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 5, 5, 5, 5, 5]
unmatch form sec array : [2, 3, 7, 4, 6, 3, 7, 4, 6, 2, 7, 4, 6, 2, 3, 7, 4, 6]
list<string> matchfromfirstarray= new arraylist<string>(); list<string> notmatchfromfisrtarray= new arraylist<string>(); list<string> notmatchfromsecondarray= new arraylist<string>(); matchfromfirstarray.addall(associds); matchfromfirstarray.retainall(actionids); // retains matching elements notmatchfromfisrtarray.addall(associds); notmatchfromfisrtarray.removeall(matchfromfirstarray); // retains not matching elements first array notmatchfromsecondarray.addall(actionids); notmatchfromsecondarray.removeall(matchfromfirstarray); // retains not matching elements sec array
java arrays
Comments
Post a Comment