jdk8的map新增api

package com.phone.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.Application;
import com.phone.entity.Car;
import com.phone.entity.GoWhere;
import com.phone.entity.Phone;
import com.phone.mapper.PhoneMapper;
import com.phone.service.IPhoneService;
import com.phone.service.impl.PhoneServiceImpl;

import ch.qos.logback.core.rolling.helper.MonoTypedConverter;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test {

	/**
	 * 如果包含指定Key就修改值,如果不包含就添加一对
	 */
	public static void method4() {
		Map<String,Integer> map=new HashMap<>();
		map.put("a", 5);
		map.put("b", 6);
		map.put("c", 7);
		Integer compute = map.compute("c", (k,v)->v+10);
		System.out.println(compute);
		System.out.println(map);
	}
	
	/**
	 * 如果不存在指定key就添加一对,存在就不修改
	 */
	public static void method5() {
		Map<String,Integer> map=new HashMap<>();
		map.put("a", 5);
		map.put("b", 6);
		map.put("c", 7);
		Integer computeIfAbsent = map.computeIfAbsent("c",key->new Integer(10));
		Integer computeIfAbsent1 = map.computeIfAbsent("d",key->new Integer(10));
		System.out.println(computeIfAbsent1);
		System.out.println(map);
	}

	/**
	 * 如果存在值就修改,不存在就不添加
	 */
	public static void method6() {
		Map<String,Integer> map=new HashMap<>();
		map.put("a", 5);
		map.put("b", 6);
		map.put("c", 7);
		Integer computeIfAbsent = map.computeIfPresent("c",(k,v)->v==7?10:v);
		System.out.println(computeIfAbsent);
		System.out.println(map);
	}
	

	/**
	 * 如果不存在就给一个默认值
	 */
	public static void method7() {
		Map<String,Integer> map=new HashMap<>();
		map.put("a", 456);
		map.put("b", 123);
		map.put("c", 234);
		System.out.println(map.getOrDefault("a", 789));
		System.out.println( map.getOrDefault("d", 789));
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值