简体   繁体   English

如果 Map 则将元素添加到 map <string , stack<integer> &gt; 映射…在 java</string>

[英]add elements to map if Map<String , Stack<Integer>> map… in java

I have a Map collection which maps String to Stack<Integer how can I put my stacks to this map?我有一个Map集合,它将String映射到Stack<Integer我怎样才能将我的堆栈放到这个 map? I've tried this so far but couldn't succeed.到目前为止,我已经尝试过了,但无法成功。

Map<String,Stack<Integer>> map=new HashMap<>();
map.put("abc",new Stack<Integer>().push(123));

You can use computeIfAbsent() method which was added in Java 8:您可以使用 Java 8 中添加的computeIfAbsent()方法:

Map<String, Stack<Integer>> map=new HashMap<>();
map.computeIfAbsent("abc", k -> new Stack<>()).push(123);

Since Stack#push method returns an Integer here.由于Stack#push方法在此处返回Integer You can modify your code this way to achieve it:您可以通过这种方式修改代码来实现它:

Map<String, Stack<Integer>> map=new HashMap<>();
Stack<Integer> stack = new Stack<>();
stack.push(123);
map.put("abc", stack);

Update:更新:

After looking at your comment on my answer.看了你对我的回答的评论。 I think you want to accomplish something like this:我想你想完成这样的事情:

Map<String, Deque<Integer>> map = new HashMap<>();
Deque<Integer> deque = new ArrayDeque<>(); // use Deque instead of Stack
map.put("abc", deque); // putting the Deque in Map
map.get("abc").add(12);
map.get("abc").add(34);
map.get("abc").add(56);
map.get("abc").add(78);

System.out.println("Before removing: " + map);
map.get("abc").remove(); // removing first element
System.out.println("After  removing: " + map);

Output: Output:

Before removing: {abc=[12, 34, 56, 78]}
After  removing: {abc=[34, 56, 78]}

Your implementation is wrong here.您的实现在这里是错误的。 You have to create object first of type Stack<Integer> .您必须首先创建类型为Stack<Integer>的 object 。 As you have specified Stack<Integer> as values you should have to create Object of that type.由于您已将Stack<Integer>指定为values ,因此您应该必须创建该类型的Object Return type of push is of type itself so it cause error in your case. push的返回type本身就是类型,因此在您的情况下会导致错误。

    Map<String,Stack<Integer>> map=new HashMap<>();
    Stack<Integer> stack = new Stack<Integer>();
    stack.push(123);
    map.put("abc",stack);

First you need to fetch the stack, that you want to update, then you need to update the stack with the new element, and finally use the property of map, that replaces the elements, of the map already exists.首先,您需要获取要更新的堆栈,然后需要使用新元素更新堆栈,最后使用 map 的替换元素的 map 的属性。

Stack<Integer> st = map.getOrDefault("abc",new Stack<Integer>());
st.push(123);
map.put("abc",st);

use this:用这个:

BiFunction<Stack<Integer>, Integer, Stack<Integer>> bi = (s, i) -> {
        s.push(i);
        return s;
    };

    map.put("abc", bi.apply(map.get("abc"), 134));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? 转换地图 <Integer, List<Object> &gt;到地图 <Integer, Map<String, Map<LocalDate, Integer> &gt;&gt;使用Java流API - Convert Map<Integer, List<Object>> to Map<Integer, Map<String, Map<LocalDate, Integer>>> using Java stream API 想要向地图Java中添加元素 - Want to add elements into map java Java中具有连续整数键的元素的有效映射 - An efficient Map of elements with continuous integer keys in Java 使用Java合并对地图中的数字求和 <String, Map<String, Integer> &gt; - Using Java merge to sum a number in a Map<String, Map<String, Integer>> Java地图 <String,Map<String,Integer> 用于填充结果 - Java Map<String,Map<String,Integer> use to populate results Java 8 stream Map <string, map<string, integer> &gt; 如果根 map 密钥包含,则返回 map 值</string,> - Java 8 stream Map<String, Map<String, Integer>> return map values if root map key contains 地图 <String, Integer> 和地图 <Integer ,String> 排序 - Map<String, Integer> and Map<Integer ,String> sorting Java List <String>到Map <String,Integer> convertion - Java List<String> to Map<String, Integer> convertion Java地图 <Integer, HashMap<String, String> &gt; - Java Map<Integer, HashMap<String, String>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM