【操作系统】java8-lamba表明式的施用-遁地尘暴

作者:操作系统

importjava.time.LocalDate;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.Optional;importjava.util.stream.Collectors;/***@authorInJavaWeTrust*/publicclassTestB{publicstaticvoidmain(String[]args卡塔尔国{Appleapple1=newApple("红富士",45,"红","广西",LocalDate.of(2019,10,12卡塔尔(قطر‎卡塔尔国;Appleapple2=newApple("国光",60,"绿","福建",LocalDate.of(2019,10,18卡塔尔(قطر‎卡塔尔(قطر‎;Appleapple3=newApple("红星",50,"红","浙江",LocalDate.of(2019,9,10State of Qatar卡塔尔国;Appleapple4=newApple("黄团长",85,"黄","西藏",LocalDate.of(2019,10,6State of Qatar卡塔尔;Appleapple5=newApple("嘎啦",35,"绿","河南",LocalDate.of(2019,8,21卡塔尔卡塔尔(قطر‎;Appleapple6=newApple("寒富",40,"红","西藏",LocalDate.of(2019,9,20卡塔尔卡塔尔;ListAppleappleList=newArrayList(卡塔尔;appleList.add(apple1卡塔尔;appleList.add(apple2卡塔尔国;appleList.add(apple3卡塔尔(قطر‎;appleList.add(apple4卡塔尔国;appleList.add(apple5卡塔尔(قطر‎;appleList.add(apple6卡塔尔(قطر‎;//寻找最重的苹水果和干果种MapString,IntegerappleVariety=newHashMap(卡塔尔国;appleList.stream(卡塔尔(قطر‎.collect(Collectors.groupingBy(Apple::getVariety卡塔尔State of Qatar//依照项目分组[花色名称Apple].forEach((k,vState of Qatar-appleVariety.put(k,v.stream(卡塔尔(قطر‎.collect(Collectors.summingInt(Apple::getWeight卡塔尔卡塔尔(قطر‎State of QatarState of Qatar;OptionalStringappleOpt=appleVariety.keySet(卡塔尔.stream(State of Qatar.sorted(Comparator.comparing(appleVariety::getState of Qatar.reversed(卡塔尔(قطر‎卡塔尔.limit(1L卡塔尔.findFirst(State of Qatar;if(appleOpt.isPresent(卡塔尔(قطر‎卡塔尔{System.out.println("最重的苹果名称:" appleOpt.get(State of Qatar卡塔尔(قطر‎;}}}结果:最重的苹果名称:黄上校importjava.time.LocalDate;/***@authorInJavaWeTrust*/publicclassApple{privateStringvariety;privateintweight;privateStringcolor;privateStringplaceOfOrigin;privateLocalDateproductionDate;publicApple(){}publicApple(Stringvariety,intweight,Stringcolor,StringplaceOfOrigin,LocalDateproductionDate){this.variety=variety;this.weight=weight;this.color=color;this.placeOfOrigin=placeOfOrigin;this.productionDate=productionDate;}publicStringgetVariety(){returnvariety;}publicvoidsetVariety(Stringvariety){this.variety=variety;}publicintgetWeight(){returnweight;}publicvoidsetWeight(intweight){this.weight=weight;}publicStringgetColor(){returncolor;}publicvoidsetColor(Stringcolor){this.color=color;}publicStringgetPlaceOfOrigin(){returnplaceOfOrigin;}publicvoidsetPlaceOfOrigin(StringplaceOfOrigin){this.placeOfOrigin=placeOfOrigin;}publicLocalDategetProductionDate(){returnproductionDate;}publicvoidsetProductionDate(LocalDateproductionDate){this.productionDate=productionDate;}@OverridepublicStringtoString(){return"Apple[" "variety=" variety "," "weight=" weight "," "color=" color "," "placeOfOrigin=" placeOfOrigin "," "productionDate=" productionDate "]";}}

return "Unknown";

  Stream<Object> strings = shapelist.stream().<Object>map(s->s.getColor());

.map(s -> s.split

  add("Red");

apples.stream;

  参数类型以致重返类型假设能够透过上下文推测出来可不写

List<String> appleName = apples.parallelStream()

  Set<String> colosSet = shpaeList.stream().map(s->s.getColor()).collect(Collectors.toSet());

Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、

  布局方法引用:Class::new

有关函数接口,供给记住的正是两件事:

  List<Shape> shapeList = colorList.stream().map(Shape::new).collect(Collectors.toList());

if (appleName.stream(卡塔尔.allMatch(a -> StringUtils.equalsIgnoreCase(a, "一流苹果"卡塔尔(قطر‎卡塔尔(قطر‎卡塔尔(قطر‎;

  BiFunction<T,U,R>

Function<Float, Float> f = a -> a * 2;

  学习lamba表明式是格外最首要的,你会发觉java变的可爱多了。

卡住查找:

};

Optional<User> user = Optional.ofNullable;

a.规则

.map(user->user.name)

List、Set   .stream()   

private Integer appleClassic;

  Map<String,Map<String, List<Shape>>> shapeMap3 = shpaeList.stream().collect(Collectors.groupingBy(s->s.getColor(),Collectors.groupingBy(s->s.getColor(),Collectors.toList())));

谓词查找:

  B 部分是函数体

举例:Apple::getAppleId

  jButton.addActionListener(e->System.out.println("Hello world"));

一、Java 8 Lambda 表达式

  jButton.addActionListener((e)->System.out.println("Hello world"));

if (appleName.stream(卡塔尔(قطر‎.noneMatch(a -> StringUtils.equalsIgnoreCase(a, "顶级苹果"卡塔尔卡塔尔(قطر‎卡塔尔;

  一时候编写翻译器通过上下文推导出的归来类型与事实上类型不符时须要进类型转变

apples.sort(Comparator.comparing(Apple::getAppleWeight));

  lamba表明式可被看作函数,java.util.function定义了常用的函数式接口

}

c.例子

再有大多不易的使用姿势,比如为空则不打字与印刷能够那样写:

  System.out.println(sign) 也正是run方法的方法体

apples.sort(Comparator

  http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

return "Unknown";

  this.color = color;

List<Apple> apples = Lists.newArrayList();

  该措施是绚烂的意趣(一上马自身联想到了HashMap卡塔尔,还应该有mapToDouble等

return user.get().name;

(3State of Qatar强逼类型转换

那活脱脱是最常用的功力之一,其实lambda表明式的固守,应该就是简单,实际上是用最短的字符,通过品种推导,语法糖等方法去对编写翻译器描述清楚这段代码的效应,那和泛型有一些相同,对于编制程序人士来讲,一定水准上也巩固了编制程序效能和代码可读性。

  public void setColor(String color) {

public static String getName {

 

List<Integer> evens = nums.stream().filter(num -> num % 2 == 0).collect(Collectors.toList;

  JButton jButton = new JButton("123");

weight10.or(apple -> apple.getAppleClassic

(2卡塔尔方法援引

evens.add;

int sum1 = Arrays.stream(oop).reduce(0,(x,y)->x y);

apples.add;

String  String.chars

}

  List<String> colos = shpaeList.stream().map(s->s.getColor()).collect(Collectors.toList());

Map<Long, String> appleIdMap = apples.stream()

  那表时T,U,Tiggo都需求编写翻译器依据上下文实行项目推导

stream实现:

  只有一个办法的接口称为函数式接口

操作系统 1操作系统 2操作系统 3

  public class Shape

apples.stream()

通过无名内部类大家领略compare方法必要五个参数

数学写作 h=g

  数组布局方法援引:TypeName[]::new

.collect(Collectors.toMap(Apple::getAppleId, Apple::getAppleName, -> s.length() > s2.length() ? s : s2));

(4卡塔尔(قطر‎lamba表明式在java对象中的应用

private Float appleWeight;

 

User champion = result.getChampion();

  大家得以将ArrayList<Shape> 调换来ArrayList<String>、Set<String>、HashMap<String,String>,HashMap<String,Shape>以致是更复杂的

谓词复合查询:

  A 部分是流传方法的参数

Function<Float, Float> g = a -> a * 2;

  }

Apple apple = new Apple();

  }

Predicate<Apple> a = apple -> apple.getAppleWeight() > 10;

你应有在此外java对象中品尝接收lamba表达式,通用准则是将以此目的转变到java.util.stream,流有串行并行之分哦!并行流关键字parallelStream

2.可复合,更灵活

  jButton.addActionListener(new ActionListener() {

让我们看看经过Optional加持过后,这么些代码会化为何样体统。

  }

.ifPresent(System.out::println);

  Collectors是搜罗器,Collectors.toList(卡塔尔国,Collectors.toMap(State of Qatar、Collectors.groupingBy等

正规用的lambda表明式:

  s.getColor())

.map(u->u.getName

  {

public static String getChampionName(Competition comp) throws IllegalArgumentException {

  Map<String,Set<Shape>> shapeMapSet = shpaeList.stream().collect(Collectors.groupingBy(s->s.getColor(),Collectors.toSet()));

可以看作&&c

  return 0;

本文由ca88发布,转载请注明来源

关键词: 技能 cabet228亚洲城 苹果 山东 名称