Java 从列表中提取条目<;字符串>;嵌套在HashMap中

Java 从列表中提取条目<;字符串>;嵌套在HashMap中,java,list,hashmap,extraction,Java,List,Hashmap,Extraction,我是Java新手,请耐心等待。 我有一个列表的哈希图 static Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>(); staticmap contactList=newhashmap(); 我正在迭代HashMap的每个条目,并使用以下命令显示它: for (Map.Entry<Integer, List<String>

我是Java新手,请耐心等待。

我有一个列表的哈希图

static Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();
staticmap contactList=newhashmap();
我正在迭代HashMap的每个条目,并使用以下命令显示它:

for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
for(Map.Entry:contactList.entrySet()){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
现在,条目.getValue()是存储列表的位置,但我只需要这些列表的第一、第二和第三个条目。我认为我需要在迭代之前将列表分配给它自己的对象,但是我似乎无法从HashMap中提取列表


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

类似的内容如何:

String result = "";
List<String> strings = entry.getValue();
for (int i = 0; i < strings.size(); i++)
{
    result += strings.get(i);
}
字符串结果=”;
List strings=entry.getValue();
对于(int i=0;i
像这样的东西怎么样:

String result = "";
List<String> strings = entry.getValue();
for (int i = 0; i < strings.size(); i++)
{
    result += strings.get(i);
}
字符串结果=”;
List strings=entry.getValue();
对于(int i=0;i
你可以这样做。我已经详细阐述了这一点,以帮助您更好地理解。此代码可以缩短

   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);
    }
Map contactList=newhashmap();
对于(Map.Entry:contactList.entrySet()){
整数=entry.getKey();
List=entry.getValue();
//第一项
String first=list.get(0);
//第二项
字符串秒=list.get(1);
//第三项
stringthird=list.get(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);
    }
Map contactList=newhashmap();
对于(Map.Entry:contactList.entrySet()){
整数=entry.getKey();
List=entry.getValue();
//第一项
String first=list.get(0);
//第二项
字符串秒=list.get(1);
//第三项
stringthird=list.get(2);
}

希望有帮助

下面的代码应该能满足您的需求

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());
}
for(Map.Entry:contactList.entrySet()){
列表值=entry.getValue();
StringBuilder=新的StringBuilder(entry.getKey()).append(“-”);
//在这里,我假设值列表从不为null,我们最多选择前3个条目
builder.append(“[”)
对于(int i=0;i0)builder.append(“,”);
append(values.get(i));
}
生成器。追加(“[”);
System.out.println(builder.toString());
}
它所做的是,对于地图中的每个条目:

  • 使用值创建一个局部变量(一个
    列表
  • 创建一个
    StringBuilder
    ,用于在条目上构建输出
  • 在生成输出时,迭代前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());
    }
    
    for(Map.Entry:contactList.entrySet()){
    列表值=entry.getValue();
    StringBuilder=新的StringBuilder(entry.getKey()).append(“-”);
    //在这里,我假设值列表从不为null,我们最多选择前3个条目
    builder.append(“[”)
    对于(int i=0;i0)builder.append(“,”);
    append(values.get(i));
    }
    生成器。追加(“[”);
    System.out.println(builder.toString());
    }
    
    它所做的是,对于地图中的每个条目:

  • 使用值创建一个局部变量(一个
    列表
  • 创建一个
    StringBuilder
    ,用于在条目上构建输出
  • 在生成输出时,迭代前3项(如果列表较短,则迭代次数较少)
  • 在生成器中输出字符串
  • 如果您仔细想想,肯定有一种更优雅的方法,这只是解决您问题的一种快速基本解决方案。

    只需在不修改数据的情况下打印:

    System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, 3));
    
    假设,您的列表有两个以上的元素,否则您可以使用
    Math.min(3,entry.getValue().size())
    (如注释中所述)来避免
    索引自动边界异常

    因此,

    System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, Math.min(3, entry.getValue().size())));
    
    只需在不修改数据的情况下打印:

    System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, 3));
    
    假设,您的列表有两个以上的元素,否则您可以使用
    Math.min(3,entry.getValue().size())
    (如注释中所述)来避免
    索引自动边界异常

    因此,

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

    我的朋友,你可以通过

     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);
    
                }
    
    Map contactList=newhashmap();
    对于(Map.Entry:contactList.entrySet()){
    List=entry.getValue();
    String firstItem=list.get(0);
    String secondItem=list.get(1);
    String thirdItem=list.get(2);
    }
    
    我的朋友,你可以通过

     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);
    
                }
    
    Map contactList=newhashmap();
    对于(Map.Entry:contactList.entrySet()){
    List=entry.getValue();
    String firstItem=list.get(0);
    String secondItem=list.get(1);
    String thirdItem=list.get(2);
    }
    
    如果列表i短于3I,这将产生异常。我已经提供了答案,因为OP提到其中有三项。谢谢,这正是我想要的。它为我提供了对象分配和输出所需的粒度类型。@zibi构造函数要求列表至少有3项。这将产生uce例外如果列表i短于3I,我已经提供了答案,因为OP提到了其中的三项谢谢,这正是我想要的。