java map 替换 value,使用java 8流替换String与hashmap值

I have String and HashMap like below codes:

Map map = new HashMap<>();

map.put("ABC", "123");

String test = "helloABC";

map.forEach((key, value) -> {

test = test.replaceAll(key, value);

});

and I try to replace the string with the HashMap values, but this doesn't work because test is final and cannot be reassigned in the body of forEach.

So are there any solutions to replace String with HashMap using Java 8 Stream API?

解决方案

As this cannot be made using only forEach() (message must be effectively final), workaround could be to create a final container (e. g. List) which stores a single String that is re-written:

final List msg = Arrays.asList("helloABC");

map.forEach((key, value) -> msg.set(0, msg.get(0).replace(key, value)));

String test = msg.get(0);

Note that I changed replaceAll() to replace() because former works with regex, but judging by your code seems you need replacement by string itself (don't worry, despite of confusing name it also replaces all occurrences).

If you want exactly Stream API, you may use reduce() operation:

String test = map.entrySet()

.stream()

.reduce("helloABC",

(s, e) -> s.replace(e.getKey(), e.getValue()),

(s1, s2) -> null);

But take into account, that such reduction will work properly only in serial (not parallel) stream, where combiner function is never called (thus may be any).

### 回答1: JavaMap可以使用put方法来替换value。具体操作如下: 1. 获取需要替换的key对应的value。 2. 使用put方法将新的value替换原有的value。 示例代码如下: Map<String, Integer> map = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); // 获取key1对应的value int oldValue = map.get("key1"); // 替换key1对应的value map.put("key1", 3); // 输出替换后的value System.out.println(map.get("key1")); // 输出3 ### 回答2: 在JavaMap是一种非常常见的数据结构,用于存储键对。有时候需要更新Map已存储的,可以使用put方法实现。但有时候需要直接替换Map某个key对应的value,这时可以使用replace方法实现。 replace方法有两个参数,第一个参数是要替换的key,第二个参数是要替换成的value使用replace方法时,如果key存在于Map,则会将其对应的value替换成第二个参数传入的。如果key不存在于Map,则不会进行任何操作。下面是replace方法的示例代码: ``` Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("orange", 3); map.replace("apple", 4); System.out.println(map.get("apple")); // 输出4 map.replace("grape", 5); System.out.println(map.containsKey("grape")); // 输出false ``` 上面的示例代码,我们首先创建了一个Map对象,并向其存储了三个键对。接着使用replace方法将"apple"对应的替换成了4,这时再获取"apple"对应的时会输出4。接着使用replace方法将"grape"对应的替换成了5,由于"grape"并不存在于Map,因此不会进行任何操作。 简单来说,替换Mapvalue可以使用replace方法,传入要替换的key替换成的value即可。需要注意的是,如果替换的key不存在于Map,则不会进行任何操作。 ### 回答3: 在Java Map 是一种非常常用的数据结构,它将键对储存在内存。在Map ,我们可以很容易地通过key获取它所对应的value。但有时候我们需要替换value,这时候可以使用以下方法: 方式一:使用put()方法 Map 提供了put()方法,它可以用于替换map的键对。如果该key不存在,则会新建一个键对。如果该key已经存在,则会使用新的value替换旧的value。 示例代码: ``` Map<String, Integer> map = new HashMap<>(); map.put("A", 1); //添加键map.put("B", 2); map.put("C", 3); map.put("A", 4); //替换 System.out.println(map); //{A=4, B=2, C=3} ``` 方式二:使用replace()方法 Map 还提供了replace()方法,它可以用于替换map已经存在的key所对应的value。如果该key不存在,则不会进行替换操作。 示例代码: ``` Map<String, Integer> map = new HashMap<>(); map.put("A", 1); //添加键map.put("B", 2); map.put("C", 3); map.replace("A", 4); //替换 map.replace("D", 5); //不会进行替换操作,因为key不存在 System.out.println(map); //{A=4, B=2, C=3} ``` 可以看出,replace()方法比put()方法更加简洁,因为它只需要传入要替换的key和value即可。 综上所述,Map 替换value的方法有两种:put()方法和replace()方法。大家可以根据具体的应用场景来选择使用哪种方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值