java - Matching a list of IDs with their corresponding objects -
i've got dictionary<int bossid, string bossname> listofbosses , dictionary<string empname, int bossid> listofemps . of course, match bossid boss , return 'dictionary whoismyboss'.
i have no idea start in java.
as far 2 lists go, we'll assume there going 1 many ratio of bosses emps. , bossids incremental 1-n.
i figure following in illiterate java. . .
dictionary<bossname, empname> whoismyboss; arraylist<integer> anotherlistofbosses; private int boss; (string empname: listofemps) boss = listofemps.get(empname) whoismyboss.put(listofbosses(boss) , empname); anotherlistofbosses.add(boss); } for(int boss: anotherlistofbosses) { listofbosses.remove(boss) } that should leave me list of bosses , employees , a listofbosses no corresponding employees hopefully.
how look? dictionary correct collection use? improvements appreciated.
as kilian foth mentioned, should use map instead of dictionary. additionally in java convention start variable names lowercase , classname uppercase letter.
you said names strings , ids integers.
public class bossexample { // key: boss-id, value: boss name private map<integer, string> mapofbosses = new hashmap<integer, string>(); // key: emp name, value: boss-id private map<string, integer> mapofemps = new hashmap<string, integer>(); // constructor fills maps public bossexample() { // ... } // returns mapping emp name boss name public map<string, string> getemptobossmap() { final map<string, string> emptobossmap = new hashmap<string, string>(); // iterate through mapofemps via entryset giving key , value // @ same time for(map.entry<string, integer> empentry : mapofemps.entryset()) { // put emp name key , retrieved boss name value // result map. emptobossmap.put(empentry.getkey(), mapofbosses.get(empentry.getvalue())); } return emptobossmap; } }
Comments
Post a Comment