JAVA8-待续
Posted it_worker365
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA8-待续相关的知识,希望对你有一定的参考价值。
1. 函数式编程,因为在并发和时间驱动编程中的优势,函数式编程又逐渐流行起来
以前是实现一个比较器需要实现Comparator接口,并重写compare方法,以下为两种实现方法(类似还有线程,事件等):
class compOldClass implements Comparator<String> { @Override public int compare(String first, String second) { return Integer.compare(first.length(), second.length()); } }
Comparator<String> compOld = new Comparator<String>() { @Override public int compare(String first, String second) { return Integer.compare(first.length(), second.length()); } };
在JDK8中,很多接口添加@FunctionalInterface被声明为函数式接口,JDK8的接口中可以有default和static方法,函数式接口中除了覆盖Object的方法之外,只允许有一个抽象方法
@FunctionalInterface public interface Comparator<T> { ...... int compare(T o1, T o2); boolean equals(Object obj);
...... default Comparator<T> reversed() { return Collections.reverseOrder(this); } ...... ...... public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; } ...... }
JDK8函数方式的Comparator实现
Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length());
自定义测试:就是迭代元素,提供处理方法,进行处理
@FunctionalInterface interface WorkerSep { public void doSthSep(String conent); }
com.timer.WorkerSep workerSep = (x) -> System.out.println(x); List<String> lists = new ArrayList<String>(); lists.add("a"); lists.add("b"); lists.stream().forEach(workerSep::doSthSep);
方法引用(对象::实例方法 / 类::静态方法 / 类::实例方法 / 类::new (支持多个构造器根据参数选择最合适的)):例为调用父类方法,传入线程中执行
/** * Created by itworker365 on 5/19/2017. */ public class Java8ThreadTest { public static void main (String[] args) { ConcurrentWorker concurrentWorker = new ConcurrentWorker(); concurrentWorker.doSth(); } } class Worker { public void doSth() { System.out.print("working......"); } } class ConcurrentWorker extends Worker { public void doSth () { Thread thread = new Thread(super::doSth); thread.start(); } }
2. Stream
例,读文件 拆分字符 打印 , 流处理的时候原字符不变,将输入流处理后传到下一个流处理器直到结束,输出的新字符为处理过后的,换成parallelStream就是并行的了,代码流程为创建一个stream,执行一些列操作,使用一个终止符来终止操作,但执行顺序为print被调用的时候,采取找数据,开始计算。
Collection接口中添加stream方法,可以将任何一个集合转换为stream,即使数组也可以Stream.of(数组),Stream.generate(),Stream.iterator(),Pattern.compile(正则).splistAsStream("****"),Files.lines(path)等
String content = new String(Files.readAllBytes(Paths.get("d:\\a.txt"))); List<String> words = Arrays.asList(content.split(",")); //过滤掉fileter中运算为false的 words.stream().filter(w -> w.startsWith("b")).forEach(System.out::println); //每行都执行以下map里的函数 words.stream().map(String::toUpperCase).map(s -> s.charAt(0)).forEach(System.out::println); //不停输入,直到Limit,skip表示忽略掉前n个元素 Stream.iterate(1, item -> item + 1).limit(10).forEach(System.out::println); //将每一行拆分为一个流添加到原来computer,it-worker ==》computer it worker words.stream().flatMap(article -> Stream.of(article.split("-"))).forEach(System.out::println); //perk显示元素是否被取用,count统计个数,同时局和方法还有max,min,findFirst,findAny,anyMatch等 words.stream().peek(System.out::println).count(); //有状态的转换,排序和去重等操作,需要记录元素状态,进行操作 words.stream().distinct().forEach(System.out::println); //判断是否存在符合条件的元素optional元素还可以ifPresent,map,orElse等添加处理 Optional<String> optional = words.stream().filter(w -> w.startsWith("c")).findAny(); if (optional.isPresent()) { System.out.println("option result: " + optional.get()); } System.out.println("any match: " + words.stream().anyMatch(w -> w.startsWith("c"))); //将stream转为数组 String[] wordArray = words.stream().toArray(String[]::new); //其他收集方式,其他方式类似,需要时查看API Set<String> hashSet = words.stream().collect(Collectors.toSet()); List<String> list = words.stream().collect(Collectors.toList()); TreeSet<String> treeSet = words.stream().collect(Collectors.toCollection(TreeSet<String>::new)); Map<String,String> map = words.stream().collect(Collectors.toMap(x -> x, String::toUpperCase)); String result = words.stream().collect(Collectors.joining("/")); //分组分片 same as String::toUpperCase Map<String, List<String>> splitMap = words.stream().collect(Collectors.groupingBy(x -> x.toUpperCase())); Map<Boolean, List<String>> partitionMap = words.stream().collect(Collectors.partitioningBy(x -> x.toUpperCase().equals("CD"))); //所有字符串concat在一起 String s = words.stream().reduce("", (a, b) -> a.concat(b)); System.out.println("reduce : " + s);
3.日期函数更新
import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAdjuster; /** * Created by itworker365 on 5/19/2017. */ public class Jdk8NewTimmer { public static void main (String[] args) { Instant start = Instant.now(); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } Instant end = Instant.now(); Duration duration = Duration.between(start, end); System.out.println(duration.toMillis()); System.out.println(duration.toMinutes()); LocalDate localDate = LocalDate.now(); System.out.println(localDate.getYear() + " " + localDate.getMonth()); LocalDate localDateMe = LocalDate.of(2008, 8, 8); System.out.println(localDateMe.getYear() + " " + localDateMe.getMonth()); //2017年第十天 LocalDate someDay = localDate.of(2017,1,1).plusDays(10); //可以用.with方法调整不同的作息周期,还可以用withHout等调整对应的具体时间 TemporalAdjuster NEXT_WORKDAY = w -> { LocalDate localDate1 = (LocalDate)w; do { localDate1 = localDate1.plusDays(1); } while (localDate1.getDayOfWeek().getValue() >= 6); return localDate1; }; LocalDate backToWork = localDate.with(NEXT_WORKDAY); System.out.println(backToWork.toString()); //时间根据时区调整 ZonedDateTime zonedDateTime = ZonedDateTime.of(2017,5,1,1,1,1,1, ZoneId.of("America/New_York")); System.out.println(zonedDateTime.toString()); //各种日期信息打印格式模板 String format = DateTimeFormatter.ISO_DATE.format(localDate); System.out.println(format); //变为本土化显示,ofPattern自定义格式。 DateTimeFormatter format1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); System.out.println(format1.format(zonedDateTime)); } }
以上是关于JAVA8-待续的主要内容,如果未能解决你的问题,请参考以下文章
java8 .stream().sorted().filter().map().collect()用法