zoukankan      html  css  js  c++  java
  • Java8 的一些使用总结


    nohup java -jar -Xmx1024M china-map.jar > log.txt & 在ssh退出后, 也不会终止进程;

    Object[] a = IntStream.range(0,100).
    map(i->String.valueOf(i).hashCode()).
    mapToObj(i->String.valueOf(i).getBytes())
    .toArray();

    c.forEach(System.out::println);



    接口中的default方法:

    public interface NewCharacter {

    public void test1();

    public default void test2(){
    System.out.println("我是新特性1");
    }

    }



    Lambda表达式:
    Collections.sort(c,(t1,t2)->t1.compareTo(t1+t2));


    Function接口:

    @FunctionalInterface // 只能定义一个抽象方法; 通过Lambda去实现这个抽象方法; 相当于 func (String y) {...}
    public interface MyLamda {
    public void test1(String y);
    }

    MyLamda m = y -> System.out.println("ss"+y);


    Function<Integer,Object> f = y -> y+1;

    Supplier 供应商, 就相当于写好的一个 匿名func()


    @FunctionalInterface
    public interface Supplier<T> {

    T get();
    }


    Optional<String> o = Optional.of("aa");
    o.orElseGet(()->"none");


    public T orElseGet(Supplier<? extends T> other) {
    return value != null ? value : other.get();
    }

    等价于:
    public T orElseGet(Function<T,T> func) {
    return value != null ? value : func.apply();
    }

    stream

    task是一个类, tasks 是list<task>;

    final double totalPoints = tasks
    .stream()
    .parallel()
    .map( task -> task.getPoints() ) // or map( Task::getPoints )
    .reduce( 0, Integer::sum );


    final Map< Status, List< Task > > map = tasks
    .stream()
    .collect( Collectors.groupingBy( Task::getStatus ) );


    final Collection< String > result = tasks
    .stream() // Stream< String >
    .mapToInt( Task::getPoints ) // IntStream
    .asLongStream() // LongStream
    .mapToDouble( points -> points / totalPoints ) // DoubleStream
    .boxed() // Stream< Double >
    .mapToLong( weigth -> ( long )( weigth * 100 ) ) // LongStream
    .mapToObj( percentage -> percentage + "%" ) // Stream< String>
    .collect( Collectors.toList() ); // List< String >


    // 文件IO
    final Path path = new File( filename ).toPath();
    try( Stream< String > lines = Files.lines( path, StandardCharsets.UTF_8 ) ) {
    lines.onClose( () -> System.out.println("Done!") ).forEach( System.out::println );
    }

    并行计算;


    long[] arrayOfLong = new long [ 20000 ];

    Arrays.parallelSetAll( arrayOfLong,
    index -> ThreadLocalRandom.current().nextInt( 1000000 ) );
    Arrays.stream( arrayOfLong ).limit( 10 ).forEach(
    i -> System.out.print( i + " " ) );
    System.out.println();

    Arrays.parallelSort( arrayOfLong );


    日期比较

    LocalDateTime dt = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
    System.out.println(dtf.format(dt));

    // Get duration between two dates
    final LocalDateTime from = LocalDateTime.of( 2014, Month.APRIL, 16, 0, 0, 0 );
    final LocalDateTime to = LocalDateTime.of( 2015, Month.APRIL, 16, 23, 59, 59 );

    final Duration duration = Duration.between( from, to );
    System.out.println( "Duration in days: " + duration.toDays() );
    System.out.println( "Duration in hours: " + duration.toHours() );

  • 相关阅读:
    修改weblogic jvm启动参数
    weblogic部署步骤
    weblogic安装步骤
    sqldeveloper 导出数据库
    VO与PO 的理解
    【Hyper-V】与【VirtualBox】【VMware】冲突的解决方法
    xstream 实现simplebean2xml ,xml2simplebean
    eclipse中SVN报错解决
    点击eclipse包报错
    myeclipce项目导入eclipse中报错
  • 原文地址:https://www.cnblogs.com/ruili07/p/11533575.html
Copyright © 2011-2022 走看看