英文:
Deleting an entry from Arraylist using java stream which is used as value in hashmap
问题
以下是翻译好的部分:
Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23", "45", "54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23", "62", "54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23", "45", "64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37", "45", "54")));
// 我想从键 "2" 中删除 "62"。
// 预期输出
HOSTING={1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
我知道如何做到这一点,但我想知道如何使用 Java 流来实现。
Collection<List<String>> result = HOSTING.entrySet().stream()
.filter(e -> "2".equals(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toCollection(ArrayList::new));
这部分代码已经按照您的要求进行了翻译。如果您需要进一步的帮助或有其他问题,请随时提出。
英文:
Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23","45","54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23","62","54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23","45","64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37","45","54")));
I want to delete "62" from key "2".
Expected output
HOSTING={1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
I know how to do this but i want to know how to do it using java stream.
Collection<List<String>> result = HOSTING.entrySet().stream()
.filter(e -> "2".equals(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toCollection(ArrayList::new));
This will just give me arraylist if that key is present in hashmap. I also thought of using nested stream and predicates.
答案1
得分: 1
你可以这样做。
Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23","45","54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23","62","54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23","45","64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37","45","54")));
System.out.println(HOSTING);
HOSTING.get("2").remove("62");
System.out.println(HOSTING);
输出
{1=[23, 45, 54], 2=[23, 62, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
或者使用流。注意:这只是一个使用映射访问适当条目并更改列表中适当值的折中方法。remove
方法返回一个布尔值,指示是否删除了该值,因此在这两种情况下,三元运算符返回 entry
。
HOSTING = HOSTING.entrySet().stream()
.map(e -> (e.getKey().equals("2")
&& e.getValue().remove("62")) ? e : e)
.collect(Collectors.toMap(Entry::getKey,
Entry::getValue));
System.out.println(HOSTING);
输出
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
但我不建议使用流来做这个。
英文:
You can do it like this.
Map<String, List<String>> HOSTING = new HashMap<String, List<String>>();
HOSTING.put("1", new ArrayList<String>(Arrays.asList("23","45","54")));
HOSTING.put("2", new ArrayList<String>(Arrays.asList("23","62","54")));
HOSTING.put("3", new ArrayList<String>(Arrays.asList("23","45","64")));
HOSTING.put("4", new ArrayList<String>(Arrays.asList("37","45","54")));
System.out.println(HOSTING);
HOSTING.get("2").remove("62");
System.out.println(HOSTING);
Prints
{1=[23, 45, 54], 2=[23, 62, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
Or with a stream. Note: This is a kludge that simply uses a map to access the appropriate entry and alter the appropriate value in the list. The remove method returns a boolean as to whether the value was removed so in both cases the ternary returns the entry
.
HOSTING = HOSTING.entrySet().stream()
.map(e -> (e.getKey().equals("2")
&& e.getValue().remove("62")) ? e : e)
.collect(Collectors.toMap(Entry::getKey,
Entry::getValue));
System.out.println(HOSTING);
Prints
{1=[23, 45, 54], 2=[23, 54], 3=[23, 45, 64], 4=[37, 45, 54]}
But I do not recommend using streams to do this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论