当前位置: 首页 > news >正文

jsp可以做网站吗企业策划推广公司

jsp可以做网站吗,企业策划推广公司,wordpress搬家后403,win7 asp.net网站架设原来Lambda表达式是这样写的 lambda 是一个匿名函数,我们可以把 lambda 表达式理解为是一段可以传递的代码。 lambda 简明地将代码或方法作为参数传递进去执行。 函数式编程:核心是把函数作为值。 函数式接口 :只有一个抽象方法的接口称之…

原来Lambda表达式是这样写的

lambda 是一个匿名函数,我们可以把 lambda 表达式理解为是一段可以传递的代码。

lambda 简明地将代码或方法作为参数传递进去执行。

函数式编程:核心是把函数作为值。

函数式接口 :只有一个抽象方法的接口称之为函数式接口。

函数式接口可以使用@FunctionalInterface进行注解。

lambda表达式拆分为两部分:

  • 左侧:lambda 表达式的参数列表

  • 右侧:lambda 表达式中所需要执行的功能,即 lambda 体

Lambda 表达式非常方便,在项目中一般在 stream 编程中用的比较多。

package com.lambda;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;public class Test1 {public static void main(String[] args) {List<Student> studentList = new ArrayList<>();studentList.add(new Student(1, "tom"));studentList.add(new Student(2, "marry"));// Collectors.toMap的用法有很多Map<Integer, String> map = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName));// {1=tom, 2=marry}System.out.println(map);}}class Student {private int id;private String name;public Student() {}public Student(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public String getName() {return name;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}
}

理解一个 Lambda 表达式就三步:

1、确认 Lambda 表达式的类型

2、找到要实现的方法

3、实现这个方法

就这三步,没其他的了。

1、确认 Lambda 表达式的类型

能用 Lambda 表达式来表示的类型,必须是一个函数式接口,而函数式接口,就是只有一个抽象方法的接口。

例如 JDK 中 Runnable 接口:

@FunctionalInterface
public interface Runnable {public abstract void run();
}

这就是一个标准的函数式接口,因为只有一个抽象方法,而且这个接口上有个注解 @FunctionalInterface

这个仅仅是在编译期帮你检查你这个接口是否符合函数式接口的条件,比如你没有任何抽象方法,或者有多个抽象

方法,编译是无法通过的。

再稍稍复杂一点,Java 8 之后接口中是允许使用默认方法静态方法的,而这些都不算抽象方法,所以也可以加在

函数式接口里。看看你可能不太熟悉又有点似曾相识的一个接口。

@FunctionalInterface
public interface Consumer<T> {void accept(T t);default Consumer<T> andThen(Consumer<? super T> after) {}
}

只有一个抽象方法,还有一个默认方法(方法体的代码省略了),这个也不影响它是个函数式接口。再看一个更复杂

的,多了静态方法,这同样也是个函数式接口,因为它仍然只有一个抽象方法。

@FunctionalInterface
public interface Predicate<T> {boolean test(T t);default Predicate<T> and(Predicate<? super T> other) {...}default Predicate<T> negate() {...}default Predicate<T> or(Predicate<? super T> other) {...}static <T> Predicate<T> isEqual(Object targetRef) {...}static <T> Predicate<T> not(Predicate<? super T> target) {...}
}

先不用管这些方法都是干嘛的,这些类在 Stream 设计的方法中比比皆是,我们就先记住这么一句话,Lambda表

达式需要的类型为函数式接口,函数式接口里只有一个抽象方法,就够了,以上三个例子都属于函数式接口。

2、找到要实现的方法

Lambda 表达式就是实现一个方法,什么方法呢?就是刚刚那些函数式接口中的抽象方法。

那就太简单了,因为函数式接口有且只有一个抽象方法,找到它就行了。我们尝试把刚刚那几个函数式接口的抽象

方法找到。

@FunctionalInterface
public interface Runnable {public abstract void run();
}@FunctionalInterface
public interface Consumer<T> {void accept(T t);default Consumer<T> andThen(Consumer<? super T> after) {...}
}@FunctionalInterface
public interface Predicate<T> {boolean test(T t);default Predicate<T> and(Predicate<? super T> other) {...}default Predicate<T> negate() {...}default Predicate<T> or(Predicate<? super T> other) {...}static <T> Predicate<T> isEqual(Object targetRef) {...}static <T> Predicate<T> not(Predicate<? super T> target) {...}
}

3、实现这个方法

Lambda 表达式就是要实现这个抽象方法,如果不用 Lambda 表达式,你一定知道用匿名类如何去实现吧?比如

我们实现刚刚 Predicate 接口的匿名类。

Predicate<String> predicate = new Predicate<String>() {@Overridepublic boolean test(String s) {return s.length() != 0;}
};

3.1 单个入参

那如果换成 Lambda 表达式呢?就像这样。

Predicate<String> predicate = (String s) -> {return s.length() != 0;
};

看出来了么?这个 Lambda 语法由三部分组成:

1、参数块:就是前面的 (String s),就是简单地把要实现的抽象方法的参数原封不动写在这。

2、小箭头:就是 -> 这个符号。

3、代码块:就是要实现的方法原封不动写在这。

首先看参数快部分,(String s) 里面的类型信息是多余的,因为完全可以由编译器推导,去掉它。

注意:lambda 表达式的参数类型可以省略不写,因为 jvm 编译器可以从上下文推断出数据类型。

即“类型推断”如果要在参数里面写数据类型,都要写上。

Predicate<String> predicate = (s) -> {return s.length() != 0;
};

当只有一个参数时,括号也可以去掉。

Predicate<String> predicate = s -> {return s.length() !=0;
};

再看代码块部分,方法体中只有一行代码,可以把花括号和 return 关键字都去掉。

Predicate<String> p = s -> s.length() != 0;

3.2 无入参

来,再让我们实现一个 Runnable 接口。

@FunctionalInterface
public interface Runnable {public abstract void run();
}
Runnable r = () -> System.out.println("I am running");

你看,这个方法没有入参,所以前面括号里的参数就没有了,这种情况下括号就不能省略。

通常我们快速新建一个线程并启动时,是不是像如下的写法,熟悉吧?

new Thread(() -> System.out.println("I am running")).start();

3.3 多个入参

之前我们只尝试了一个入参,接下来我们看看多个入参的。

@FunctionalInterface
public interface BiConsumer<T, U> {void accept(T t, U u);
}

然后看看一个用法,是不是一目了然。

BiConsumer<Random, Integer> randomNumberPrinter = (random, number) -> {for (int i = 0; i < number; i++) {System.out.println("next random = " + random.nextInt());}
};
randomNumberPrinter.accept(new Random(314L), 5);

3.4 带返回值

刚刚只是多个入参,那我们再加个返回值:

@FunctionalInterface
public interface BiFunction<T, U, R> {R apply(T t, U u);
}
BiFunction<String, String, Integer> findWordInSentence =(word, sentence) -> sentence.indexOf(word);
Integer result = findWordInSentence.apply("e","hello");
// 1
System.out.println(result);

4、四大核心函数式接口

其实函数式接口里那个抽象方法,无非就是入参的个数,以及返回值的类型。入参的个数可以是一个或者两个,返

回值可以是 void,或者 boolean,或者一个类型。那这些种情况的排列组合,就是 JDK 给我们提供的

java.util.function 包下的类。

别看晕了,我们分分类就好了。可以注意到很多类前缀是 Int,Long,Double 之类的,这其实是指定了入参的特

定类型,而不再是一个可以由用户自定义的泛型,比如说 DoubleFunction。

@FunctionalInterface
public interface DoubleFunction<R> {R apply(double value);
}

这完全可以由更自由的函数式接口 Function 来实现。

@FunctionalInterface
public interface Function<T, R> {R apply(T t);
}

函数式接口几个简单分类就是:

supplier: 没有入参,有返回值。

function: 有入参,有返回值。

consumer: 有入参,无返回值。

predicate: 有入参,返回 boolean 值。

Consumer: 消费性接口,void accept(T t)。

Supplier: 共给性接口,T get()。

Function<T,R>: 函数性接口,T代表参数,R代表返回值,R apply(T t)。

Predicate: 断言性接口,boolean test(T t)。

其实就是给我们提供了一个函数的模板,区别仅仅是入参返参个数的排列组合。

package com.lambda;import java.util.function.Consumer;public class Test2 {public static void main(String[] args) {happy(10000, (money) -> System.out.println("happy消费" + money + "元"));}public static void happy(double money, Consumer<Double> con) {con.accept(money);}
}

5、Stream编程

下面这段代码如果你项目中有用 stream 编程那肯定很熟悉,有一个 Student 的 list,你想把它转换成一个 map,

key 是 student 对象的 id,value 就是 student 对象本身。

package com.lambda;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;public class Test3 {public static void main(String[] args) {List<Student> studentList = new ArrayList<>();studentList.add(new Student(1, "tom"));studentList.add(new Student(2, "marry"));// Collectors.toMap的用法有很多Map<Integer, Student> map = studentList.stream().collect(Collectors.toMap(Student::getId, a -> a, (a, b) -> a));// {1=com.lambda.Student@2f4d3709, 2=com.lambda.Student@4e50df2e}System.out.println(map);}
}

把 Lambda 表达式的部分提取出来。

Collectors.toMap(Student::getId, a -> a, (a, b) -> a)

由于我们还没见过 :: 这种形式,先打回原样,这里只是让你预热一下。

Collectors.toMap(a -> a.getId(), a -> a, (a, b) -> a)

为什么它被写成这个样子呢?我们看下 Collectors.toMap 这个方法的定义就明白了。

public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends U> valueMapper,BinaryOperator<U> mergeFunction) {return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}

看,入参有三个,分别是 Function,Function,BinaryOperator,其中 BinaryOperator 只是继承了 BiFunction

并扩展了几个方法,我们没有用到,所以不妨就把它当做 BiFunction。还记得 Function 和 BiFunction 吧?

Function R apply(T t)
BiFunction R apply(T t, U u)

第一个参数 a -> a.getId() 就是 R apply(T t) 的实现,入参是 Student 类型的对象 a,返回 a.getId()。

第二个参数 a -> a 也是 R apply(T t) 的实现,入参是 Student 类型的 a,返回 a 本身。

第三个参数 (a, b) -> a 是 R apply(T t, U u) 的实现,入参是Student 类型的 a 和 b,返回是第一个入参 a,

Stream 里把它用作当两个对象 ,a 和 b 的 key 相同时,value 就取第一个元素 a,其中第二个参数 a -> a 在

Stream 里表示从 list 转为 map 时的 value 值,就用原来的对象自己,你肯定还见过这样的写法。

Collectors.toMap(a -> a.getId(), Function.identity(), (a, b) -> a)

为什么可以这样写,给你看 Function 类的全貌你就明白了。

@FunctionalInterface
public interface Function<T, R> {R apply(T t);default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}static <T> Function<T, T> identity() {return t -> t;}
}

看到了吧,identity 这个方法,就是帮我们把表达式给实现了,就不用我们自己写了,其实就是包了个方法。这回

知道一个函数式接口,为什么有好多还要包含一堆默认方法和静态方法了吧?就是干这个事用的。

我们再来试一个,Predicate 里面有这样一个默认方法 and。

@FunctionalInterface
public interface Predicate<T> {boolean test(T t);default Predicate<T> and(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) -> test(t) && other.test(t);}default Predicate<T> negate() {return (t) -> !test(t);}default Predicate<T> or(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) -> test(t) || other.test(t);}static <T> Predicate<T> isEqual(Object targetRef) {return (null == targetRef)? Objects::isNull: object -> targetRef.equals(object);}
}

它能干嘛用呢?我来告诉你,如果没有这个方法,有一段代码你可能会这样写。

Predicate<String> p = s -> (s != null) && !s.isEmpty() && s.length() < 5;

如果利用上这个方法,就可以变成如下这种优雅形式。

Predicate<String> nonNull = s -> s != null;
Predicate<String> nonEmpty = s -> s.isEmpty();
Predicate<String> shorterThan5 = s -> s.length() < 5;
Predicate<String> p = nonNull.and(nonEmpty).and(shorterThan5);

6、Lambda方法引用

主要有三种语法格式:

对象::实例方法名

类::静态方法名

类::实例方法名

那我们回过头再看刚刚的 Student::getId 这种写法。当方法体中只有一个方法调用时,就可以作这样的简化。

比如这个 a -> a.getId() 就只是对 Student 对象上 getId() 这个方法的调用,那么就可以写成 Student::getId 这种形

式。

package com.lambda;import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;public class Test9 {public static void main(String[] args) {// 类::实例方法名Function<User, String> n = User::getName;Function<User, Integer> a = User::getAge;// 类::静态方法名Function<String, String> func1 = User::concat2;Supplier<String> func2 = User::getAddress;// 对象::实例方法名User user = new User();Function<String, String> func3 = user::concat1;Supplier<String> func4 = user::getSchool;// 无参构造方法引用Supplier<Student> supplier = Student::new;Student student = supplier.get();student.setName("tom");student.setId(10);// com.lambda.Student@5b480cf9System.out.println(student);Supplier<Student> supplier2 = () -> (new Student());Student student2 = supplier2.get();student2.setName("tom");student2.setId(10);// com.lambda.Student@723279cfSystem.out.println(student2);// 有参构造方法引用BiFunction<Integer, String, Student> biFunction = Student::new;Student stu = biFunction.apply(11, "marry");// com.lambda.Student@b4c966aSystem.out.println(stu);BiFunction<Integer, String, Student> bf = (id, name) -> new Student(id, name);Student s = bf.apply(12, "john");// com.lambda.Student@4e50df2eSystem.out.println(s);// 自定义有参构造函数StudentBuilder sb1 = ((id, name) -> new Student(id, name));Student s1 = sb1.build(13, "xm");// com.lambda.Student@7cc355beSystem.out.println(s1);StudentBuilder sb2 = Student::new;Student s2 = sb2.build(14, "xh");// com.lambda.Student@12edcd21System.out.println(s2);// 自定义无参构造函数StudentBuilderNoArgs sb3 = Student::new;Student s3 = sb3.build();// com.lambda.Student@5b6f7412System.out.println(s3);StudentBuilderNoArgs sb4 = () -> (new Student());Student s4 = sb4.build();// com.lambda.Student@312b1daeSystem.out.println(s4);}
}class User {private static int id;private String name;public static int getId() {return id;}public static void setId(int id) {User.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String concat1(String str) {return str + "!";}public static String concat2(String str) {return str + "!";}public int getAge() {return 12;}public static String getAddress() {return "天津";}public String getSchool() {return "小学";}
}interface StudentBuilder {Student build(int id, String name);
}interface StudentBuilderNoArgs {Student build();
}

再看几个例子:

Function<String, Integer> toLength1 = s -> s.length();
Function<String, Integer> toLength2 = String::length;
Function<User, String> getName = user -> user.getName();
Function<User, String> toLength = User::getName;

如果是构造方法的话,也可以简化。

Supplier<List<String>> newListOfStrings1 = () -> new ArrayList<>();
Supplier<List<String>> newListOfStrings2 = ArrayList::new;
// ClassName::new
Supplier<String> sup = () -> new String();
Supplier<String> sup2 = String::new;
String str = sup.get();
String str2 = sup2.get();
// false
System.out.println(str == str2);
// 对象::实例方法名
Consumer<String> con = (x) -> System.out.println(x);
con.accept("hello");
Consumer<String> con2 = System.out::println;
con2.accept("world");
// 类::静态方法名
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
Comparator<Integer> com2 = Integer::compare;
System.out.println(com.compare(1, 2));
System.out.println(com2.compare(1, 2));
// 类::实例方法名
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
System.out.println(bp.test("a", "a"));
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp2.test("a", "a"));

7、Lambda例子

实例1:

package com.lambda;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;public class Test6 {public static List<Employee> list = Arrays.asList(new Employee("张三", 10, 1), new Employee("里斯", 20, 1), new Employee("王五", 16, 1), new Employee("二三", 30, 1));public static List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {List<Employee> emps = new ArrayList<>();for (Employee employee : list) {if (mp.test(employee)) {emps.add(employee);}}return emps;}public static void main(String[] args) {test1();test2();}public static void test1() {//需要使用自定义的方法List<Employee> list2 = filterEmployee(list, (e) -> e.getAge() >= 15);list2.stream().map(Employee::getName).forEach(System.out::println);}public static void test2() {//可以使用stream进行list集合的过滤  不使用自定义接口List<Employee> list2 = list.stream().filter((e) -> e.getAge() >= 15).collect(Collectors.toList());list2.stream().map(Employee::getName).forEach(System.out::println);}
}class Employee {private String name;private int age;private double salary;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Employee(String name, int age, double salary) {this.name = name;this.age = age;this.salary = salary;}
}interface MyPredicate<T> {boolean test(T t);
}
# 程序输出
里斯
王五
二三
里斯
王五
二三

实例2:

创建一个MyFun接口使用@FunctionalInterface注解,并创建一个抽象方法Integer getValue(Integer num);在

Test类对变量进行某种操作。

package com.lambda;@FunctionalInterface
interface MyFun {Integer getValue(Integer num);
}public class Test7 {public static void test() {int a = 10;System.out.println(a);a = operation(a, num -> ++num);System.out.println(a);}public static void main(String[] args) {test();}/*** param1 num : 传入的整形数* param2 mf : 实现某种方式对 整形数 进行操作。**/public static Integer operation(Integer num, MyFun mf) {return mf.getValue(num);}
}
# 程序输出
10
11
package com.lambda;import java.util.Arrays;
import java.util.Collections;
import java.util.List;public class Test8 {public static List<Employee> list = Arrays.asList(new Employee("张三", 10, 1),new Employee("里斯", 20, 1),new Employee("王五", 16, 1),new Employee("二三", 30, 1));public static void test() {Collections.sort(list, (e1, e2) -> {if (e1.getAge() == e2.getAge()) {return e1.getName().compareTo(e2.getName());} else {//比较年龄大小return Integer.compare(e1.getAge(), e2.getAge());}});for (Employee e : list) {System.out.println(e);}}public static void main(String[] args) {test();}
}
# 程序输出
com.lambda.Employee@404b9385
com.lambda.Employee@6d311334
com.lambda.Employee@682a0b20
com.lambda.Employee@3d075dc0
http://www.dt0577.cn/news/7597.html

相关文章:

  • 西宁做网站君博推荐包就业的培训学校
  • 江西公共资源交易网seo是什么化学名称
  • 网站建站网站设计公司搜索指数的数据来源是什么
  • 交通运输行业网站建设网络推广运营主要做什么
  • 石家庄优化公司哪家好南宁优化网站收费
  • 网站新闻编辑怎么做查询网官网
  • 做任务 网站百度图片搜索
  • 红河优才网站建设百度账户代运营
  • 网店美工是什么seo点击软件手机
  • 企业创建网站的途径都有啥如何创建网站的快捷方式
  • 腾讯云网站搭建百度公司高管排名
  • 网站建设投资seo推广优化官网
  • 嘉兴建网站进一步优化落实
  • 低功耗集成主板做网站手机流畅优化软件
  • 丽水市莲都区建设局网站关键词热度分析工具
  • 国外网站推广宣传百度首页纯净版怎么设置
  • 文成网站制作全网关键词云在哪里看
  • 网站开发需要的语言软文的概念是什么
  • 网站建设布为网做网站的软件有哪些
  • 正规公司简历模板汕头seo排名
  • 网站开发网络网店运营
  • 合肥做网站怎么样网络优化需要哪些知识
  • 外贸企业网站功能要求网络媒体有哪些
  • 网站需要访客上传图片该怎么做外包seo公司
  • 往网站上传照片怎么做比百度好用的搜索引擎
  • 网站域名必须备案吗百度网盘链接
  • 推荐一些做网站网络公司b站在线观看人数在哪
  • 太原网站运营优化500强企业seo服务商
  • 局域网网站建设seo站内优化和站外优化
  • 海口网站建设费用网站关键词优化排名外包