JAVA8新特性汇总,后续不断更新

1 篇文章 1 订阅

1.Java8中Map的遍历方式总结

public class LambdaMap {

    private Map<String, Object> map = new HashMap<>();

    @Before
    public void initData() {
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", "value4");
        map.put("key5", "value5");
        map.put("key5", "value6");
    }

    /**
     * 遍历Map的方式一
     * 通过Map.keySet遍历key和value
     */
    @Test
    public void testErgodicWayOne() {
        System.out.println("---------------------Before JAVA8 ------------------------------");
        for (String key : map.keySet()) {
            System.out.println("map.get(" + key + ") = " + map.get(key));
        }
        System.out.println("---------------------JAVA8 ------------------------------");
        map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
    }

    /**
     * 遍历Map第二种
     * 通过Map.entrySet使用Iterator遍历key和value
     */
    @Test
    public void testErgodicWayTwo() {
        System.out.println("---------------------Before JAVA8 ------------------------------");
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("---------------------JAVA8 ------------------------------");
        map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
    }

    /**
     * 遍历Map第三种
     * 通过Map.entrySet遍历key和value,在大容量时推荐使用
     */
    @Test
    public void testErgodicWayThree() {
        System.out.println("---------------------Before JAVA8 ------------------------------");
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("---------------------JAVA8 ------------------------------");
        map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
    }

    /**
     * 遍历Map第四种
     * 通过Map.values()遍历所有的value,但不能遍历key
     */
    @Test
    public void testErgodicWayFour() {
        System.out.println("---------------------Before JAVA8 ------------------------------");
        for (Object value : map.values()) {
            System.out.println("map.value = " + value);
        }
        System.out.println("---------------------JAVA8 ------------------------------");
        map.values().forEach(System.out::println); // 等价于map.values().forEach(value -> System.out.println(value));
    }

    /**
     * 遍历Map第五种
     * 通过k,v遍历,Java8独有的
     */
    @Test
    public void testErgodicWayFive() {
        System.out.println("---------------------Only JAVA8 ------------------------------");
        map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
    }
}

2.JAVA8对List对象集合按对象属性分组、分组汇总、过滤、排序等操作示例

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args){
        List<PersonData> list = new ArrayList<PersonData>();
        PersonData p1 = new PersonData();
        p1.setId("1");
        p1.setName("张三");
        p1.setType("管理员");
        p1.setAge(20);
        list.add(p1);

        PersonData p2 = new PersonData();
        p2.setId("2");
        p2.setName("李四");
        p2.setType("管理员");
        p2.setAge(30);
        list.add(p2);

        PersonData p3 = new PersonData();
        p3.setId("3");
        p3.setName("王五");
        p3.setType("用户");
        p3.setAge(40);
        list.add(p3);

        PersonData p4 = new PersonData();
        p4.setId("4");
        p4.setName("马六");
        p4.setType("访客");
        p4.setAge(50);
        list.add(p4);

        //跟据某个属性分组
        Map<String, List<PersonData>> collect = list.stream().collect(Collectors.groupingBy(PersonData::getType));Map<String, List<PersonData>> collect = list.stream().collect(Collectors.groupingBy((e->e.getType())));
        System.out.println(collect);
        
        //根据某个属性分组,汇总某个属性
        Map<String, Integer> collect2 = list.stream().collect(Collectors.groupingBy(PersonData::getType,Collectors.summingInt(PersonData::getAge)));
        System.out.println(collect2);
        
        //根据某个属性添加条件过滤数据,
        list = list.stream().filter(u -> !u.getType().equals("访客")).collect(Collectors.toList());
        System.out.println(list);
        
        //判断一组对象里面有没有属性值是某个值
        boolean add = list.stream().anyMatch(m -> "王五".equals(m.getName()));
        System.out.println(add);
        
        //取出一组对象的某个属性组成一个新集合
        List<String> names=list.stream().map(PersonData::getName).collect(Collectors.toList());
        System.out.println(names);

		//取出一组对象的某个属性组成一个新集合并去重
        List<String> names=list.stream().map(PersonData::getName).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        System.out.println(names);

		//将List集合转为map
		Map<String, String> result = list.stream().collect(Collectors.toMap(PersonData::getId, PersonData::getName));
    
    	//从List中过滤出一个元素
 		PersonData match = list.stream().filter((user) -> user.getId() == 1).findAny().get();

		//将List对象转换为其他List对象(注意:1.新对象需要根据需求自己创建一个构造器 2.如果不需要过滤"张三"可以直接去掉'equals' 3.新对象中最后一个参数 "1000"为我给这个新对象每个都赋值一个1000的属性,可以去掉,也可以赋值其他相同的属性)
 		List<Employee> employees = list.stream().filter(p -> p.getName().equals("张三")).map(p -> new Employee(p.getName(), p.getType(), p.getAge(),1000)).collect(Collectors.toList());                    
         //将对象集合中某一属性汇总   
       	int ages = list.stream().collect(Collectors.summingInt(PersonData::getAge));
       	//遍历修改对象集合中某一属性
		list.forEach(p -> p.setName("17_" + p.getName()));
		//将User集合转成key ->id ,value->User对象的map(方式一:推荐)
		Map<Integer, User> map2 = list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
		//将User集合转成key ->id ,value->User对象的map(方式二)
		HashMap<Integer, User> map1 = list.stream().collect(HashMap::new, (m, v) -> m.put(v.getId(), v), HashMap::putAll);

		List<String> lists = new ArrayList<>();
		lists.add("a");
		lists.add("b");
		lists.add("c");
		//使用removeIf方法,->里的是判断条件,如果符合这个条件就删除。这里会删除带有c的元素
		lists.removeIf(s -> s.contains("c"));
//将list集合对象根据对象中年龄进行升序排序
List<PersonData> lists = list.stream().sorted(Comparator.comparing(PersonData::getAge)).collect(Collectors.toList());
 //将list集合对象根据对象中年龄进行降序排序
List<PersonData> lists = list.stream().sorted(Comparator.comparing(PersonData::getAge).reversed()).collect(Collectors.toList());
//将list集合对象根据对象中年龄进行降序排序,再根据ID升序排序
List<PersonData> lists = list.stream()
                .sorted(Comparator.comparing(PersonData::getAge).reversed().thenComparing(PersonData::getId))
                .collect(Collectors.toList());

List<User> list = Arrays.asList(
        new User("段誉","男",18),
        new User("段誉","男",18),
        new User("段誉","男",14),
        new User("段誉","男",27),
        new User("段誉","男",null),
        new User("段誉","男",36)
);
// 注意点:如果查询的获取list中的所有年龄时,返回结果还是6个,一般情况下建议做过滤处理
list.stream()
	.map(User::getAge)
	.filter(ObjectUtil::isNotEmpty) // 将为空的数据过滤掉
	.map(String::valueOf)
	.collect(Collectors.toList());

    }
}

打印

{用户=[com.test4.PersonData@19a45b3], 访客=[com.test4.PersonData@99a589], 管理员=[com.test4.PersonData@372a00, com.test4.PersonData@dd8dc3]}
{用户=40, 访客=50, 管理员=50}
[com.test4.PersonData@372a00, com.test4.PersonData@dd8dc3, com.test4.PersonData@19a45b3]
true
[张三, 李四, 王五]

3.java8 lambda 统计list中对象的重复次数

List<Integer> list = new ArrayList() {
            {
                add(12);
                add(20);
                add(12);
                add(22);
                add(22);
                add(23);
                add(159);
                add(12);
            }
        };
 //利用java8的lambda表达式统计list集合中各个数字出现的次数(数字,次数)
Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
//java8遍历打印map
map.forEach((k, v) -> System.out.println(k + ":" + v));

4.java8 map根据key或者value进行排序

将map根据key或者value进行排序

Map<String,BigDecimal> map =new HashMap<>();
map.put("one", 0.08);
map.put("two", 0.1);
map.put("three", 0.2);
map.put("four", 0.91);
//对这个map根据value值倒序排序,下面给出工具类:
public <K, V extends Comparable<? super V>> Map<K, V> sortByValueDesc(Map<K, V> map) {
	Map<K, V> result = new LinkedHashMap<>();
	map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));     
	return result;
}
//根据map的key进行排序,需要对上面的工具类进行小小的修改,代码如下:
public <K extends Comparable<? super K>, V > Map<K, V> sortByKeyDesc(Map<K, V> map) {
	Map<K, V> result = new LinkedHashMap<>();
	map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));     
	return result;
}
//如果我们需要根据key排序,就需要让key 继承 Comparable ,也就说我们需要对待排序的字段继承 Comparable接口。另一个问题就是,上面的这种写法排序效果是 降序排序,如果我们需要升序排序的话,只需要将上面的.reversed()关键字限制去掉即可:
public <K, V extends Comparable<? super V>> Map<K, V> sortByValueAsn(Map<K, V> map) {
	Map<K, V> result = new LinkedHashMap<>();
	map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));      
	return result;
}

附通用的map的key排序:

public static <K extends String, V> Map sortMapKey(Map<K, V> map) {
	List<Map.Entry<K, V>> list = new ArrayList(map.entrySet());
	list.sort((o1, o2) -> o2.getKey().length() - o1.getKey().length());
	Map<K, V> linkedMap = new LinkedHashMap<>();
	for (Map.Entry<K, V> entry : list) {
	  linkedMap.put(entry.getKey(), entry.getValue());
	}
	return linkedMap;
}

5.java8的lambda表达式获取map中value最大值的五种方法:

        Map<String,Integer> map = new HashMap<>();
        map.put("张三", 1);
        map.put("李四", 2);
        map.put("王五", 3);
        //第一种方法
        Optional<Map.Entry<String, Integer>> max1 = map.entrySet().stream().max(Map.Entry.comparingByValue());              
        //第二种方法
        Optional<Map.Entry<String, Integer>> max2 = map.entrySet().stream().max((x1, x2) -> Integer.compare(x1.getValue(), x2.getValue()));
        //第三种方法
        Optional<Map.Entry<String, Integer>> max3 = map.entrySet().stream().collect(Collectors.maxBy(Map.Entry.comparingByValue()));              
        //第四种方法
        Optional<Map.Entry<String, Integer>> max4 = map.entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue));    
        //第五种方法
        IntSummaryStatistics max5 = map.entrySet().stream().collect(Collectors.summarizingInt(Map.Entry::getValue));

6.将Map数据转换为自定义对象的List,例如把map的key,value分别对应Person对象两个属性

//方式一
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
//方式二
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
//方式三
List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
	.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

7.附:普通方式将map的key和value转list, 或者list转set, 并取出List集合中的前N个数据

Map<String,Object> map = new HashMap<String,Object>(){{
			put("name","Tom");
			put("age",28);
}};
//将Map中的value提取到list集合中
List<Object> values = new ArrayList<Object>(map.values());
//将Map中的key提取到list集合中
List<String> keys = new ArrayList<String>(map.keySet());
//将List转换为Set
Set<Object> set = new HashSet<Object>(list);

//在java.util.List中有一个subList()方法,作用是返回一个List集合的其中一部分视图
List<E>(对象的集合).subList(int fromIndex, int toIndex);
/**
1.因为返回的是List<E>中一部分对象的集合,返回的结果集合也是List<E>的子集合,并是以下标索引取值。

2.父集合List<E>以fromIndex开始(包含),到toIndex结束(不包含)的部分为返回的子集合。

3.因为是通过下标索引取值,可用此方法分页。

4.此方法返回的是父集合的一部分视图,无论改变那个集合,另一个都会随动。而解决方法很简单,只要开辟一个新的集合对象去接受就OK了。

举个例子需要取List集合的前1000条数据,那么可以这样写:
*/

list.subList(0, 1000);
//注意,结束的索引是不包含的,因此不是999,而是1000。

8.java8中根据判断删除列表list中的元素

List<String> lists = new ArrayList<>();
lists.add("a");
lists.add("b");
lists.add("c");
 
//使用removeIf方法,->里的是判断条件,如果符合这个条件就删除。这里会删除带有c的元素
lists.removeIf(s -> s.contains("c"));

9.Java8中时间LocalDateTime或LocalDate实用补充

前端请求参数,后端使用日期接收:
import org.springframework.format.annotation.DateTimeFormat; //格式化接收时间参数
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate endTime;

前端请求参数,后端使用日期及时间接收:
import org.springframework.format.annotation.DateTimeFormat; //格式化接收时间参数
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime endTime;

后端返回日期时间参数给前端:
import com.fasterxml.jackson.annotation.JsonFormat; //格式化时间参数
import com.fasterxml.jackson.annotation.JsonProperty; //定义返回json名称
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty(value = "addedTime")
private LocalDateTime addedTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty(value = "shelfTime")
private LocalDateTime shelfTime;

获取当前时间:
LocalDateTime now = LocalDateTime.now();

获取今天凌晨0~24时时间:
LocalDateTime nowBeginTime = LocalDateTime.of(now.toLocalDate(), LocalTime.MIN);
LocalDateTime nowEndTime = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);

获取昨日当前时间:
now.minusDays(1);

获取昨日凌晨0~24时时间:
LocalDateTime yesBeginTime = LocalDateTime.of(now.minusDays(1).toLocalDate(), LocalTime.MIN);
LocalDateTime yesEndTime = LocalDateTime.of(now.minusDays(1).toLocalDate(), LocalTime.MAX);

获取当月第一天0时和最后一天24时时间:
LocalDateTime monthBeginTime = LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
LocalDateTime monthEndTime = LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值