Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a32b64a

Browse files
authored
Update java-new-features.md
1 parent 5fd28b0 commit a32b64a

File tree

1 file changed

+320
-5
lines changed

1 file changed

+320
-5
lines changed

java-new-features.md

Lines changed: 320 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
\readme.txt
2+
```
13
##Java 5
24
###StringBuilder class
35
###Generics
@@ -357,7 +359,176 @@ BiPredicate<L, R> (L, R) -> boolean
357359
BiConsumer<T, U> (T, U) -> void
358360
BiFunction<T, U, R>
359361
360-
LambdaExpressionTest
362+
363+
#STREAMS
364+
many Stream functions are lazy, you do need to use an eager operation such as collect at the end of a sequence of chained method calls
365+
366+
A higher-order function is a function that either takes another function as an argument or returns a function as its result.
367+
368+
map is a higher-order function because its mapper argument is a function. In fact, nearly all the functions that we’ve encountered on the Stream interface are higher-order functions.
369+
370+
Method References
371+
372+
country -> country.getName();
373+
Country::name();
374+
375+
376+
//.parallelStream()
377+
On a 4-core machine with < 50 students, the sequential code was multiple times faster.
378+
At 100 students, almost same
379+
At 10,000 students, parallel code multiple times faster.
380+
381+
##Interfaces
382+
In support of Streams, there are new methods in interfaces such as List, Map, and Set, which have been largely unchanged since the long-gone days of Java 1.1. Fortunately the Java 8 language support adds a default method type in interfaces, so your custom implementations of these interfaces are not required to change (as long as you make sure you change your IDE settings to Java 8 Compiler Compliance).
383+
As one example of default methods in action, Iterable gets a new default method called forEach(), which lets you write code like this:
384+
myList.forEach(o -> /* do something with o here... */);
385+
This is discussed further in Iterable.forEach method (Java 8).
386+
A new JavaScript implementation codenamed Nashorn is available via javax.script (see Calling Other Languages via javax.script) and can also be run from the command line.
387+
388+
#Functional Programming
389+
a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional programming emphasizes functions that produce results that depend only on their inputs and not on the program state - i.e. pure mathematical functions. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects, i.e. changes in state that don’t depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming
390+
```
391+
\src\com\in28minutes\java\newfeatures\threads\Example1.java
392+
```
393+
package com.in28minutes.java.newfeatures.threads;
394+
395+
import java.util.concurrent.TimeUnit;
396+
397+
public class Example1 {
398+
public static void main(String[] args) {
399+
Runnable runnable = new Runnable() {
400+
@Override
401+
public void run() {
402+
try {
403+
String threadName = Thread.currentThread().getName();
404+
System.out.println("Start " + threadName);
405+
TimeUnit.SECONDS.sleep(1);
406+
System.out.println("End " + threadName);
407+
} catch (InterruptedException e) {
408+
// TODO Auto-generated catch block
409+
e.printStackTrace();
410+
}
411+
}
412+
413+
};
414+
415+
Thread thread1 = new Thread(runnable);
416+
thread1.start();
417+
418+
Thread thread2 = new Thread(runnable);
419+
thread2.start();
420+
421+
System.out.println("Done!");
422+
}
423+
}
424+
```
425+
\src\com\in28minutes\java\newfeatures\threads\Example2.java
426+
```
427+
package com.in28minutes.java.newfeatures.threads;
428+
429+
import java.util.concurrent.ExecutorService;
430+
import java.util.concurrent.Executors;
431+
432+
public class Example2 {
433+
public static void main(String[] args) {
434+
ExecutorService executor = Executors.newSingleThreadExecutor();
435+
executor.submit(() -> {
436+
String threadName = Thread.currentThread().getName();
437+
System.out.println("Hello " + threadName);
438+
});
439+
440+
}
441+
}
442+
```
443+
\src\com\in28minutes\java\newfeatures\threads\ThreadLocalExample1.java
444+
```
445+
package com.in28minutes.java.newfeatures.threads;
446+
447+
import java.util.Random;
448+
449+
public class ThreadLocalExample1 {
450+
451+
public static class ThreadClass implements Runnable {
452+
int value;
453+
Random ran = new Random();
454+
455+
@Override
456+
public void run() {
457+
value = ran.nextInt();
458+
System.out.println(Thread.currentThread().getName() + "START" + value);
459+
try {
460+
Thread.sleep(2000);
461+
} catch (InterruptedException e) {
462+
}
463+
System.out.println(Thread.currentThread().getName() + " END " + value);
464+
}
465+
466+
}
467+
468+
public static void main(String[] args) throws InterruptedException {
469+
ThreadClass sharedThreadInstance = new ThreadClass();
470+
Thread thread1 = new Thread(sharedThreadInstance);
471+
Thread thread2 = new Thread(sharedThreadInstance);
472+
Thread thread3 = new Thread(sharedThreadInstance);
473+
474+
// Start all Threads
475+
thread1.start();
476+
thread2.start();
477+
thread3.start();
478+
479+
// Wait for all threads to complete
480+
thread1.join();
481+
thread2.join();
482+
thread3.join();
483+
}
484+
485+
}
486+
```
487+
\src\com\in28minutes\java\newfeatures\threads\ThreadLocalExample2.java
488+
```
489+
package com.in28minutes.java.newfeatures.threads;
490+
491+
import java.util.Random;
492+
493+
public class ThreadLocalExample2 {
494+
495+
public static class ThreadClass implements Runnable {
496+
ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
497+
Random ran = new Random();
498+
499+
@Override
500+
public void run() {
501+
threadLocal.set(ran.nextInt());
502+
System.out.println(Thread.currentThread().getName() + "START" + threadLocal.get());
503+
try {
504+
Thread.sleep(2000);
505+
} catch (InterruptedException e) {
506+
}
507+
System.out.println(Thread.currentThread().getName() + " END " + threadLocal.get());
508+
}
509+
510+
}
511+
512+
public static void main(String[] args) throws InterruptedException {
513+
ThreadClass sharedThreadInstance = new ThreadClass();
514+
Thread thread1 = new Thread(sharedThreadInstance);
515+
Thread thread2 = new Thread(sharedThreadInstance);
516+
Thread thread3 = new Thread(sharedThreadInstance);
517+
518+
// Start all Threads
519+
thread1.start();
520+
thread2.start();
521+
thread3.start();
522+
523+
// Wait for all threads to complete
524+
thread1.join();
525+
thread2.join();
526+
thread3.join();
527+
}
528+
529+
}
530+
```
531+
\test\LambdaExpressionTest.java
361532
```
362533
import static org.junit.Assert.assertEquals;
363534
@@ -592,10 +763,107 @@ public class LambdaExpressionTest {
592763
}
593764
594765
}
595-
596766
```
767+
\test\LambdaExpressionTest2.java
768+
```
769+
import java.util.Arrays;
770+
import java.util.List;
771+
import java.util.function.Function;
772+
import java.util.function.Predicate;
773+
import java.util.stream.Collectors;
774+
775+
import org.junit.Test;
776+
777+
public class LambdaExpressionTest2 {
778+
779+
public class Person {
780+
781+
private final String name;
782+
private final int age;
783+
784+
public Person(final String theName, final int theAge) {
785+
name = theName;
786+
age = theAge;
787+
}
788+
789+
public String getName() {
790+
return name;
791+
}
792+
793+
public int getAge() {
794+
return age;
795+
}
796+
797+
public int ageDifference(final Person other) {
798+
return age - other.age;
799+
}
800+
801+
@Override
802+
public String toString() {
803+
return String.format("%s - %d", name, age);
804+
}
805+
}
806+
807+
final List<Person> people = Arrays.asList(new Person("John", 20), new Person("Sara", 21), new Person("Jane", 21),
808+
new Person("Greg", 35));
809+
810+
@Test
811+
public void testSomething() {
812+
813+
final Function<String, Predicate<String>> startsWithLetter = (
814+
String letter) -> (String name) -> name.startsWith(letter);
815+
816+
final List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
817+
818+
friends.stream().forEach(System.out::println);
819+
820+
friends.stream().map(String::toUpperCase).forEach(System.out::println);
821+
822+
friends.stream().filter(startsWithLetter.apply("N")).forEach(System.out::println);
823+
824+
System.out.println(friends.stream().reduce((s1, s2) -> s1.length() > s2.length() ? s1 : s2).get());
825+
826+
System.out.println(friends.stream().map(String::toUpperCase).collect(Collectors.joining(", ")));
827+
828+
final String str = "text1234";
829+
830+
str.chars().forEach(ch -> System.out.println(ch));
831+
832+
str.chars().filter(Character::isDigit).forEach(System.out::println);
833+
834+
people.stream().sorted((p1, p2) -> p1.ageDifference(p2)).forEach(System.out::println);
835+
836+
people.stream().sorted(Person::ageDifference).forEach(System.out::println);
837+
838+
people.stream().sorted((person1, person2) -> person1.getName().compareTo(person2.getName()))
839+
.forEach(System.out::println);
597840
598-
StreamsTest
841+
}
842+
843+
@Test
844+
public void lambdaExpression_simpleExample() {
845+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
846+
numbers.stream().filter(StreamsTest::isOdd).forEach(number -> System.out.print(number));
847+
// 137
848+
}
849+
850+
@Test
851+
public void lambdaExpression_predicate() {
852+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
853+
numbers.stream().filter((number) -> (number % 2 != 0)).forEach(number -> System.out.print(number));
854+
// 137
855+
}
856+
857+
@Test
858+
public void lambdaExpression_BiFunction() {
859+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
860+
numbers.stream().forEach(number -> System.out.print(number));
861+
// 137
862+
}
863+
864+
}
865+
```
866+
\test\StreamsTest.java
599867
```
600868
601869
import static org.junit.Assert.assertEquals;
@@ -776,7 +1044,54 @@ public class StreamsTest {
7761044
sum += value;
7771045
System.out.println(sum);
7781046
}
779-
}
7801047
781-
```
1048+
@Test
1049+
public void sumOfOddNumbers_Usual() {
1050+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
1051+
int sum = 0;
1052+
for (int number : numbers)
1053+
if (number % 2 != 0)
1054+
sum += number;
1055+
assertEquals(11, sum);
1056+
}
7821057
1058+
@Test
1059+
public void sumOfOddNumbers_FunctionalProgramming() {
1060+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
1061+
int sum = numbers.stream().filter(StreamsTest::isOdd).reduce(0, Integer::sum);
1062+
assertEquals(11, sum);
1063+
}
1064+
1065+
static boolean isOdd(int number) {
1066+
return number % 2 != 0;
1067+
}
1068+
1069+
@Test
1070+
public void streamExample_Distinct() {
1071+
List<Integer> numbers = Arrays.asList(1, 1, 2, 6, 2, 3);
1072+
numbers.stream().distinct().forEach(System.out::print);
1073+
// 1263
1074+
}
1075+
1076+
@Test
1077+
public void streamExample_Sorted() {
1078+
List<Integer> numbers = Arrays.asList(1, 1, 2, 6, 2, 3);
1079+
numbers.stream().sorted().forEach(System.out::print);
1080+
// 112236
1081+
}
1082+
1083+
@Test
1084+
public void streamExample_Filter() {
1085+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
1086+
numbers.stream().filter(StreamsTest::isOdd).forEach(System.out::print);
1087+
// 137
1088+
}
1089+
1090+
@Test
1091+
public void streamExample_Collect() {
1092+
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 2, 7);
1093+
List<Integer> oddNumbers = numbers.stream().filter(StreamsTest::isOdd).collect(Collectors.toList());
1094+
System.out.println(oddNumbers);
1095+
// [1, 3, 7]
1096+
}
1097+
}

0 commit comments

Comments
 (0)