`
wuxiaozeng2440
  • 浏览: 25791 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

lambda test

    博客分类:
  • code
 
阅读更多

  @Test

    public void test() {

DaDz dz1 = new DaDz(2L,2223L,"2","bb");

DaDz dz2 = new DaDz(1L,2222L,"2","aa");

DaDz dz3 = new DaDz(3L,2224L,"2","cccc");

DaDz dz4 = new DaDz(4L,2225L,"3","dd");

List<DaDz> entities = Lists.newArrayList(dz1,dz2,dz3,dz4);

 

//元素过滤  

List<DaDz> filter =entities.stream().filter((e)->{return e.getDZ1().equals("dd");}).collect(Collectors.toList());

//list迭代 

filter.forEach((e)->{System.out.println(e);});

System.out.println("元素过滤  -----------");

 

//list排序

entities.sort(Comparator.comparing(DaDz::getSYSID));

entities.forEach((e)->{System.out.println(e);});

System.out.println("list排序-----------");

 

//字段抽取  

List<String>  field= entities.stream().map((e)->{return  e.getDZ1();}).collect(Collectors.toList());

field.forEach((e)->{System.out.println(e);});

System.out.println("字段抽取-----------");

 

//list转map  

Map<String,DaDz> map=entities.stream().collect(Collectors.toMap(DaDz::getDZ1, e->e));

//map迭代  

map.forEach((k,v)->{System.out.println(k+" "+v.getDZ1());});

System.out.println("list转map-----------");

 

//list分组

Map<String,List<DaDz>> map1=entities.stream().collect(Collectors.groupingBy(DaDz::getLX));

map1.forEach((k,v)->{System.out.println(k+" "+v.size()+" "+v.get(0));});

Map<String,List<String>> map2=entities.stream().collect(Collectors.groupingBy(DaDz::getLX,Collectors.mapping(DaDz::getDZ1,Collectors.toList())));

System.out.println(map2);

System.out.println("list分组-----------");

 

//平均值

Double avg = entities.stream().map(DaDz::getSYSID).collect(Collectors.averagingDouble(value -> value)); 

Optional<DaDz> max1 =  entities.stream().collect(Collectors.maxBy(Comparator.comparing(DaDz::getSYSID)));

Optional<Long> max2 =  entities.stream().map(DaDz::getSYSID).reduce(Math::max);

System.out.println("平均值-----------"+avg);

System.out.println("max1值-----------"+max1);

System.out.println("max2值-----------"+max2);

 

DoubleSummaryStatistics doubleSummaryStatistics = entities.stream().map(dz->dz.getSYSID()).collect(Collectors.summarizingDouble(value -> value));

Long sum = entities.stream().map(dz->dz.getSYSID()).collect(Collectors.summingLong(value -> value));

System.out.println("DoubleSummaryStatistics-----------"+doubleSummaryStatistics);

System.out.println("sum-----------"+sum);

 

//函数式接口Predicate

Predicate<DaDz> startWithD = (n) -> n.getDZ1().startsWith("d");

Predicate<DaDz> fourLength = (n) -> n.getDZ1().length() == 4;

entities.stream().filter(startWithD.and(fourLength)).forEach(System.out::println);

System.out.println("Predicate and-----------");

entities.stream().filter(startWithD.or(fourLength)).forEach(System.out::println);

System.out.println("Predicate or-----------");

 

//求最大值

        List<Integer> list2 = new Random().ints(-100,100).limit(250).boxed().collect(Collectors.toList());

        System.out.println(list2);

        Optional<Integer> max = list2.stream().reduce(Math::max);

        max.ifPresent(value -> System.out.println(value));

 

new Thread(() -> System.out.println("In Java8!")).start();

    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics