- 1.接口默认方法
java8 允许我们给一个接口添加一个非抽象方法的实现,使用关键字 defalut即可。
public interface IJDK8Test {
default void sys(){
System.out.println("接口方法默认实现");
}
}
.2.函数式接口
函数式接口是指包含一个抽象方法的接口, 添加@FunctionalInterface注解,则会检测是否是函数式接口。
@FunctionalInterface
public interface IJDK8Functional {
public int convert(String m);
}
IJDK8Functional F=(a)->Integer.valueOf(a);
int result=F.convert("1234");
System.out.println(result);
- 3.对方法和构造函数引用
//静态方法引用
IJDK8Functional F=Integer::valueOf;
int result=F.convert("1234");
System.out.println(result);
//静态方法引用
IJDK8Functional F=Integer::valueOf;
int result=F.convert("1234");
System.out.println(result);
IJDK8Test T=new JDK8TestImpl();
//对象方法调用
IJDK8Functional F=T::con;
int result=F.convert("1234");
System.out.println(result);
//类
class Person {
String firstName;
String lastName;
Person() {}
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
//函数式接口
interface PersonFactory<P extends Person> {
P create(String firstName, String lastName);
}
//对构造方法的调用
PersonFactory<Person> personFactory = Person::new;
Person person = personFactory.create("Peter", "Parker");
-
- 4.Predicate接口
Predicate接口只有一个参数返回boolean类型,
Predicate<String> p=(s)->s.length()>0;
System.out.println(p.test("1"));
- 5.Supplier接口
Supplier<Person> personSupplier = Person::new;
personSupplier.get(); // new Person
- 6.Function接口
Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
backToString.apply("123"); // "123"
-7. Stream接口
java.util.stream表示能应用在一组元素上一次执行的操作序列。Stream操作分为中间操作或者最终操作,中间操作则返回Stream本身,最终操作则返回一特定类型的计算结果。
Filter过滤
过滤通过一个Predicate接口来过滤并只保留符合条件的元素,该操作属于中间操作,
Sort 排序
排序 是一个中间操作,可以指定Comparator
stringCollection
.stream()
.sorted((a,b)->a.compareto(b))
.filter((s) -> s.startsWith("a"))
.forEach(System.out::println);
需要注意的是,排序只创建了一个排列好后的Stream,而不会影响原有的数据源.
map映射
将元素通过指定function接口依次将元素转换成其他对象,
stringCollection.
stream().
filter((s)->s.startsWith("a")).
sorted((a,b)->a.compareTo(b)).
map(String::toUpperCase).
forEach(System.out::println);
Match 匹配
Stream提供很多匹配,允许检测指定的匹配是否匹配整个Stream.该操作是最终操作。
System.out.println(stringCollection.
stream().
filter((s)->s.startsWith("a")).
sorted((a,b)->a.compareTo(b)).
map(String::toUpperCase).
allMatch((s)->s.startsWith("A")));
Count计数
计数是一个最终操作,返回Stream中元素的个数,返回值类型是long。
Reduce规约
最终操作,允许通过指定的函数来将Stream的元素规约为一个元素最终接口由Optional接口表示。
Optional<String> reduced =
stringCollection
.stream()
.sorted()
.reduce((s1, s2) -> s1 + "#" + s2);
reduced.ifPresent(System.out::println);
并行Streams
int max=1000000;
List<String> values=new ArrayList<String>();
for(int i=0;i<max;i++){
UUID uuid=UUID.randomUUID();
values.add(uuid.toString());
}
long start1=System.currentTimeMillis();
values.stream().sorted((a,b)->a.compareTo(b)).count();
long end1=System.currentTimeMillis();
System.out.println(end1-start1);
long start=System.currentTimeMillis();
values.stream().parallel().sorted((a,b)->a.compareTo(b)).count();
long end=System.currentTimeMillis();
System.out.println(end-start);
long start2=System.currentTimeMillis();
结果显示,并行比串行要快50%左右。
-8. DateApi
JAVA8 在java.time下包含了一组新的时间日期API.
//clock
Clock clo=Clock.systemDefaultZone();
long mil=clo.millis();
System.out.println(mil);
//时区
System.out.println(ZoneId.getAvailableZoneIds());
//LocalTime
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
LocalTime now1 = LocalTime.now(zone1);
LocalTime now2 = LocalTime.now(zone2);
System.out.println(now1);
System.out.println(now2);
long hourBetween=ChronoUnit.HOURS.between(now1, now2);
long minuteBetween=ChronoUnit.MINUTES.between(now1, now2);
System.out.println(hourBetween);
System.out.println(minuteBetween);
//LocalDate
LocalDate today=LocalDate.now();
LocalDate tomorrow=today.plus(1, ChronoUnit.DAYS);
System.out.println(tomorrow);
LocalDate yest=today.minus(1, ChronoUnit.DAYS);
System.out.println(yest);
System.out.println(yest.getDayOfMonth());
//DateTimeFormatter LocalDateTime
DateTimeFormatter forma=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime todyTime=LocalDateTime.now();
String dat=forma.format(todyTime);
System.out.println(dat);