The new special type java

 

Cache Flow

Buffered modified character stream with a byte stream to the conventional method fileinputStram similar operation to reduce memory to the disk, through the operation of the cache memory in the cache to disk operations,

BufferedinputStream commonly used method is also read () method, the size of the array is read into the byte length

BufferRead has a readline method when the time read is not the character row is null may cycle through the condition

 

Random access file

A RandomAccessFile ( File , mode ) to read the file in random mode can only as " R & lt " , "RW" , and other random read the file, the method used to seek the pointer to the location, getFilePorint obtaining the position resume value reaches a position off the object, SetLength sized file

 

package cn.jiedada.radomaccessfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("hello.txt");
        //Exception in thread "main" java.lang.IllegalArgumentException:
        //Illegal mode "2" must be one of "r", "rw", "rws", or "rwd"
        String mode="r";
        RandomAccessFile raf = new RandomAccessFile(file, mode);
        System.out.println(raf.length());
        //偏移量
        raf.seek(3);
        raf.getFilePointer();
        System.out.println((char)raf.read());
    }

}
View Code

 

 

 

Java New Features

Lambda expressions, method references, the default method, Stream, Optional etc.

Functional Interface

Definition: Only a common approach can have default methods and static methods, static methods through an interface name . Method, a static method by implementing the class object . Method calls by @Functionlainterface identify the judge is not functional interfaces

Is defined as the interface name target = (data type of the variable) -> {} ; Topic Structure

When the data type can be omitted when only one variable may be omitted (), only when the {} only when the output of a statement may be omitted {} then return statement may be omitted when the return ;

Lambda expressions in Java is not independent of the byte code file, when there are variables for final modifications and similar interfaces

 

Package cn.jiedada.lamda; 

public  class the Test {
     / * parameter 1, parameter 2 ...) represents a list of parameters; -> denotes a connector; {} method inside the body 
    1, the right type = function based on the interface types of the left side of Formula automatically inferred; 
    2, if the parameter list is empty, just retained (); 
    3, if only one parameter, () can be omitted, to name only parameter; 
    4, if only one statement execution, and no return value, {} may be omitted, if the return value, if you want to omit the {}, 
    then the return must be omitted, and the statement is also performed to ensure that only one; 
    5, the data type parameter list automatically infer; 
    6 , lambda does not generate a separate internal class file; 
    7, lambda expressions when accessing the local variables, the local variables must be final, 
    if no local variables plus final keyword, the system will automatically add, modify the local thereafter variable being given; * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        / * Testfuntion test1 = name-> System.out.println (name + "eating");
        test1.print ( "kit greatly"); * / 
        
        Testfuntion T2 = (A, B) -> A + B; 
        System.out.println (t2.sum ( . 5,. 7 )); 
        StudentInterface studentInterface = () -> { 
            the System .out.println ( "During the study, please do not disturb" ); 
        }; 
        studentInterface.student (); 
        playInterface playInterface = (String name) -> { 
            System.out.println (name + "in play" ); 
        }; 
        playInterface.play ( "Jay handsome" ); 
        StudentInterface.eat (); 
        studentInterface.walk ();
        
    }

}
View Code

 

 

 

Reference Methods

Classes and to lambda into shorthand expressions

Constructor: class name :: new new;

Static method: class name :: static method name;

Object methods: Object name :: method name;

 

Package cn.jiedada.methods; 

Import java.util.function.Function; 

public  class the Test { 

    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub
         // static reference, into a string a Interger
         // left is the interface, the right is a reference to the class name :: static method 
        ParaseInteger Integer = Integer :: valueOf; 
        Integer parase = integer.parase ( "10" ); 
        System.out.println (parase); 
        // solid column references, pass a String to see whether the way in ending 
        String S = "hello.txt" ; 
        Function <String, Boolean> f1 = S :: endsWith;
        System.out.println(f1.apply("txt"));
    }

}
View Code

 

 

 

Stream

 

Similar lines generated by each method does one thing, the return value to the method of using the last to return;

Divided

Stream serial stream and paralleStream parallel streams

May arrays, collections, IO stream generator operation

Common method

Foreach () traversal , sorted () sorting , limlt (), map () predetermined , filter () filtered , Collectors (). ToList () storing data in the set

 

Package cn.jiedada.stream; 

Import java.util.Arrays;
 Import java.util.List;
 Import java.util.stream.Collectors; 

public  class StreamTest {
     / * 
     * Stream stream 
     * range of arrays, collections, IO stream, Creator 
     * * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        / * List <Integer> = asList Arrays.asList (1,4,7,8,4,9,7, 8,2); 
        // print out and use of flow traversal 
        asList.stream () the sorted () forEach (the System.out :: Print);..  
        // Get the number of squares and deduplication
        asList.stream () Map (I-> I * I) .distinct. () .forEach (System.out :: println); * /
        List <String> asList2 = Arrays.asList ( "sda", "", "Happy", "", "as unhappy or" );
         Long COUNT = asList2.stream () filter (S->!. S.isEmpty ()) .count (); 
        System.out.println (COUNT); 
        // Collectors in toList very important, able to choose out of a collection of the collect 
        List <String> = asList2.stream the collect () filter (S->.! S. isEmpty ()) the collect (Collectors.toList ());. 
        System.out.println (the collect); 
        
    } 

}
View Code

 

 

 

Optional

High grade atmosphere makes the code () with no

Optional.ofnullLable ( Object ) .map (lambda) .orElse () ;

During API

In jdk1.8 later be called directly LocalDateTime can directly call up the date, etc., as well as getDay methods

 

package cn.jiedada.LocalDateTime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDate localDate = now.toLocalDate();
        System.out.println(localDate);
        int dayOfMonth = now.getDayOfMonth();
        int second = now.getSecond();
        System.out.println(dayOfMonth);
        System.out.println(second);
        int minute = now.getMinute();
        int hour = now.getHour();
        /*ZoneId of = ZoneId.of("Europe/Paris");
        System.out.println(LocalDateTime.now(of));*/
    }

}
View Code

 

Guess you like

Origin www.cnblogs.com/xiaoruirui/p/11301014.html