compute

default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction)

对key的value进行操作, 最终 value 的值为 lamda 表达式 remappingFunction 的执行结果

public class AnswerApp {

public static void main(String[] args) {
Map<String, String> map = Maps.newHashMap();

map.put("name", "answer");
map.put("address", "");

System.out.println("1->" +
map.compute("name",
(k, v) -> (v == null) ? "jaemon" : v));
System.out.println("2->" +
map.compute("sex",
(k, v) -> (v == null) ? "man" : v));
System.out.println("3->" +
map.compute("address",
(k, v) -> (StringUtils.isEmpty(v)) ? "sz" : v));

System.out.println(map);
}

}

程序运行结果

1->answer
2->man
3->sz
{address=sz, sex=man, name=answer}

 

putIfAbsent

default V putIfAbsent(K key, V value)

如果当前map中不存在 key, 则将value赋值给key, 如果 key存在, 则不进行任何操作

public class AnswerApp {

public static void main(String[] args) {
Map<String, String> map = Maps.newHashMap();

map.put("name", "answer");
map.put("address", "");

System.out.println("1->" + map.putIfAbsent("name", "jaemon"));
System.out.println("2->" + map.putIfAbsent("sex", "man"));
System.out.println("3->" + map.putIfAbsent("address", "sz"));

System.out.println(map);
}

}

程序运行结果

1->answer
2->null
3->
{address=, sex=man, name=answer}

 

computeIfAbsent

default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction)

如果key不存在或对应的value为null, 则使用 lamda 表达式 mappingFunction 函数的执行结果作为value; 如果key存在,则不进行操作

public class AnswerApp {

public static void main(String[] args) {
Map<String, List<String>> map = Maps.newHashMap();

map.put("names", Lists.newArrayList("kobe"));
map.put("address", null);

System.out.println("1->" +
map.computeIfAbsent("names",
k -> Lists.newArrayList("answer")));
// key=address对应的value为null, 使用lamda函数的执行结果作为value
System.out.println("2->" +
map.computeIfAbsent("address",
k -> Lists.newArrayList("sz")));
// key=books不存在, 使用lamda函数的执行结果作为value
System.out.println("3->" +
map.computeIfAbsent("books",
k -> Lists.newArrayList()));

System.out.println(map);
}

}

程序运行结果

1->[kobe]
2->[sz]
3->[]
{books=[], names=[kobe], address=[sz]}

 

computeIfPresent

default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction)

如果key存在,则将lamda表达式 remappingFunction 的执行结果作为value替换旧的value值; 如果key不存在或存在但对应的value为null, 则不进行任何操作

public class AnswerApp {

public static void main(String[] args) {
Map<String, List<String>> map = Maps.newHashMap();

map.put("names", Lists.newArrayList("kobe"));
map.put("sex", null);
map.put("books", Lists.newArrayList("java"));

// 如果存在 key, 则将key对应的value替换为指定的value值
System.out.println("1->" +
map.computeIfPresent("names",
(k, v) -> Lists.newArrayList("answer", "jaemon")));
// 不存在key为address的键值对, 因此下面语句不会对map哈希集有影响
System.out.println("2->" +
map.computeIfPresent("address",
(k, v) -> Lists.newArrayList("sz")));
System.out.println("3->" +
map.computeIfPresent("sex",
(k, v) -> Lists.newArrayList("man")));
List<String> rlt = map.computeIfPresent("books", (k, v) -> {
List<String> result = Lists.newArrayList();
result.addAll(v);
result.add("Spring");
return result;
});
System.out.println("4->" + rlt);

System.out.println(map);
}

}

程序运行结果

1->[answer, jaemon]
2->null
3->null
4->[java, Spring]
{names=[answer, jaemon], books=[java, Spring], sex=null}

 

merge

default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction)

如果key不存在, 则将value赋值给key, 否则执行 remappingFunction 函数并将函数执行结果赋值给key

public class AnswerApp {

public static void main(String[] args) {
Map<String, List<String>> map = Maps.newHashMap();

map.put("names", Lists.newArrayList("kobe"));

ArrayList<String> myNames = Lists.newArrayList("answer", "jaemon");
// 如果 key存在, 则执行lamda表达式中的规则(参数 newVal 为 myNames 的值)
List<String> names = map.merge("names", myNames,
(oldVal, newVal) -> {
oldVal.addAll(newVal);
return oldVal;
});
System.out.println("names=" + names);

ArrayList<String> myBooks = Lists.newArrayList("Java", "MySQL");
// 如果key=books不存在或对应的value为null, 则将 myBooks 值赋值给 books
List<String> books = map.merge("books", myBooks,
(oldVal, newVal) -> {
newVal.addAll(oldVal);
return newVal;
});
System.out.println("books=" + books);

System.out.println(map);
}

}

程序运行结果

names=[kobe, answer, jaemon]
books=[Java, MySQL]
{books=[Java, MySQL], names=[kobe, answer, jaemon]}