java - Get value from HashMap<String, ArrayList> -
java - Get value from HashMap<String, ArrayList<String>> -
[updated code] (sorry guys, didn't provide whole code, because in experience big codes seem "scare off" possible helpers.)
for expandablelistview want build hashmap<string, arraylist<string>> string name of category , arraylist<string> names of animals belonging category. populate hashmap such:
hashmap<string, arraylist<string>> map_groups_childs; arraylist<string> list_group_titles; private void preparelistdata(arraylist<id_triv_cat> search_results) { list_group_titles = new arraylist<string>(); // list of grouping titles map_groups_childs = new hashmap<string, arraylist<string>>(); // map. each grouping title gets list of respective childs // temporary list of items category/group arraylist<string> temp_childs_list = new arraylist<string>(); // "search_results" arraylist of self defined objects each containing id, name , category name int count = search_results.size(); int i_cat = 0; int i=0; // if category "i" same next category "i+1", add together kid list (i=0; i<count-1; i++) { // build grouping category name list_group_titles.add(search_results.get(i).get_type()); // while category not change, add together kid temporary childs-array while (i<=count && search_results.get(i).get_type().equals(search_results.get(i+1).get_type())) { temp_childs_list.add(search_results.get(i).get_name()); i++; } // must done, while loop not lastly "i" of every category temp_childs_list.add(search_results.get(i).get_name()); log.i("debug", temp_childs_list.size()); // --> returns more 0 log.i("debug", temp_childs_list.tostring()); // --> returns [word1, word2, word3, ...] log.i("debug", list_group_titles.get(i_cat)); // --> returns single word "insekten" // add together [group_title:list_of_childs] map map_groups_childs.put(list_group_titles.get(i_cat++), temp_childs_list); // clear temp_list, otherwise former category's species added new category temp_childs_list.clear(); } log.i("debug", map_groups_childs.containskey("insekten")); // --> returns true log.i("debug", map_groups_childs.size()); // --> returns 10 log.i("debug", map_groups_childs.get("insekten").size()); // --> returns 0 log.i("debug", map_groups_childs.tostring()); // --> returns {insekten=[], reptilien=[], ...} } the utilize of same i in for- , while-loop may seem wrong or confusing, okay. no i skipped in way or used twice.
all keys set in hashmap there, arraylist want (for example) map_groups_childs.get("insekten") empty. doing wrong?
... map_groups_childs.put(..., temp_childs_list); temp_childs_list.clear(); }
objects passed reference in java. putting same list in map , clearing after every iteration. every value in map points same list empty.
what need this:
for( ... ) { list<string> tempchildslist = new arraylist<>(); ... mapgroupchilds.put(..., tempchildslist); } thus new list created on every iteration.
i agree @candiedorange code mess , overly complex. in general point of abstractions list , map not access things counting numerical indexes time.
note in java, convention identifiers variables camelcase, not under_scored.
java hashmap
Comments
Post a Comment