Giter Site home page Giter Site logo

java14's Introduction

java14

Collections -> Factory Method of: creates an immutable data structure.

// Before
List<String> names = new ArrayList<>();
names.add( "Jardel" );
names.add( "Kuhn" );
List<String> unmodifiableNames = Collections.unmodifiableList( names );
  
// After
List<String> immutableNames = List.of( "Jardel", "Kuhn" );
// immutableNames.add( "other" ); -> throws Exception

Set.of( "My", "Name" );

Map<Integer, String> immutableMap = Map.of( 1, "A", 2, "B", 3, "C" );

LocalVariable Type-Inference: can be used in method context and local variables only.

public class Test {

  // var someString = "here it doesn't works";

  private String prcoess() { return "result"; }

  public static void main(String[] args){
  
    // Recommended situations to use
    var message = "Hello, Java 10"; // message -> string
    var oneMap = new HashMap<Integer, String>(); // correct
    
    // Unrecommended situations
    var result = obj.prcoess(); // result type is not clear
    var x = emp.getProjects.stream().findFirst().map(String::length).orElse(0); // streams
    var oneMap = new HashMap<>(); // map type will be <Object, Object>
    
    // Error situations
    // var n;
    // var emptyList = null;
    // var inLambda = ( String s ) -> s.length() > 10;
  }

}

Text Block:

String longText = """
  one really long text one really long text
  one really long text one really long text
  one really long text one really long text
  one really long text one really long text
  """;

Switch Expression:

String nome = "Jardel";
switch (nome){
  case "Jardel" -> System.out.println( "Acertou o nome" );
  case "Kuhn" -> System.out.println( "Acertou o sobrenome" );
  default -> System.out.println( "Errou" );
}

HTTP/2 Client API

public class Teste{
  
  public static void main(String[] args) throws Exception {
    HttpClient httpClient = new HttpClient.newHttpClient();
    HttpRequest httpRequest = HttpRequest.newBuilder( new URI("https://casadocodigo.com.br") ).GET().build();
    
    // Blocking request
    HttpResponse<String> response = httpClient.send( httpRequest, BodyHandlers.ofString() );
    
    System.out.println( response.statusCode() );
    System.out.println( response.body() );
    System.out.println( response.version() );
    
    // Async request
    httpClient.sendAsync( httpRequest, BodyHandlers.ofString() ) // sendAsync returns a CompletableFuture<HttpResponse<String>>
              .whenComplete( ( success, throwable ) -> System.out.println( success.body() ) );
  }

}

java14's People

Contributors

jackanakin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.