[java] JDK8常用特性

阅读: 评论:0

[java] JDK8常用特性

[java] JDK8常用特性

JDK8新特性

  1. Lambda 表达式
  2. 新的日期时间 API (LocalDateTime、LocalDate等等)
  3. Optional
  4. Base64
  5. HashMap的改进
  6. 接口的默认方法和静态方法
  7. Consumer、Supplier、Predicate、Function
  8. Stream

有多种方式生成 Stream:

    从 Collection 和数组

        Collection.stream()

        Collection.parallelStream()

        Arrays.stream(T array) or Stream.of()

    从 BufferedReader

        java.io.BufferedReader.lines()

    静态工厂

        java.util.stream.IntStream.range()

        java.nio.file.Files.walk()

    自己构建

        java.util.Spliterator

    其它

        Random.ints()

        BitSet.stream()

        Pattern.splitAsStream(java.lang.CharSequence)

        JarFile.stream()

 

 

1、allTasks集合----->过滤----->转成map集合

    List<TaskInfo> allTasks = ……;

    Map<String, TaskInfo> map = allTasks.stream().filter(o -> TaskType.Type()) != TaskType.IMPLIED).Map(TaskInfo::getId, Function.identity()));

 

2、先过滤后再取集

    public static List<ExchangeItem> sortItems(List<ExchangeItem> list, List<String> ids) {               Map<String, ExchangeItem> result = list.stream().Map(ExchangeItem::getId, Function.identity()));

      List<ExchangeItem> exchangeItems = ids.stream().filter(result :: containsKey).map(result :: get).List());

    }

 

3、list集合----->转成map集合

    list.stream().Map(TopPlayDetail::getNewsId, Function.identity()));

 

4、过滤非空实体

    public static List<NewsWithBLOBs> sortNews(List<NewsWithBLOBs> list, List<String> ids) {

        Map<String, NewsWithBLOBs> result = list.stream().Map(NewsWithBLOBs::getId, Function.identity()));

        return ids.stream().map(result::get).filter(Objects :: nonNull).List());

    }

 

5、调用当前类中的方法,参数为遍历的实体

    public void render(List<NewsWithBLOBs> news) {

        news.stream().forEach(this :: render);

    }

    public void render(NewsWithBLOBs news) {

        xxxx;

    }

 

6、遍历一个list集合,关联另一个集合

        List<DaHospital>daHospitalNameList = HospitalLikeName(keyWord);

        List<DaHospitalResponse> list = new ArrayList<>();

        daHospitalNameList.forEach(o -> list.add(DaHospitalResponse.from(o)));

 

7、取最小值(通过集合中元素的某字段,取该字段最小的这个元素。)

    List<TaskInfo> tasks = taskInfoMapper.selectByExample(example);

    TaskInfo nextTask = tasks.stream().min(Comparatorparing(TaskInfo :: getPhase)).get();

 

8、过滤----->取数量

    List<Integer> nums = wArrayList(1,null,3,4,null,6);

    nums.stream().filter(num -> num != null).count();

 

9、过滤----->排序(按年龄)----->取值

        List<Integer> result2 = ps.stream().filter(o -> !"fanbo".Name())).sorted(Comparatorparing(Person::getAge).reversed()).map(Person::getAge).List()); (倒序)

        List<Integer> result3 = ps.parallelStream().filter(o -> !"fanbo".Name())).sorted(Comparatorparing(Person::getAge)).map(Person::getAge).List()); (正序)

 

10、取和(过滤----->取值----->取年龄的和)

        Integer sum = ps.stream().filter(o -> !"fanbo".Name())).mapToInt(Person::getAge).sum();

 

11、取最大值

        Person maxAge = ps.stream().filter(o -> !"fanbo".Name())).max(Comparatorparing(Person::getAge)).get();

 

12、集合转换成数组

        String[] result = ps.stream().map(o -> o.getName() + o.getAge()).toArray(String[]::new);

 

13、转换成ArrayList

        ArrayList<Person> al = ps.stream().filter(o -> !"fanbo".Name())).collect( Collection(ArrayList::new));

 

14、去重

        List<String> strs = Arrays.asList("111", "222", "333", "222");

        strs.stream().distinct().forEach(System.out::println);

 

15、拼接元素

        String result = ps.stream().map(o -> o.getName() + o.getAge()).collect( Collectors.joining(";")).toString();

 

16、大小写转换

        strs = strs.stream().map(String :: toUpperCase).List());

 

17、操作了元素,再将操作后的集合重新生成stream

        Integer max = ps.stream().filter(o -> !"fanbo".Name()))

                .peek(System.out::println).mapToInt(Person::getAge)

                .peek(ages::add).max().getAsInt();

 

18、数据转成流,再操作

        String[] strs = new String[]{"a","b","c"};

        List<String> ss = Stream.of(strs).filter(o -> !"b".equals(o)).List());

        ss.stream().forEach(System.out::println);

 

19、集合limit、skip

    List<Phone> phones = ……; 

    phones = phones.stream().limit(20).skip(5).List());

翻第二页的情况下。skip写在前面,limit写在后面

list = list.stream().skip(pNo).limit(pageSize).List());

 

20、分组groupingBy

List<TicketWsaFile> ticketWsaFiles = .....;

Map<String, List<TicketWsaFile>> ticketGroupMap = ticketWsaFiles.stream().upingBy(TicketWsaFile::getUser_name));

 

21、判断是否有符合条件的元素

List<String> datas = Arrays.asList("aaa", "bbb", "ccc");

boolean match = datas.stream().anyMatch(o -> o.equals("ddd")); //有一个符合返回true

boolean match2 = datas.stream().allMatch(o -> o.equals("ddd")); //都符合才返回true

boolean match3 = datas.stream().noneMatch(o -> o.equals("ddd")); //是否都不符合

 

22、数组中元素是数组,把元素取出来合并成一个数组。

Map<Long, List<PolicyDetailInvoiceVo>> detailInvoiceVoMap = invoiceTrans(invoices);

detailInvoiceVoMap.values()的值是个数组,其元素是List<PolicyDetailInvoiceVo>

detailInvoiceVoMap.values().stream().flatMap(o -> o.stream()).List()));

 

 

 

 

本文发布于:2024-01-28 23:22:55,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170645537811037.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:特性   常用   java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23