当值在 ArrayList 中时,如何继续向已存在的键添加值?输入来自扫描仪以创建 TreeMap
How to keep adding values to a key that already exists, when the values are in an ArrayList? Input is coming in from a scanner to create a TreeMap
我正在尝试接收来自用户的输入,其中每行必须包含一些文本(一个键),后跟一个制表符,然后是一个 double
文字(一个值),然后是一个新队。
如果允许用户继续输入相同的键,然后是 /t
,然后是不同的值和 /n
,我该如何编写一个程序来不断将值添加到相同的值树图中的关键?
每个键都有一个 ArrayList
,这是我卡住的地方,因为我不知道如何为不同的 lines/keys.
添加到数组列表
这是我目前拥有的:
TreeMap<String, ArrayList<Double>> categoryMap = new TreeMap<>();
Double val = 0.0;
String inputKey = "";
System.out.println("Welcome, please enter text");
Scanner scn = new Scanner(System.in);
dataSource = scn;
try
{
// adds all words of input to a string array
while (dataSource.hasNextLine())
{
ArrayList<Double> valueMap = new ArrayList<>();
inputKey = dataSource.next();
val = dataSource.nextDouble();
valueMap.add(val);
if (categoryMap.get(inputKey) == null)
{
categoryMap.put(inputKey, valueMap);
}
else
{
categoryMap.put(inputKey, valueMap);
}
dataSource.nextLine();
}
}
// Exception if no lines detected and shows message
catch (IllegalArgumentException lineExcpt)
{
System.out.println("No lines have been input: " + lineExcpt.getMessage());
}
finally
{
scn.close();
}
return categoryMap;
我对 java 非常陌生,只有大约一个月的经验。
您应该从地图中获取该键的数组列表并在其中添加值,例如 categoryMap.get (inputKey).add(val)
在您的 else 中,代码可以改进,但我现在使用的是 phome...
这是您的 while
循环中的逻辑,需要进行一些修改。目前,您每次都用一个新值覆盖值列表。
这是您在纸上的内容:
- 如果该键不存在,则使用给定的
double
创建一个新列表并将其用作值。
- 否则,获取(已经存在的)列表并向其中添加
double
。
在代码中,我们只需要修改你所做的:
String inputKey = dataSource.next();
double val = dataSource.nextDouble();
List<Double> list = categoryMap.get(inputKey);
if (list == null) // If the key does not exist
{
list = new ArrayList<>(); // create a new list
list.add(val); // with the given double
categoryMap.put(inputKey, list); // and use it as the value
}
else // Else
{
list.add(val) // (got the list already) add the double to it
}
如果你使用 Java 8,地图有 computeIfAbsent
方法。
List<Double> addTo = map.computeIfAbsent(key, ArrayList::new);
我正在尝试接收来自用户的输入,其中每行必须包含一些文本(一个键),后跟一个制表符,然后是一个 double
文字(一个值),然后是一个新队。
如果允许用户继续输入相同的键,然后是 /t
,然后是不同的值和 /n
,我该如何编写一个程序来不断将值添加到相同的值树图中的关键?
每个键都有一个 ArrayList
,这是我卡住的地方,因为我不知道如何为不同的 lines/keys.
这是我目前拥有的:
TreeMap<String, ArrayList<Double>> categoryMap = new TreeMap<>();
Double val = 0.0;
String inputKey = "";
System.out.println("Welcome, please enter text");
Scanner scn = new Scanner(System.in);
dataSource = scn;
try
{
// adds all words of input to a string array
while (dataSource.hasNextLine())
{
ArrayList<Double> valueMap = new ArrayList<>();
inputKey = dataSource.next();
val = dataSource.nextDouble();
valueMap.add(val);
if (categoryMap.get(inputKey) == null)
{
categoryMap.put(inputKey, valueMap);
}
else
{
categoryMap.put(inputKey, valueMap);
}
dataSource.nextLine();
}
}
// Exception if no lines detected and shows message
catch (IllegalArgumentException lineExcpt)
{
System.out.println("No lines have been input: " + lineExcpt.getMessage());
}
finally
{
scn.close();
}
return categoryMap;
我对 java 非常陌生,只有大约一个月的经验。
您应该从地图中获取该键的数组列表并在其中添加值,例如 categoryMap.get (inputKey).add(val)
在您的 else 中,代码可以改进,但我现在使用的是 phome...
这是您的 while
循环中的逻辑,需要进行一些修改。目前,您每次都用一个新值覆盖值列表。
这是您在纸上的内容:
- 如果该键不存在,则使用给定的
double
创建一个新列表并将其用作值。 - 否则,获取(已经存在的)列表并向其中添加
double
。
在代码中,我们只需要修改你所做的:
String inputKey = dataSource.next();
double val = dataSource.nextDouble();
List<Double> list = categoryMap.get(inputKey);
if (list == null) // If the key does not exist
{
list = new ArrayList<>(); // create a new list
list.add(val); // with the given double
categoryMap.put(inputKey, list); // and use it as the value
}
else // Else
{
list.add(val) // (got the list already) add the double to it
}
如果你使用 Java 8,地图有 computeIfAbsent
方法。
List<Double> addTo = map.computeIfAbsent(key, ArrayList::new);