糖果派对官方网站_可以赌钱的糖果游戏_手机版
何人还会有越来越好的主意 搜索最重的苹果名称

何人还会有越来越好的主意 搜索最重的苹果名称

作者:操作系统    来源:未知    发布时间:2020-03-20 15:56    浏览量:

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,12State of Qatar卡塔尔国;Appleapple2=newApple("国光",60,"绿","西藏",LocalDate.of(2019,10,18State of Qatar卡塔尔(قطر‎;Appleapple3=newApple("红星",50,"红","新疆",LocalDate.of(2019,9,10State of Qatar卡塔尔;Appleapple4=newApple("黄少校",85,"黄","湖南",LocalDate.of(2019,10,6卡塔尔国卡塔尔;Appleapple5=newApple("嘎啦",35,"绿","青海",LocalDate.of(2019,8,21State of Qatar卡塔尔;Appleapple6=newApple("寒富",40,"红","广东",LocalDate.of(2019,9,20卡塔尔国卡塔尔(قطر‎;ListAppleappleList=newArrayList(卡塔尔;appleList.add(apple1卡塔尔国;appleList.add(apple2卡塔尔(قطر‎;appleList.add(apple3卡塔尔;appleList.add(apple4卡塔尔;appleList.add(apple5State of Qatar;appleList.add(apple6卡塔尔(قطر‎;//寻找最重的苹水果和干果种MapString,IntegerappleVariety=newHashMap(卡塔尔(قطر‎;appleList.stream(State of Qatar.collect(Collectors.groupingBy(Apple::getVarietyState of QatarState of Qatar//依照项目分组[项目名称Apple].forEach((k,v卡塔尔-appleVariety.put(k,v.stream(State of Qatar.collect(Collectors.summingInt(Apple::getWeight卡塔尔国卡塔尔(قطر‎State of Qatar卡塔尔国;OptionalStringappleOpt=appleVariety.keySet(卡塔尔国.stream(卡塔尔(قطر‎.sorted(Comparator.comparing(appleVariety::get卡塔尔国.reversed(卡塔尔国State of Qatar.limit(1LState of Qatar.findFirst(卡塔尔(قطر‎;if(appleOpt.isPresent(卡塔尔(قطر‎State of Qatar{System.out.println("最重的苹果名称:"+appleOpt.get(卡塔尔卡塔尔;}}}结果:最重的苹果名称:黄元帅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+"]";}}

大要一年多事情发生前,笔者对java8的精通还仅限一些片言一字的篇章之上,后来出于对函数式编制程序的志趣,买了本仿照效法书看了一回,然后放在了书架上,后来,当我接手大顾客利用的开支工作今后,java8的有个别工具,对自身的频率有了十分大的晋级换代,由此想记录一下java’8的局地常用项景,作者期待那会化为一个小词典,能让自家免于频繁翻书,但是总能找到自个儿想找的学识。

(-1)前言

何人还会有越来越好的主意 搜索最重的苹果名称。用来比方的model:

  学习lamba表达式是卓殊重大的,你会意识java变的可爱多了。

@Data

(0卡塔尔函数式接口

public class Apple {

  唯有二个方法的接口称为函数式接口

private Long appleId;

  JButton jButton = new JButton("123");

private String appleName;

  jButton.addActionListener(new ActionListener() {

private Float appleWeight;

  @Override

private Integer appleClassic;

  public void actionPerformed(ActionEvent e) {}});

}

等同于

一、Java 8 Lambda 表达式

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

这活脱脱是最常用的功用之一,其实lambda表明式的效应,应该就是轻易,实际上是用最短的字符,通过项目推导,语法糖等方法去对编写翻译器描述清楚这段代码的法力,这和泛型有一些雷同,对于编制程序职员来讲,一定程度上也增加了编制程序成效和代码可读性。

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

常规用的lambda表明式:

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

bb电子糖果派对,process->System.out.println(“this is so cool!”))

  jButton.addActionListener((ActionEvent e)->{System.out.println("Hello world");return;});

比如对苹果重量排序:

a.规则

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

  A->B

for (int i = 1; i < 10; i++) {

  A 部分是传播方法的参数

Apple apple = new Apple();

  B 部分是函数体

apples.add;

  参数类型以致重返类型如若可以透过上下文估量出来可不写

}

   当参数独有二个并且它的类型能够被演绎获知时,该参数列表外面包车型地铁括号能够被略去

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

   当函数体唯有叁个语句且再次来到类型可被演绎时**{}可省略**

反序:

b.例子

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

  Runnable runnable = ()->System.out.println(sign);

净重相同期:比较等级:

  Runnable runnable2 = new Runnable() {

apples.sort(Comparator

  @Override

.comparing(Apple::getAppleWeight)

  public void run() {

.reversed()

  // TODO Auto-generated method stub}

谓词复合查询:

  };

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

  通过无名氏内部类大家清楚**run方法是没有须求参数的**

weight10.or(apple -> apple.getAppleClassic

  System.out.println(sign卡塔尔 约等于run方法的方法体

.and(apple -> StringUtils.equalsIgnoreCase(apple.getAppleName(卡塔尔国, "卓绝苹果"卡塔尔卡塔尔;

 

能够当作&&c

  Comparator<Integer> lol = (x,y)->x-y;

函数复合:

  Comparator<Integer> lol = (x,y)->{System.out.println(x+y);return x-y;};

Function<Apple, Float> f = a -> a.getAppleWeight() + 1;

  new Comparator<Integer>() {

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

  @Override

Function<Apple, Float> h = f.andThen;

  public int compare(Integer o1, Integer o2) {

数学写作 h=g

  // TODO Auto-generated method stub

Function<Apple, Float> g = a -> a.getAppleWeight() + 1;

  return 0;

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

  }

Function<Apple, Float> h = f.compose;

};

数学写作 h=f

经过无名氏内部类我们精晓compare方法须求七个参数

小结:java8实在想传递函数,函数是何等?是四个映射,能够视作x->y,输入x然后映射到值y的长河,

那儿编写翻译通过Comparator<Integer>中Integer推导出参宿类型及重返类型

java无法超脱一切都已经目标的酌量,因而函数式依靠在对象上传递的,由此也是有了上面包车型大巴传教,方法引用,以致函数式接口,让函数随着对象传递,为了函数式编制程序,以致特意写一个接口—对象来传递函数。然则,函数才是顶梁柱。

c.深入

二、Java 8 方法引用

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

主意援用十二分精短,其实也是将艺术作为参数字传送递。使用::域功用符,将一段方法传递。

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

举例:Apple::getAppleId

 

String::subString

  BiFunction<T,U,R>

System.out::println

  T 代表第一个参数

三、Java 8 函数式接口 函数式编程

  U 代表第二个参数

动用java进行函数式编制程序首要就是运用函数式接口,不过函数式接口在java8事情发生前就有一点点了,就比方多线程的runnable,但是8早前是不曾lambda表明式的,所以一定要利用无名内部类,在用过lambda表明式的人看来,那是一定丰腴的,8翻新了lambda表达式,这就使函数式编制程序更进一竿.

  Enclave 代表再次来到值

下一篇:没有了
友情链接: 网站地图
Copyright © 2015-2019 http://www.tk-web.com. bb电子糖果派对有限公司 版权所有