Java8新特性
Lambda
System.out.println("foreach-less1.8");
List<String> strings = Arrays.asList("a","b","c","c"); for (String str:strings) { System.out.println(str); } System.out.println("-------"); System.out.println("foreach 1.8");
strings.forEach(str -> System.out.println(str));
|
Stream
strings.stream() .distinct() .limit(2) .filter(str -> str == "a") .forEach(str -> System.out.println(str));
System.out.println("==========="); System.out.println("peek"); strings.parallelStream() .skip(2) .forEach(str -> System.out.println(str)); System.out.println("==========="); distinctPrimary("12","23","12","23");
|
public static void distinctPrimary(String...numbers) { List<String> strings = Arrays.asList(numbers); List<Integer> collect = strings.parallelStream() .map(e -> new Integer(e))
.distinct() .collect(Collectors.toList()); System.out.println(collect); }
|
Interface
public interface Animal { void run();
default void jump(){ System.out.println(this.getClass().getSimpleName()+ " jump"); }
static void eat(){ System.out.println(Animal.class.getSimpleName()+" eat"); } }
|
LocalDate & LocalDateTime
public class LocalDateAndTimeAndDateTime {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()); }
public static void test1(){ LocalDateTime localDateTime1 = LocalDateTime.parse("2016-09-06T06:00:00"); LocalDateTime localDateTime2 = LocalDateTime.parse("2016-09-06T07:00:00");
long time1 = localDateTime1.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); long time2 = localDateTime2.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static void localdate(){ LocalDate today = LocalDate.now(); System.out.println(today);
LocalDate someday = LocalDate.of(2016,8,9);
LocalDate withstr = LocalDate.parse("2016-08-09");
if (someday.equals(withstr)) System.out.println(true); else System.out.println(false);
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); LocalDate firstMondayOf2015 = LocalDate.parse("2016-09-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); }
public static void localtime() { LocalTime now = LocalTime.now(); System.out.println(now);
LocalTime time = LocalTime.now().withNano(0); System.out.println(time);
LocalTime zero = LocalTime.of(0, 0, 0); LocalTime mid = LocalTime.parse("12:00:00"); System.out.println(zero); System.out.println(mid);
}
public static void localdatetime() { long start = System.currentTimeMillis();
int i = 5; System.out.println(System.currentTimeMillis()-start); } }
|
源码
源码已发布到了Github上,欢迎Star
本文标题:Java8新特性
文章作者:Shea
原始链接:https://di1shuai.com/Java8新特性.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!