JDK8 集合之List、Map新增方法

list

replaceAll

public void testList() {
    List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
    list.replaceAll(t -> t + 10);
    System.out.println(list);
}

toArray

public void testList2() {
    List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
    Integer[] array = list.toArray(new Integer[]{});
}

map

replace

replace(k,v)

在指定的键存在,有与之相关的映射值时,才会将指定的键映射到指定的值

在指定的键不存在时,方法会return回来一个null,指定的键值不会添加到Map中

public void testMap() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    String replace = map.replace("a", "ab"); // A
    String replace1 = map.replace("c", "cc"); // null
    System.out.println(map); // {a=ab, b=B}
}

String replace = null;
if (map.containsKey("a")) {
    replace = map.put("a", "ab");
}
System.out.println(replace);

replace(k,v,v)

if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
    map.put(key, newValue);
    return true;
} else {
    return false;
}
public void testMap() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    boolean replace = map.replace("a", "A", "ab"); // true
    System.out.println(map); // {a=ab, b=B}
}

replaceAll(BiFunction)

将每个条目的值替换为对该条目调用给定函数的结果,直接所有条目都被处理或该函数抛出异常。函数抛出的异常被转发给调用者

for (Map.Entry<K, V> entry : map.entrySet()) {
    entry.setValue(function.apply(entry.getKey(), entry.getValue()));  
}
public void testMap() {
    Map<String, Integer> map = new HashMap<>();
    map.put("a", 1);
    map.put("b", 2);
    // 实现v自增
    map.replaceAll((k, v) -> v + 1);
}

getOrDefault

统计一个字符串中各个字符出现的频率

public void test() {
    String str = "hello java, i am vary happy! nice to meet you";
    Map<Character, Integer> map = new HashMap<>();
    for (char c : str.toCharArray()) {
        map.put(c, map.getOrDefault(c, 0) + 1);
    }
    System.out.println(map);
}

compute

put/replace返回的是旧值,compute方法返回的是新值

对指定的key在map中的值进行操作 不管存不存在,操作完成后保存到map中

public void test2() {
  String str = "hello java, I am vary happy! nice to meet you";
  Map<Character, Integer> map = new HashMap<>();
  for (char c : str.toCharArray()) {
    // Integer count = Optional.ofNullable(map.get(c)).orElse(0);
    Integer count = map.getOrDefault(c, 0);
    map.put(c, count + 1);
  }
  System.out.println(map);
}
public void test() {
  String str = "hello java, i am vary happy! nice to meet you";
  Map<Character, Integer> map = new HashMap<>();
  for (char c : str.toCharArray()) {
    /**
    * Map接口的compute方法的二元函数,将c作为k,map.get(c)作为v,
    * 作为BiFunction函数的入参,返回结果最终会被作为v,重新赋值给当前k
    */
    // put方法返回的是旧值,compute方法返回的是新值
    Integer newValue = map.compute(c, (k, v) -> 
                                   Optional.ofNullable(v).orElse(0) + 1);
    // map.put(c, map.getOrDefault(c, 0) + 1);
  }
  System.out.println(map);
}

最终返回的还是v的值

public void testMap2() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    for (Map.Entry<String, String> entry : map.entrySet()) {
        map.compute(entry.getKey(), (k, v) -> k + "a");
    }
    System.out.println(map); // {a=aa, b=ba}
}

computeIfAbsent

computeIfAbsent:存在时返回存在的值,不存在时返回新值

当k存在时,直接返回k对应的value值

当k不存在时,通过Function函数计算的值给当前v

public void testMap() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    /**
     * k为入参,
     * 如果k对应的value不为null,则直接返回旧值
     * 如果k对应的value为null,则执行后面的function函数部分,返回值作为v
     */
    String v = map.computeIfAbsent("b", k -> "v");  // 输出 B
    String v1 = map.computeIfAbsent("c", k -> "v"); // 输出 v
    System.out.println(map); // {a=A, b=B, c=v}
}
Map<String, List<String>> result = new HashMap<>();
List<String> a = result.computeIfAbsent("a", k -> new ArrayList<>());
a.add("aa");
a = result.computeIfAbsent("a", k -> new ArrayList<>());
a.add("bb");

Map<String, List<String>> result = new HashMap<>();
List<String> a = result.compute("a", (k, v) -> Optional.ofNullable(v).orElse(new ArrayList<>()));
a.add("aa");
a = result.compute("a", (k, v) -> Optional.ofNullable(v).orElse(new ArrayList<>()));
a.add("bb");

computeIfPresent

只对已经存在key的进行操作,其他不操作

public void testMap() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    String v1 = map.computeIfPresent("b", (k, v) -> "v");  // 输出 v
    String v2 = map.computeIfPresent("c", (k, v) -> "v"); // 输出 null
    System.out.println(map); // {a=A, b=v}
}

putIfAbsent

如果指定的k尚未与值相关联(或者映射到null),将其与给定值相关联并返回null,否则直接返回当前值

public void testMap() {
    Map<String, String> map = new HashMap<>();
    map.put("a", "A");
    map.put("b", "B");
    /**
     * putIfAbsent(K key, V value)
     *  如果k对应的v存在,则执行put(k,v),则返回v
     *  如果K对应的k不存在,则执行put(k,v),同时返回null
     */
    String v = map.putIfAbsent("b","v");  // 输出 B
    String v1 = map.putIfAbsent("c","v");  // 输出 null
    System.out.println(map); // {a=A, b=B, c=v}
}

merge

把list中的对象,按照属性男女分组,然后把年龄汇总

public void test() {
    // 学生的集合
    List<Student> students = new ArrayList<>();
    students.add(new Student("张三", "男", 18));
    students.add(new Student("李四", "男", 20));
    students.add(new Student("韩梅梅", "女", 18));
    students.add(new Student("小红", "女", 45));

    // 声明接收结果的 map
    Map<String, Integer> resultMap = new HashMap<>();
    // BiFunction 函数有点类似于 reduce
    students.forEach(student -> resultMap.merge(student.getSex(), 
                                         student.getAge(), (a, b) -> a + b));
    resultMap.forEach((k, v) -> System.out.println(k + "  : " + v));
}
public void test() {
    //学生的集合
    List<Student> students = new ArrayList<>();
    students.add(new Student("张三", "男", 18));
    students.add(new Student("李四", "男", 20));
    students.add(new Student("韩梅梅", "女", 18));
    students.add(new Student("小红", "女", 45));

    //声明接收结果的 map
    Map<String, List<Student>> resultMap = new HashMap<>();
    students.forEach(student -> {
        List<Student> s = resultMap.computeIfAbsent(student.getSex(), k -> 
                                                    new ArrayList<>());
        s.add(student);
    });
    resultMap.forEach((k, v) -> System.out.println(k + "  : " + v));
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

①笶侕濄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值