gpt4 book ai didi

java - 从迭代中返回值列表

转载 作者:行者123 更新时间:2023-12-02 07:39:24 25 4
gpt4 key购买 nike

当我使用下面的代码迭代 HashMap 列表时,我得到的键和值如下

    System.out.println( "   (" + key + "," + value + ")" )

但我希望我的值返回为

键 1:值 1

键 1:值 2

键 2:值 1

键 2:值 2...依此类推。有人可以帮帮我吗?

    public static void main(String[] args)  {
Map<String, List<String>> conceptMap = new HashMap<String, List<String>>();
Map<String, List<String>> PropertyMap = new HashMap<String, List<String>>();
try{
Scanner scanner = new Scanner(new FileReader("C:/"));

while (scanner.hasNextLine()){
String nextLine = scanner.nextLine();
String [] column = nextLine.split(":");
if (column[0].equals ("Property")){
if (column.length == 4) {
PropertyMap.put(column [1], Arrays.asList(column[2], column[3]));
}
else {
conceptMap.put (column [1], Arrays.asList (column[2], column[3]));
}
}
}
Set<Entry<String, List<String>>> entries =PropertyMap.entrySet();
Iterator<Entry<String, List<String>>> entryIter = entries.iterator();
System.out.println("The map contains the following associations:");
while (entryIter.hasNext()) {
Map.Entry entry = (Map.Entry)entryIter.next();
Object key = entry.getKey(); // Get the key from the entry.
Object value = entry.getValue(); // Get the value.
System.out.println( " (" + key + "," + value + ")" );
}
scanner.close();

}

catch (Exception e) {
e.printStackTrace();
}

最佳答案

替换这个:

 System.out.println( "   (" + key + "," + value + ")" );

for (Object listItem : (List)value) {
System.out.println(key + ":" + listItem);
}

关于java - 从迭代中返回值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com