JAVA8新特性

 

时间日期类 private LocalDate
//        LocalDate localDate=LocalDate.now();
// System.out.println(localDate); 2021-3-19
// LocalTime time=LocalTime.now(); 11:58:40.031
// LocalDateTime dateTime=LocalDateTime.now();2021-2-18T19:19:40.031

// LocalDate date=LocalDate.of(2021,3,17); 指定整数组成时间

// LocalDate date1=LocalDate.parse("2021-03-16"); 将字符序列转成日期,中间必须用-两位数
// date1.getYear();
data1.getMonthValue();提取年月日
// date1.getDayOfWeek();

// //时间戳 毫秒数
System.out.println(Instant.now().toEpochMilli());

//时间格式化
// DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// System.out.println(formatter.format(LocalDateTime.now())); 2021-03-19 12:01:40
 
   

// String[]names={"zhang","li","yang"};
// Arrays.asList(names).forEach(System.out::println);
//asList转化为集合 forEach放的consumer接口
// Consumer 消费型接口 <T> 接受参数输出 都是函数式接口
// Consumer <Integer> consumer1=Demo01::printData;
// consumer1.accept(5);

// Supplier 供给型接口 获取
Supplier<Integer>supplier=()-> new Random().nextInt(10);
System.out.println(supplier.get());

Supplier<Integer>supplier1=new Random()::nextInt;
System.out.println(supplier1.get());

// Predicate 断言 布尔值
Predicate<Integer>predicate=(x) -> x>0;
predicate.test(5);


// Function 函数 执行 第一个参数是参数类型 第二个参数返回值类型
Function<String,Integer>function=(str)->str.length();
function.apply("sda");
       Function<Integer,Integer>function=(num)->num*num;
function.apply(5);

}
 //  Consumer 消费型接口 一条语句实现
//    public static void printData(Integer x){
// System.out.println(x+1);
// }
@FunctionalInterface 函数式接口
//一个抽象方法
public interface Aritst {
void sing(String singer);
default void dance(){};
default void paint(){};
static void test(){
System.out.println();
}

接口可以加default实现或者加static静态方法
@FunctionalInterface注解只能包含
函数式接口只有一个抽象方法

   Aritst aritst =  (singer) -> {    //只能实现一个抽象接口
// System.out.println(singer);
// System.out.println(singer.length());
// };
// aritst.sing("a");
public class someOne implements Aritst {

@Override
public void sing(String name) {

}

// @Override
// public void dance() {
//
// }
//
// @Override
// public void paint() {
//
// }
}
public class Calc {
// int calc(int x int y){
// return x+y;
// }
// (x,y)->{x+y}

函数式接口可以用:
lambda表达式->  是一个匿名函数,允许把一个函数作为参数进行传递
(参数1,参数2。。。)->{方法体} 只能用在函数式接口


方法的引用::
方法的引用是用来访问类或者实例已经存在的方法或者构造方法
前提是,lambda表达式方法体只有一条代码

 

optional 非空验证 对普通对象的包装  私有


public class Demo {
public Demo() {
}

public static void main(String[] args) {
// Person person = null; 用get抛出异常
Person person1 = new Person();
person1.setName((String)null);

Optional<Person> op = Optional.ofNullable(person1); 不能为空ofNullable
op.get();获取值
System.out.println("----------------");

PrintStream var10001 = System.out;
op.ifPresent(var10001::println);

//orElse orElseGet当值不存在时返回默认值
System.out.println(((Person)op.orElse(person1)).getName());
System.out.println(((Person)op.orElseGet(() -> {
return person1;
})).getName());


Street street = new Street("");
street.setName("宾水西道");
City city = new City();
city.setStreet(street);
Country country = new Country();
country.setCity(city);
Address address = new Address;
address.setCountry(country);
person1.setAddress(address);
System.out.println(getStreetName(op));
System.out.println("*******");

System.out.println(op.filter((p) -> { filter返回断言接口
return Optional.of(p.getName()).isPresent();
}));
}

public static String getStreetName(Optional<Person> p) {
return (String)p.map((p1) -> {
return p1.getAddress();
}).map((a) -> {
return a.getCountry();
}).map((c) -> {
return c.getCity();
}).map((c) -> {
return c.getStreet();
}).map((s) -> {
return s.getName();
}).orElse("未知地址");
}
}
public class Address {
private Country country;

public Address() {
}

public Country getCountry() {
return this.country;
}

public void setCountry(Country country) {
this.country = country;
}
}



public class City {
private Street street;

public Street getStreet() {
return street;
}

public void setStreet(Street street) {
this.street = street;
}
}
 
public class Country {
private City city;

public Country() {
}

public City getCity() {
return this.city;
}

public void setCity(City city) {
this.city = city;
}
}



public class Street {
private String name;

public String getName() {
return this.name;
}

public Street(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return "Street{name='" + this.name + '\'' + '}';
}
}
 
public class Person {
private String name;
private Address address = null;

public Person() {
}

public Address getAddress() {
return this.address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
}

s

stream 和集合一起,对集合或者数组中的数据  数据源:可以是数组或集合

public class Demo {
public Demo() {
}

public static void main(String[] args) {
List<Street> list = new ArrayList();
list.add(new Street("宾水西道"));
list.add(new Street("工西路"));
list.add(new Street("ab"));

Stream<Street> stream = list.stream();list获取流
stream.forEach(System.out::println);
String[] names ={"zhangsan", "lisi"};

Arrays.stream(names).forEach(System.out::println);
        Stream var10000 = Arrays.stream(names);
PrintStream var10001 = System.out;
var10000.forEach(var10001::println);

Stream<Integer> s1 = Stream.of(10, 20, 30);
        s1.forEach(System.out::println);
        System.out.println("--------generate无限流--------");
        s1=Stream.generate(()->new Random().nextInt(10));
        System.out.println("--------iterate无限流--------");
        s1=Stream.iterate(0,x->x+10);
        System.out.println("---------filter-------");断言
list.stream().filter((s)->s.getName().length()>3).forEach(System.out::println);

list.stream().skip(2).forEach(System.out::println); 跳过skip

list.stream().map((s)->s.getName().length()).forEach(System.out::println);映射

list.stream().sorted(Comparator.comparingInt((s)->s.getName().length())).forEach(System.out::println);
}sort Comparator接口
NIO paths类

public class Demo {
public Demo() {
}
Path path= Paths.get("filess","abc.txt");
if (!Files.exists(path));
try {
Files.createDirectory(Paths.get("filess"));
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}

try {
Files.write(path,"今天上了9小时课".getBytes());写
} catch (IOException e) {
e.printStackTrace();
}
try {
List<String>list= Files.readAllLines(path);读
list.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

channel通道 写到buffer里
FileChannel fileChannel=new FileOutputStream("filess/abc.txt").getChannel();文件通道
fileChannel.write(ByteBuffer.wrap("hahha".getBytes()));把字符数组转成字符缓冲流
fileChannel.close();

buffer
ByteBuffer buffer=ByteBuffer.allocate(1024);分配空间
fileChannel= new FileInputStream("filess/abc.txt").getChannel();
fileChannel.read(buffer);
buffer.flip();从第一个元素读
while (buffer.hasRemaining()){有没有继续的状态
System.out.println(buffer.get());
}
posted @ 2021-03-21 13:24  YangYuJia  阅读(6)  评论(0)    收藏  举报