Java8语法基本使用
lambda表达式使用
lambda表达式基本概念
只需要知道lambda表达式主要功能是用来实现接口的和匿名内部类差不多,而且实现的是一个只有一个抽象方法的接口(函数式接口,被@FunctionalInterface注释)
快速入门案例
public class APP1 {
public static void main(String[] args) {
//使用匿名内部类实现Runnable接口
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello java8");
}
}).start();
//使用lambda表达式实现Runnable接口
new Thread(() -> System.out.println("Hello java8 Lambda!!!") ).start();
}
}
基本语法
() -> {} 相当于 一个public void xxx(){ }方法
() -> "hello world" 相当于 一个public String xxx(){ return "hello world"; }方法,返回类型会自动根据返回值设置
() -> { return "hello world"; } 相当于 () -> "hello world"的显式写法
() -> { return "hello world" ; } 相当于 () -> "hello world"的显式写法
int -> { return "hello world" + int ; } 相当于 一个public String xxx(int i){ return "hello world" + i; }
(int,int) -> { return "hello world" + int ; } 相当于 一个public String xxx(int i){ return "hello world" + i; }
注意:
1. ()中的是参数变量名称,可以随意定义
2. (int a,String b) -> a + b,当有两个参数的时候可以给起指定上类型
java8新增的函数式接口
java8新增函的数式接口位于java.util.function包下,以下为常用的几个函数式接口:
Interface Predicate<T>
default Predicate<T> and(Predicate<? super T> other)
Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
static <T> Predicate<T> isEqual(Object targetRef)
Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
default Predicate<T> negate()
Returns a predicate that represents the logical negation of this predicate.
default Predicate<T> or(Predicate<? super T> other)
Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
boolean test(T t) //返回boolean形值
Evaluates this predicate on the given argument.
Interface Consumer<T>
void accept(T t) //无返回值
Performs this operation on the given argument.
default Consumer<T> andThen(Consumer<? super T> after)
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
Interface Function<T,R>
default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
Returns a composed function that first applies this function to its input, and then applies the after function to the result.
R apply(T t) //返回R这个类型的值
Applies this function to the given argument.
default <V> Function<V,R> compose(Function<? super V,? extends T> before)
Returns a composed function that first applies the before function to its input, and then applies this function to the result.
static <T> Function<T,T> identity()
Returns a function that always returns its input argument.
函数式接口的使用例子
下面的例子只是为了举例子而举例子,具体怎么用还是根据情况需要而定,当java api提供的函数式接口不够用,那么就可以自定义;
自定义函数式接口使用例子:
public class APP2 {
public static void main(String[] args) {
/**
* 这里的a和b实质值就是1和2
* (a,b) -> a + b相当于一个Count接口的实现,count方法体就是 return a + b;
* 所以,count.count(a, b)就return了一个3
*/
count(1,2,(a,b) -> a + b);
}
//计算a+b
public static void count(int a, int b, Count count) {
System.out.println(count.count(a, b));
}
}
//被该注解注释的接口只能有一个抽象方法
@FunctionalInterface
interface Count{
int count(int a, int b);
}
Predicate
import java.util.function.Predicate;
public class APP3 {
public static void main(String[] args) {
checkAge( new Person("谭迪", 21), (Person) -> Person.age>=18 );
}
//判断person是否成年
public static void checkAge(Person person, Predicate<Person> predicate) {
if(predicate.test(person)) {
System.out.println(person.name + "你已经成年,时光飞逝,请你珍惜接下来的日子。");
} else {
System.out.println(person.name + "你还是个宝宝!!!");
}
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Consumer
import java.util.function.Consumer;
public class APP4 {
public static void main(String[] args) {
//lambda表达式如果参数只有一个,()可以省去,但无参数时要加上
sayHello("hello", String -> {
for(int i=0; i<10; i++) {
System.out.println(String + "-" +i);
}
} );
}
//打印10次 hello java
public static void sayHello(String str,Consumer consumer) {
consumer.accept(str);
}
}
Function
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class APP5 {
public static void main(String[] args) {
List<Apple> list = new ArrayList<>();
list.add(new Apple("绿色"));
list.add(new Apple("红色"));
list.add(new Apple("蓝色"));
list.add(new Apple("红色"));
list.add(new Apple("黑色"));
List<Apple> redApples = returnRedApple(list, List -> {
List<Apple> newList = new ArrayList<>();
for (Apple apple : list) {
if("红色".equals(apple.color)) {
newList.add(apple);
}
}
return newList;
});
System.out.println("红苹果有" + redApples.size() + "个");
}
//选出红色的苹果
public static List<Apple> returnRedApple(List<Apple> source, Function<List<Apple>,List<Apple>> function){
return function.apply(source);
}
}
class Apple{
String color;
public Apple(String color) {
this.color = color;
}
}
方法引用
当你了解了lambda表达式,那么再来了解方法引用其实是很容易的,方法引用也是对函数式接口的实现;
快速入门例子:
//Function<String,Integer> function = (String str) -> Integer.parseInt(str); //lambda表达式
/**说明:
* Function<String,Integer> function = Integer::parseInt;
* Integer::parseInt对应的类型一定是Function<T,R>中的R类型,因为Function<T,R>中的抽象方法是 R apply(T t)
*/
Function<String,Integer> function = Integer::parseInt; //方法调用
Integer apply = function.apply("100");
System.out.println(apply); //100
图解:
例子2:
public class APP1 {
public static void main(String[] args) {
//利用方法引用调用构造方法
Function<String,Apple> function = Apple::new;
Apple apple = function.apply("紫色");
System.out.println(apple.color);
}
}
class Apple{
String color;
public Apple(String color) {
this.color = color;
}
}
接口新特性
静态方法
public class APP3{
public static void main(String[] args) {
Test test = System.out::println;
test.sayHello("谭迪"); //实现方法
test.defaultSayHello("小明"); //默认方法
}
}
interface Test {
//默认方法:需要实现了该接口才能调用该方法
default void defaultSayHello(String name) {
System.out.println(name + "你好!!!");
}
void sayHello(String name);
}
默认方法
public class APP3{
public static void main(String[] args) {
Test.sayHello("谭迪");
}
}
interface Test {
//静态方法:注意接口中的静态方法只能是public和默认修饰
static void sayHello(String name) {
System.out.println(name + "你好!!!");
}
}
Stream API(流)
什么是流?
你可以将它理解成一个遍历数据集合的高级迭代器 -- 来自Java8实战
快速入门例子
public class APP4 {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三", 20,"广东"));
list.add(new Person("李四", 16,"四川"));
list.add(new Person("王五", 29,"广东"));
list.add(new Person("赵六", 11,"广西"));
list.add(new Person("孙7", 30,"广东"));
/**
* 使用Strem筛选出年龄大于18居住在广州的人并且如果经过筛选后超过3个人那么只打印出这前人的名字
*/
list.stream()
.filter( Person -> Person.age>18 ) //返回岁数大于18的
.filter( Person -> "广东".equals(Person.address) ) //返回住在广东的
.map( Person -> Person.name ) //提取出他们的名字
.limit(3) //截取前3条数据
.forEach(Person -> System.out.println(Person) ); //将过来完的Person打印出来,注意:只能够迭代一次,迭代完流会关闭,即不能再做流操作
}
}
class Person{
String name;
int age;
String address;
Person(String name, int age, String address){
this.name = name;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
Stream接口方法
boolean allMatch(Predicate<? super T> predicate)
Returns whether all elements of this stream match the provided predicate.
boolean anyMatch(Predicate<? super T> predicate)
Returns whether any elements of this stream match the provided predicate.
static <T> Stream.Builder<T> builder()
Returns a builder for a Stream.
<R,A> R collect(Collector<? super T,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a Collector.
<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
Performs a mutable reduction operation on the elements of this stream.
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.
long count()
Returns the count of elements in this stream.
Stream<T> distinct()
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
static <T> Stream<T> empty()
Returns an empty sequential Stream.
Stream<T> filter(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.
Optional<T> findAny()
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
Optional<T> findFirst()
Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
Returns an DoubleStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
Returns an LongStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
void forEach(Consumer<? super T> action)
Performs an action for each element of this stream.
void forEachOrdered(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
static <T> Stream<T> generate(Supplier<T> s)
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
static <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
Stream<T> limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
<R> Stream<R> map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
IntStream mapToInt(ToIntFunction<? super T> mapper)
Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
LongStream mapToLong(ToLongFunction<? super T> mapper)
Returns a LongStream consisting of the results of applying the given function to the elements of this stream.
Optional<T> max(Comparator<? super T> comparator)
Returns the maximum element of this stream according to the provided Comparator.
Optional<T> min(Comparator<? super T> comparator)
Returns the minimum element of this stream according to the provided Comparator.
boolean noneMatch(Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided predicate.
static <T> Stream<T> of(T... values)
Returns a sequential ordered stream whose elements are the specified values.
static <T> Stream<T> of(T t)
Returns a sequential Stream containing a single element.
Stream<T> peek(Consumer<? super T> action)
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
Optional<T> reduce(BinaryOperator<T> accumulator)
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
T reduce(T identity, BinaryOperator<T> accumulator)
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
<U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
Stream<T> skip(long n)
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
Stream<T> sorted()
Returns a stream consisting of the elements of this stream, sorted according to natural order.
Stream<T> sorted(Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
Object[] toArray()
Returns an array containing the elements of this stream.
<A> A[] toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.
构建流的方法
//方法1:从容器中获取
//Collection.stream();
//方法2:使用Stream接口静态方法of方法获取
Stream<String> s = Stream.of("java","java7","java8","java9");
//方法3:使用数组获取
int[] arr = {1,2,3,4,5};
IntStream i = (IntStream)Arrays.stream(arr);
System.out.println( i.sum() ); //15
//方法4:从io流中获取
Stream<String> lines = Files.lines(Paths.get("D:\\", "123.txt"));
lines.forEach( str -> System.out.println(str) ); //打印文件中的内容
时间: 2024-10-14 23:11:37