有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从HashMap<String,ArrayList<String>>

[更新代码](抱歉,伙计们,我没有提供全部代码,因为根据我的经验,大型代码似乎“吓跑”了可能的帮助者。)

对于ExpandableListView,我想构建一个HashMap<String, ArrayList<String>>,其中String是一个类别的名称ArrayList<String>是属于该类别的动物的名称。我按如下方式填充HashMap:

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>();                  // this is a list of group titles
    map_groups_childs = new HashMap<String, ArrayList<String>>(); // this is a map. each group title gets a list of its respective childs

    // temporary List of items for a certain category/group
    ArrayList<String> temp_childs_list = new ArrayList<String>();

    // "search_results" is an ArrayList of self defined objects each containing an ID, a name and a category name
    int count = search_results.size();
    int i_cat = 0;
    int i=0;

    // if category "i" is the same as the next category "i+1", add child to list
    for (i=0; i<count-1; i++) {
        // build group with category name
        list_group_titles.add(search_results.get(i).get_type());

        // while category does not change, add child to the 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 be done, as the while loop does not get to the last "i" of every category
        temp_childs_list.add(search_results.get(i).get_name());

        Log.i("DEBUG", temp_childs_list.size());      // --> returns always more than 0
        Log.i("DEBUG", temp_childs_list.toString());  // --> returns always something like [word1, word2, word3, ...]
        Log.i("DEBUG", list_group_titles.get(i_cat)); // --> returns always a single word like "Insekten"

        // add [group_title:list_of_childs] to map
        map_groups_childs.put(list_group_titles.get(i_cat++), temp_childs_list);

        // clear temp_list, otherwise former category's species will be added to 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=[], ...}
}

在for-and-while循环中使用相同的i可能看起来是错误的或令人困惑的,但这是可以的。不以任何方式跳过i或使用两次

我放在HashMap中的所有键都在那里,但是我想要获得的ArrayList(例如)map_groups_childs.get("Insekten")是空的。我做错了什么


共 (3) 个答案

  1. # 1 楼答案

    即使进行了编辑,您的代码仍然缺少关于正在发生的事情的大量线索。我做了一些猜测,如果我是对的,你这样做太难了

    我推断了一个类Id_triv_cat的存在,它的getter名为get_name()get_type()

    static class Id_triv_cat {
    
        String type;
        String name;
    
        Id_triv_cat( String type, String name )
        {
            this.name = name;
            this.type = type;
        }
    
        public String get_type()
        {
            return type;
        }
        public String get_name()
        {
            return name;
        }
    }
    

    我还编写了这段代码来测试prepareListData()方法

    public static void main(String[] args){
        ArrayList<Id_triv_cat> search_results = new ArrayList<Id_triv_cat>();
        search_results.add( new Id_triv_cat("type1", "name1") );
        search_results.add( new Id_triv_cat("type2", "name2") );
        search_results.add( new Id_triv_cat("Insekten", "insekten name1") );
        search_results.add( new Id_triv_cat("Insekten", "insekten name2") );
        search_results.add( new Id_triv_cat("type3", "name3") );
        new Test().myPrepareListData( search_results );
    }
    

    虽然我可以像一个优秀的SE居民一样修复你的小缺陷,但我还是要冒险将所有这些迁移到程序员身上,因为你最大的问题是设计问题。您的代码缺少清晰的本地标识符。你不是在做本地人,而是在用java实用程序类举办一场点滴狂欢节,这让你的代码毫无意义地难以理解

    如果,正如我所怀疑的,您正试图从listId_triv_cat中使用其类型作为键、名称作为值来构建映射,那么请尝试以下操作:

    private void myPrepareListData(ArrayList<Id_triv_cat> search_results) {
        // Each group title is mapped to a list of its respective children
        HashMap<String, ArrayList<String>> my_map_groups_childs = 
            new HashMap<String, ArrayList<String>>(); 
    
        for (Id_triv_cat search_result : search_results)
        {
            String type = search_result.get_type();
            String name = search_result.get_name();
    
            if ( my_map_groups_childs.containsKey(type) )
            {
                ArrayList<String> names = my_map_groups_childs.get(type);
                names.add(name);
            }
            else
            {
                ArrayList<String> names = new ArrayList<String>();
                names.add(name);
                my_map_groups_childs.put(type, names);
            }
        }
    
        Log.i("DEBUG", "my_map_groups_childs = " + my_map_groups_childs);
    }
    

    这将显示:my_map_groups_childs = {Insekten=[insekten name1, insekten name2], type1=[name1], type3=[name3], type2=[name2]}

    看看几个精心挑选的当地人怎么能让生活变得如此轻松

    如果这不是你想要的,你就必须让你的问题更清楚

    Radiodef是对的。在用Java编写代码时,确实应该使用camelCase

  2. # 2 楼答案

        ...
    
        map_groups_childs.put(..., temp_childs_list);
    
        temp_childs_list.clear();
    }
    

    对象在Java中作为引用传递。你总是把相同的列表放到地图上,并在每次迭代后清除它。因此,映射中的每个值都指向同一个空列表

    你可能需要这样的东西:

    for( ... ) {
        List<String> tempChildsList = new ArrayList<>();
    
        ...
    
        mapGroupChilds.put(..., tempChildsList);
    }
    

    因此,每次迭代都会创建一个新列表

    我也同意@CandiedOrange,你的代码很混乱,可能过于复杂。一般来说,像List和Map这样的抽象概念的要点是不通过一直计算数字索引来访问对象

    请注意,在Java中,惯例是变量的标识符是camelCase,而不是不得分的

  3. # 3 楼答案

    我几乎能告诉你你需要的是下面第二组中的5行

        HashMap<String, ArrayList<String>> map_groups_childs = new HashMap<String, ArrayList<String>>();
        ArrayList<String> list_group_titles = new ArrayList<String>();
        ArrayList<String> temp_childs_list  = new ArrayList<String>();
    
        list_group_titles.add("insects");
        temp_childs_list.add("Swallowtail");
        temp_childs_list.add("Small White");
        temp_childs_list.add("Large White");
        temp_childs_list.add("Silverfish");
    
        int k = 0; 
    
        Log.i("DEBUG", list_group_titles.get(k)); //  > returns "insects"
        Log.i("DEBUG", temp_childs_list);         //  > returns [Swallowtail, Small White, Large White, Silverfish]
    
        map_groups_childs.put(list_group_titles.get(k), temp_childs_list);
    
        Log.i("DEBUG", map_groups_childs.size());                 //  > returns 1 not 10
        Log.i("DEBUG", map_groups_childs.containsKey("insects")); //  > returns true
        Log.i("DEBUG", map_groups_childs.get("insects").size());  //  > returns 4 not 0