java - How to compare values in different collections that need different type of iteration loops? -
java - How to compare values in different collections that need different type of iteration loops? -
lets have iterator contains values need compare values located in separate list.
iterator<map.entry<string, object>> = aobj.items(); while (it.hasnext()) { map.entry<string, object> item = it.next(); namevalue = item.getnamevalue(); keyvalue = item.getkeyvalue(); system.out.println("name: " + namevalue); system.out.println("value: " + keyvalue); }
this outputs:
name: header value: 22222
lets have separate list (in want compare above values with):
list<items> items = new arraylist<>(); (item item : items) { itemnamevalue = item.getname(); itemkeyvalue = item.getkey(); system.out.println("name: " + itemnamevalue); system.out.println("value: " + itemkeyvalue); }
this outputs:
name: header value: 44444
since these different types of loops (one while loop , other 1 each loop) how can compare example:
if (namevalue.equals(itemnamevalue())) { // something? }
i need iterate on both collections / info structures @ same time...
would solution?
string namevalue = ""; object keyvalue = ""; string itemnamevalue = ""; string itemkeyvalue = ""; iterator<map.entry<string, object>> = aobj.items(); while (it.hasnext()) { map.entry<string, object> item = it.next(); namevalue = item.getnamevalue(); keyvalue = item.getkeyvalue(); (item item : items) { itemnamevalue = item.getname(); itemkeyvalue = item.getkey(); } if (namevalue.equals(itemnamevalue())) { // something? } }
basically, trying inquire (in simplified way this):
(1) collection needs iterated in while loop test input (sample data)
(2) array list sec collection list of info returned database phone call (dao) , placed arraylist.
i trying verify if input iterator within while loop same values arraylist (which came database). since these different info structures requiring different looping mechanisms. how iterate through both info structures @ same time , compare them? sec info construction (the array list) actual set of values correct.
i don't know if there's guarantee each iteration comparing same items if utilize nested loop?
thank taking time read this...
the problem facing direct result of bad application design. underline incorrect assumption of question map , list hold objects in same sequence.
list --> info construction ordered not sorted map --> info construction neither ordered nor sortedthis not these 2 info structures don't work together. however, using them store same list should result awkward programme design.
even though reply question, can utilize below code accomplish this:
iterator<map.entry<string, object>> = aobj.items(); list<items> items = dbcall.getitems(); // list of items db int index = 0; while (it.hasnext()) { map.entry<string, object> itemfrommap = it.next(); item itemfromlist = items.get(index); if(itemfrommap.getnamevalue().equals(itemfromlist.getname()) && itemfrommap.getkeyvalue().equals(itemfromlist.getkey())){ // if prefer single .equals() method on &&, can implement comparator<item> homecoming false; } index++; } homecoming true;
java arraylist iterator
Comments
Post a Comment