怎样在Java中替换List<map>中的映射键的值

huangapple go评论72阅读模式
英文:

How can i replace List<map> map key's value in java

问题

我想要替换这里所有迭代的时间值

List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
Map myMap1 = new HashMap();
myMap1.put("name", "jai");
myMap1.put("time", Instant.now());
mapList.add(myMap1);
Map myMap2 = new HashMap();
myMap2.put("name", "kishan");
myMap2.put("time", Instant.now());
英文:

I want to repalce here time value for all iteration

List&lt;Map&lt;String,Object&gt;&gt; mapList=new ArrayList&lt;Map&lt;String,Object&gt;&gt;();
Map myMap1=new HashMap();
myMap1.put(&quot;name&quot;, &quot;jai&quot;);
myMap1.put(&quot;time&quot;, Instant.now());
mapList.add(myMap1);
Map myMap2=new HashMap();
myMap2.put(&quot;name&quot;, &quot;kishan&quot;);
myMap2.put(&quot;time&quot;, Instant.now()); 

答案1

得分: 1

使用remove("time")来返回旧值并放到新键中

mapList.forEach(map ->{
    map.put("new time", map.remove("time"));
});
英文:

use remove(&quot;time&quot;) to return old value and put to new key

mapList.forEach(map -&gt;{
    map.put(&quot;new time&quot;,map.remove(&quot;time&quot;));
});

答案2

得分: 1

我得到了答案

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HelloWorld {

    public static void main(String... args) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> myMap1 = new HashMap<String, Object>();
        myMap1.put("name", "jai");
        myMap1.put("time", Instant.now());
        mapList.add(myMap1);
        Map<String, Object> myMap2 = new HashMap<String, Object>();
        myMap2.put("name", "kishan");
        myMap2.put("time", Instant.now());
        mapList.add(myMap2);
        System.out.println(mapList);
        mapList.forEach(oneMap -> replaceKeyBy(oneMap));
        System.out.println(mapList);

    }

    private static void replaceKeyBy(Map<String, Object> map) {
        map.replace("time", Instant.now().plus(10, ChronoUnit.DAYS));
    }
}
英文:

I got the answer

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HelloWorld {

    public static void main(String... args) {
        List&lt;Map&lt;String,Object&gt;&gt; mapList=new ArrayList&lt;Map&lt;String,Object&gt;&gt;();
        Map&lt;String, Object&gt; myMap1=new HashMap&lt;String, Object&gt;();
        myMap1.put(&quot;name&quot;, &quot;jai&quot;);
        myMap1.put(&quot;time&quot;, Instant.now());
        mapList.add(myMap1);
        Map&lt;String, Object&gt; myMap2=new HashMap&lt;String, Object&gt;();
        myMap2.put(&quot;name&quot;, &quot;kishan&quot;);
        myMap2.put(&quot;time&quot;, Instant.now());
        mapList.add(myMap2);
        System.out.println(mapList);
        mapList.forEach(oneMap -&gt; replaceKeyBy(oneMap));
        System.out.println(mapList);
        
        }
    private static void replaceKeyBy(Map&lt;String, Object&gt; map) {
        map.replace(&quot;time&quot;,Instant.now().plus(10, ChronoUnit.DAYS));
    }
}

huangapple
  • 本文由 发表于 2020年10月26日 14:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64532246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定