Heim  >  Artikel  >  Java  >  So drücken Sie die Syntax der funktionalen Ableitung in Java8 Lamdba aus

So drücken Sie die Syntax der funktionalen Ableitung in Java8 Lamdba aus

WBOY
WBOYnach vorne
2023-04-14 19:10:201202Durchsuche

Vorwort

Die Schnittstelle, die nur eine abstrakte Funktion hat, ist die funktionale Schnittstelle, mit der wir Lambdba erstellen können, sie kann jedoch statische Methoden und Standardmethoden enthalten

1, die Syntax des Lambdba-Ausdrucks Schauen Sie sich die Anwendung eines einfachen Lambdba-Ausdrucks an, also beim Erstellen eines Threads

Parameter + Funktionskörper So wie o1 und o2 oben Parameter sind, folgt der Funktionskörper (Apple a) -> a.getColor() (Parameter) -> >{Anweisungen}

CompareTo-Funktionseinführung:

        //创建一个线程,将线程的名字打印出来
        new Thread(()-
       System.out.println(Thread.currentThread().getName())).start();
        Thread.currentThread().join();
        //Thread-0
2, gemeinsame funktionale Schnittstelle

Prädikat boolescher Test (T t)

参数+函数体就像上面的o1 和o2就是参数,后面的就是函数体 (Apple a) -> a.getColor() (parameters)-> expression (parameters) ->{statements}

compareTo函数介绍:

        Comparator<Apple> byColor = new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        };
        listApple.sort(byColor);
        Comparator<Apple> byCOlor2 = (o1,o2) -> o1.getColor().compareTo(o2.getColor());

2,常见的函数式接口

  • Predicate boolean test(T t)

  • Consumer accept(T t)

  • Function469df1b70b2914a3841e0404ec3e3d5d R apple(T,t)

  • Supplier T get()

Predicate :传入一个变量返回一个Boolean值

 // 根据Unicode来判断,返回一个boolean 根据boolean来说明,这两个对象需不需要交换,其实我们可以不用这个CompareTo,我们可以自己写判断条件,只要是返回boolean就行
  public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

Consumer 这个单词中文意思是消费者,既然消费了就不会有返回值Verbraucher akzeptieren (T t)

Funktion469df1b70b2914a3841e0404ec3e3d5d Es wird verbraucht, es wird keinen Rückgabewert geben.

private static List<Apple> filterApple(List<Apple> list, Predicate<Apple> predicate){
        List<Apple> lists = new ArrayList<>();
        for (Apple apple:list){
            if (predicate.test(apple)){
                lists.add(apple);
            }
        }
        return lists;
    }
//使用
        List<Apple> list = filterApple(listApple, apple -> apple.getWeight() > 100);
        System.out.println(list);
//我们发现在 filterApple 函数里面我们传入了两个参数,一个是list 一个是Predicate抽象函数式接口,我们调用这个抽象函数式接口,但是我们不实现它,我们在调用的时候实现它是根据重量来比较,当然我们在调用的时候也可以根据颜色来比较
/**
举一反三:Predicate是传入一个参数进行判断,BiPredicate是传入两个参数进行判断,例如下面的例子
*/

private static List<Apple> filterAppleByBiPredicaTe(List<Apple> apples, BiPredicate<String, Long> predicate){
List<Apple> lists = new ArrayList()
for(Apple apple:apples){
	if (predicate.test(apple.getColor(),apple.getWeight())){
			lists.add(apple)
	}
}
//从上面的例子上来看我们知道BiPredicate传入两个参数,根据颜色和重量来进行比较,我们在调用的时候可以实现者两个参数的比较规则
}

Funktion: Funktion, wir wissen, dass alle Funktionen Ausgänge haben Schauen Sie sich an, wie Sie diese Funktion verwenden

private static void printlnApple(List<Apple> list, Consumer<Apple> consumer){
        if (list.size()>0){
            for (Apple apple:list){
                consumer.accept(apple);
            }
        }
    }
//使用
printlnApple(listApple,apple -> System.out.println(apple.getColor()));
/**
从上面我们可以看到 这个Consumer不会返回任何对象
举一反三: 上面有BiPredicate,那么我们这个Consumer有没有BiConsumer,答案肯定是有的,下面我们来看下BiConsumer的用法
*/
private static void printGreenAppleByBiConsumer(List<Apple> apples, Consumer<Apple, String> consumer){
	if(apples.size()>0){
		for(Apple apple:apples){
			consumer.accept(apple, apple.getColor())
		}
	}
}
//使用
printGreenAppleByBiConsumer(list,(apple, color)->{
	if(apple.getWeight()>100&&color.equal("green")){
		System.out.println(apple);
		}
})
//学习玩上面的例子 我再提一个 LongConsumer,从字面上看,我们知道,它接收的是一个Long类型的,其他的并没什么区别,这里我们就不多啰嗦了

Lieferant:
    Dieses Wort bedeutet „bereitstellen“, d , funktionale Ableitung des Lamdba-Ausdrucks
  • 1, verwenden Sie die statische Methode einer Klasse, um abzuleiten
  • 2, verwenden Sie die Instanzmethode des Objekts, um abzuleiten

 private static Long getWeightList(Apple apple, Function<Apple,Long> function)
    {
        return function.apply(apple);
    }
//使用
  Long green = getWeightList(new Apple("green", 120), Apple::getWeight);
        System.out.println(green);
//在这里我们可以看到,Function函数的apply抽象方法接收一个值,返回一个Long类型的值

//根据上面的经验,想必大家也已经知道了还有 BiFunction 这个函数式接口,没错它只是传进去的参数有两个而已,还是返回一个结果。除此之外还有呢,大家别着急,还有  IntFunction DoubleFunction,这些有什么作用呢,其实可以看成是Function的具体情况  IntFuntion也就是  apply里面的参数是Int类型的 DoubleFunction也就是apply的参数是double类型的,大家再看一下,是不是非常非常的简单呢

Konstruktorableitung:

    private static Apple  getApple(Supplier<Apple> supplier){
       return  supplier.get();
    }
    //使用
        Apple green1 = getApple(() -> new Apple("green", 150));
        System.out.println(green1);
   //有人肯定再想了,这样创建对象那不是脱裤子放屁吗 多此一举,这里只是举一个小例子,在实际开发中肯定不会这样

Beispiel: 🎜sort-Klassifizierung und funktionale Ausdrucksableitung 🎜
    private static void printstr(Consumer<String> consumer, String str){
        consumer.accept(str);
    }
    //使用
        Consumer<String> consumer = a -> System.out.println(a);
        printstr(consumer,"hello world");
   //写成
   		Consumer<String> consumer = System.out::println
   		printstr(consumer,"hello world");
   //思考我们为什么可以写成这样呢,我们看下println的源码
       // public final static PrintStream out = null;,在源码里面我们可以看到PrintStream是一个静态的类,而println是这个静态类下面的方法,因此可以做函数推导
       public void println(String x) {
       //刚好是接收一个参数,没有返回
        synchronized (this) {
            print(x);
            newLine();
        }
    }

```再举个例子

```java
//将字符串转换成数字
int  value = Integer.parseInt("123")
 Function<String, Integer> stringFunction = Integer::parseInt;
 Integer result = stringFunction.apply("123");
 System.out.println(result)
 /**
 很多人看到这里就很纳闷了,怎么冒出一个 Function来了呢,这就是类方法推导,parseInt是Integer的类方法它需要传入一个数,然后输出一个数,这样的模式就是前面我们介绍的Function函数式接口,接着再来看一个例子
 */
 String string = new String("hello");
 Function<Integer, Character> f = string::charAt;
 Character c = f3.apply(4);
 System.out.println(c);

Das obige ist der detaillierte Inhalt vonSo drücken Sie die Syntax der funktionalen Ableitung in Java8 Lamdba aus. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen