Java 8 Features

Array parallelSort: Arrays.parallelSort(arr); * * * Default Method in Interface: Böylelikle Multi Inheritance ile yapılamayan birden fazla sınıftan kalıtımı, Interface içinde default metodunu tanımlayarak ve bu Interface’i implement ederek implement eden sınıfında bu default metoda sahip olmasını sağlayabiliriz. * * * Lambda Expression: ->. Argument List – Arrow Token – Body of Lambda Expression A function that can be created without belonging to any class. – Lambda expression body can be define () zero parameter, (p) one parameter, (p1, p2) multi parameter. – Curly bracket is not necessary for single statement in the body of lambda expression and return keyword also not necessary but if you want to use you can use it. – * * * Functional Interface (Single Abstract Method Interfaces): An interface with single abstract method. Only one abstract method must be in interface, default and static methods also can be defined. Lambda expression can be work with this abstract method. Using with lambda expression. Runnable, ActionListener, Comparable are some of the examples of functional interfaces. [code lang=”java”] // create anonymous inner class object new Thread(new Runnable() { @Override public void run() { System.out.println("New thread created"); } }).start(); yerine new Thread( () -> {System.out.println("New thread created");} ).start(); [/code] Abstract method can has multiple parameters. [code lang=”java”] @FunctionalInterface // Opsiyonel interface Foo { int apply(int x, int y); } Foo addition = (x,y) -> x + y Foo multiplication = (x,y) -> x * y System.out.println("3 + 5 = " + addition(3,5)); System.out.println("3 * 5 = " + multiplication(3,5)); [/code] [code lang=”java”] interface FuncInterface { // An abstract function void abstractFunc(int x); // A non-abstract (or default) function default void normalFunc() { System.out.println("Hello"); } } FuncInterface funcInterface = (int x) -> System.out.println(2 * x); // This calls above lambda expression and prints 10. funcInterface.abstractFunc(5); [/code] [code lang=”java”] interface FuncInterface { // An abstract method void abstractMethod(int x); // A non-abstract (or default) method default void normalMethod() { System.out.println("Hello"); } // A static method staic void staticMethod(int x, int y); } [/code] * * * Method Reference: :: (double colon) Static methods use with class name, non-static methods use with instance of object. [code lang=”java”] // a static method SomeClass::someStaticMethod testList.forEach(TestClass::staticMetod); TestClass testClass=new TestClass(); // non static method testList.forEach(testClass::nonStaticMetod); // an instance method System.out::println list.forEach((new YCN())::someFunction); list.forEach(Test::someFunction); // class constructor ArrayList::new list.forEach(YCN::new); stream.forEach(s -> System.out.println(s)); stream.forEach(System.out::println); Arrays.asList("y","c","n").stream().forEach(System.out::println); // super method super::print [/code] * * * Stream API: [code lang=”java”][/code] Java 8 Stream API examples * * * Optional Class: for nullCheck Optional provide easy manage of null instances. Optional, contains various static methods like empty, of, nullable. Empty, creates a Optinal object. of, wraps the object and don’t accept null values. ofNullable, wraps the object and accepts null values. isPresent, if the value is not null and be want to do process. map, provide mapping filter, provide filtering orElse, if wrapped value exist return this wrapped value, if not exist return other value orElseGet, if wrapped value exist return this this wrapped value, if not exist creates one orElseThrow, if there is wrapped value return this wrapped value, if not exist than throw exception [code lang=”java”] // empty usage Optional<String> empty = Optional.empty(); * * // of usage for String Optional<String> ofString = Optional.of("Hello"); // if account is null, it throws exception Optional.of(account); // throws exception Optional<String> ofNull = Optional.of(null); * * // don’t throws exception Optional<Integer> ofNullable = Optional.ofNullable(null); // it don’t throws any exception, account is can null or not Optional.ofNullable(account); * * // if account is not null, isPresent returns true Optional.ofNullable(account).isPresent(); // we can use isPresent like this Integer number = null; Optional<Integer> opt = Optional.ofNullable(number); opt.ifPresent(num -> { System.out.println(Math.pow(num , 2)); }); // instead of old usage Integer number = null; if(number != null) { System.out.println(Math.pow(numara , 2)); } * * // map Integer number = null; Optional<Integer> numberOpt = Optional.ofNullable(number); numberOpt.map(num->Math.pow(num, 2)).ifPresent(System.out::println); * * // filter // we can use filter like this String str = null; Optional<String> strOpt = Optional.ofNullable(str); strOpt.filter(str -> str.length() > 5).ifPresent(System.out::println); // instead of old usage String str = null; if (str != null && message.length() > 5) { System.out.println(str); } * * // orElse // we can use orElse like this Integer number = null; Optional<Integer> numberOpt = Optional.ofNullable(number); int result = numberOpt.orElse(0); // instead of old usage Integer number = null; int result = (number != null) ? number : 0; * * // orElseGet // if account is null, return new Account() Optional.ofNullable(account).orElseGet(new Account()); // if names null, create one List<String> wordList = Arrays.asList("a","b"); Optional<List<String>> wordListOpt = Optional.ofNullable(wordList); wordListOpt = opt.orElseGet(()-> new ArrayList()); // with Lambda usage // or wordListOpt = opt.orElseGet(ArrayList::new); // with Metod Reference usage * * // if account is null, it throws exception Optional.ofNullable(account).orElseThrow(throw new CodedException(ErrorCode.NULL_INSTANCE)); Integer number = null; Optional<Integer> numberOpt = Optional.ofNullable(number); int result = numberOpt.orElseThrow(RuntimeException::new); [/code] * * * Concurency with ExecutorService: [code lang=”java”][/code] * * * Concurrency Enhancements: [code lang=”java”][/code] * * * JDBC Enhancements: [code lang=”java”][/code] * * * LongAdder: AtomicLong’dan daha yüksek başarımlı sayaç işlemlerinin yapıldığı LongAdder objesi geldi. AtomicLong a gore daha fazla bellek kullanmaktadır. * * * Java Lambda Expressions What are lambda expressions? Formal syntax for lambda expressions Lambda expression simple syntax Lambda expressions that return a value Lambda expressions with multiple arguments Lambda expressions and scope Common usage scenarios Method enhancements Method references Constructor references Default methods Static methods in interfaces New Functional Interfaces Function Predicate Consumer Supplier BinaryOperator Additional new functional interfaces Streams Sequential vs. parallel streams Immediate vs. terminal operations Stream example Lazy evaluation A closer look at immediate and terminal operations Primitive specialized streams Enhanced Collections API Iteration New methods in List New methods in Map Enhanced concurrency API ConcurrentHashMap CompletableFuture CountedCompleter Adders and accumulators Additional new features Time IO / NIO additions Reflection and annotation changes Nashorn JavaScript Engine]]>

Leave a Reply

Your email address will not be published. Required fields are marked *