【问题标题】:Pull entry from List<String> nested in HashMap从嵌套在 HashMap 中的 List<String> 中提取条目
【发布时间】:2014-02-22 16:22:12
【问题描述】:

我是 Java 新手,请多多包涵。

我有一个列表的 HashMap。

static Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();

我正在遍历 HashMap 的每个条目并使用以下方法显示它:

for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }

现在,entry.getValue() 是我的列表的存储位置,但我只需要这些列表的第一个、第二个和第三个条目。我相信我需要在迭代之前将列表分配给它自己的对象,但我似乎无法从 HashMap 中提取列表。

输出必须显示 HashMap 的每个条目,包括它的键,但只显示列表的前 3 项。

【问题讨论】:

    标签: java list hashmap extraction


    【解决方案1】:

    只需打印subList 而不修改您的数据:

    System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, 3));
    

    假设,您的列表有超过 2 个元素,否则您可以使用 Math.min(3, entry.getValue().size())(如 cmets 中所述)来避免 IndexOutOfBoundsException

    因此,

    System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, Math.min(3, entry.getValue().size())));
    

    【讨论】:

    • 这个答案比我的要简洁优雅得多(显然我考虑得不够多)。虽然你想用Math.min(3, entry.getValue().size()) 做子列表,以防它更短。
    • 非常好的答案,继续加油!
    • @Gorkk 谢谢我根据 OP 的要求做了一个假设,但你的建议会使解决方案准确
    【解决方案2】:

    你可以这样做。我已经详细说明了,以帮助您更好地理解。这段代码可以缩短

       Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();
        for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
            Integer integer = entry.getKey();
            List<String> list =  entry.getValue();
            //first item
            String first = list.get(0);
            //second item  
             String second = list.get(1);
            //third item
            String third = list.get(2);
        }
    

    希望对你有帮助!

    【讨论】:

    • 如果列表 i 短于 3,这将产生异常
    • 我已经提供了答案,因为 OP 提到其中包含三个项目
    • 谢谢,这正是我想要的。它为我提供了对象分配和输出所需的粒度类型。
    • @zibi 构造函数要求列表至少有3个项目。
    【解决方案3】:

    下面的代码应该会给你你想要的。

    for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
        List<String> values = entry.getValue();
        StringBuilder builder = new StringBuilder(entry.getKey()).append(" - ");
        // here I assume the values list is never null, and we pick at most the first 3 entries
        builder.append("[")
        for (int i = 0; i < Math.min(3, values.size()); i++) {
            if (i > 0) builder.append(", ");
            builder.append(values.get(i));
        }
        builder.append("[");
        System.out.println(builder.toString());
    }
    

    它的作用是,对于地图中的每个条目:

    1. 使用值创建一个局部变量 (a List&lt;String&gt;)
    2. 创建一个StringBuilder 用于在条目上构建输出
    3. 在构建输出时迭代前 3 个项目(如果列表较短,则迭代更少)
    4. 在builder中输出字符串

    如果您考虑一下,肯定有一种更优雅的方法,这只是您问题的快速基本解决方案。

    【讨论】:

      【解决方案4】:

      这样的事情怎么样:

      String result = "";
      List<String> strings = entry.getValue();
      for (int i = 0; i < strings.size(); i++)
      {
          result += strings.get(i);
      }
      

      【讨论】:

        【解决方案5】:

        我的朋友,你可以这样做

         Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();
                    for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
                        List<String> list =  entry.getValue();
                        String firstItem = list.get(0);
                        String secondItem = list.get(1);
                        String thirdItem = list.get(2);
        
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-27
          • 2016-03-10
          • 2020-03-03
          • 1970-01-01
          • 1970-01-01
          • 2018-10-20
          • 2019-09-30
          • 1970-01-01
          相关资源
          最近更新 更多