Java8 十大新特性
来源:互联网 发布:网络营销策划书的步骤 编辑:程序博客网 时间:2024/04/29 16:17
1、lambda表达式
1)对象 =(变量…) ->{
方法实现
}
2)实例:
public class LambdaDemo{ public static void main(String[] args){ System.out.println("kk"); Runnable r =()->{ int i = 0; System.out.println("i="+(i++)); }; Action action = (String content)->{ System.out.println(content); }; action.execute("haha"); } static interface Action{ void execute(String content); }}
2、Stream泛型接口,有过滤功能
1)创建一个元素Person集合:people使用Stream 和 forEach显示该集合所以元素
public class Person { public static enum Sex { FEMALE, MALE; } public String name; public int age; public Sex sex; public Person(String name, int age, Sex sex) { this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + '}'; }}
打印所有信息
public class StreamDemo { public static void main(String[] args){ List<Person> persons = initPersons(); Stream<Person> stream = persons.stream(); stream.filter(p -> p.sex == Person.Sex.FEMALE); stream.forEach( p -> System.out.println(p.toString()) ); } public static List<Person> initPersons(){ List<Person> persons = new ArrayList<Person>(); Person person = new Person("tom", 22 , Person.Sex.FEMALE); persons.add(person); person = new Person("jack", 23 , Person.Sex.FEMALE); persons.add(person); person = new Person("lily", 24 , Person.Sex.FEMALE); persons.add(person); person = new Person("stone", 25 , Person.Sex.FEMALE); persons.add(person); return persons; }}
3、Stream求平均值
public class StreamDemo { public static void main(String[] args) { List<Person> persons = initPersons(); Stream<Person> stream = persons.stream(); double avgAge = stream.filter(p -> p.age > 18) .filter(p -> p.name.indexOf("tom")>=0) .mapToDouble(p -> p.age) .average() .getAsDouble(); System.out.println(avgAge); } public static List<Person> initPersons() { List<Person> persons = new ArrayList<Person>(); Person person = new Person("tom", 22, Person.Sex.FEMALE); persons.add(person); person = new Person("jack", 23, Person.Sex.FEMALE); persons.add(person); person = new Person("lily", 24, Person.Sex.FEMALE); persons.add(person); person = new Person("stone", 25, Person.Sex.FEMALE); persons.add(person); return persons; }}
4、LocateDate 与 LocateTime的用法
1)LocateDate.now():系统日期
2)LocateDate.of(int year, int month, int dayOfMonth):按指定日期创建LocateDate
3)getYear():返回日期中的年份
4)getMonth():返回日期中的月份
5)getDayOfMonth():返回月份中的日
public class LocateDateDemo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date.toString()); System.out.println(date.getYear() + "年"); System.out.println(date.getMonth()+ "月"); System.out.println(date.getDayOfMonth() + "日"); LocalTime time = LocalTime.now(); System.out.println(time.getHour()); System.out.println(time.getMinute()); System.out.println(time.getSecond()); System.out.println(time.toString()); LocalDateTime dateTime = LocalDateTime.now(); System.out.println(dateTime.toString()); System.out.println(dateTime.getYear() + "年"); System.out.println(dateTime.getMonth()+ "月"); System.out.println(dateTime.getDayOfMonth() + "日"); System.out.println(dateTime.getHour()); System.out.println(dateTime.getMinute()); System.out.println(dateTime.getSecond()); }}
5、DateTimeFormatter
public class DateFormatterDemo { public static void main(String[] args) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse("2018-11-31:11:12:13",dtf); System.out.println(dateTime.toString()); }}
6、ZonedDateTime系统时间
LocalDateTime dateTime = LocalDateTime.parse("2018-11-31:11:12:13",dtf);ZonedDateTime dt = ZonedDateTime.now();String formatDate = dt.format(dtf);System.out.println(formatDate);
0 0
- Java8 十大新特性
- Java8 十大新特性
- Java8十大新特性
- jAVA8 十大新特性
- Java8十大新特性
- jAVA8 十大新特性
- jAVA8 十大新特性
- JAVA8 十大新特性详解
- Java8十大新特性详解
- Java8十大新特性详解
- JAVA8 十大新特性详解
- JAVA8 十大新特性详解
- JAVA8 十大新特性详解
- JAVA8 十大新特性详解
- JAVA8 十大新特性详解
- JAVA8 十大新特性详解
- Java8 十大新特性详解
- JAVA8 十大新特性详解
- Centos最新方法升级php版本到5.2.11
- intent携带数据的集中方式
- Linux驱动模块初始教程:一步一步,从helloworld到insmod->printk!!!
- Python中的闭包
- 正则表达式匹配
- Java8 十大新特性
- iOS UITextfield进一步解析
- 清晨的第一缕阳光
- 基于注解ssh的通用dao的crud
- uva10129(play with words)
- 关于select中DISTINCT的技巧和使用
- 数据结构---图---邻接表
- 初次动手编写驱动——LED在linux中的驱动。
- java中获取路径中的空格处理(%20)问题