Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.1</version>
<version>2.13.4.2</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/java11/FeatureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package java11;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

//import javax.annotation.Nonnull;
import lombok.NonNull;

/**
* No need to compile, just run: java FeatureTest.java
*/
public class FeatureTest {
public static void main(String[] args) {
String multilineString = "This is a text \n \n from \n new line!";

// introducing lines() stream
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());

System.out.println(lines);

// introducing toArray()
String[] arr = lines.toArray(String[]::new);

for(String s: arr) System.out.println(s);

lines.add(" ");
System.out.println(lines);

// introducing not method in Predicate
List<String> withoutBlanks = lines.stream()
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());

System.out.println(withoutBlanks);

// introducing local-variable syntax
String resultString = lines.stream()
.map((@NonNull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));

System.out.println(resultString);

}

}
22 changes: 22 additions & 0 deletions src/main/java/java8/FunctionalInterfaceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package java8;

public class FunctionalInterfaceTest {

public static void main(String[] args) {
FunctionalInterfaceTest fit = new FunctionalInterfaceTest();
Functionalnterface fi = new Functionalnterface();
fi.setFi(fit::getConvertedValue);

fi.checkFi("9");
}

public void test() {
Functionalnterface fi = new Functionalnterface();
fi.setFi(this::getConvertedValue);
fi.checkFi("9");
}

public Integer getConvertedValue(String str) {
return Integer.valueOf(str);
}
}
12 changes: 12 additions & 0 deletions src/main/java/java8/Functionalnterface.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package java8;

import java.util.function.Function;

public class Functionalnterface {

Function<String, Integer> fi;

public static void main(String[] args) {
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
Expand All @@ -18,6 +22,14 @@ public static void main(String[] args) {
tester.test();

}

public void setFi(Function<String, Integer> fi){
this.fi = fi;
}

public void checkFi(String str) {
System.out.println(this.fi.apply(str));
}

@FunctionalInterface
interface Converter<F, T> {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/java8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ Lambda expressions can only appear in places where they will be assigned to a va
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.

Anonymous Classes, shows you how to implement a base class without giving it a name. Although this is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes more compactly.


### Streams

1. The main difference between intermediate and terminal operations is that intermediate operations return a stream as a result and terminal operations return non-stream values like primitive or object or collection or may not return anything.

2. As intermediate operations return another stream as a result, they can be chained together to form a pipeline of operations. Terminal operations cannot be chained together.

3. Pipeline of operations may contain any number of intermediate operations, but there has to be only one terminal operation, that too at the end of pipeline.

4. Intermediate operations are lazily loaded. When you call intermediate operations, they are actually not executed. They are just stored in the memory and executed when the terminal operation is called on the stream.

5. As the names suggest, intermediate operations doesn’t give end result. They just transform one stream to another stream. On the other hand, terminal operations give end result.

![Streams](https://vinkrish-notes.s3.us-west-2.amazonaws.com/streams.jpeg)
124 changes: 124 additions & 0 deletions src/main/java/java8/SupplierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package java8;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Supplier;

/*
@FunctionalInterface
public interface Supplier<T> {
T get();
}
*/
public class SupplierTest<T> {

private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public static void main(String[] args) {

Supplier<LocalDateTime> s = () -> LocalDateTime.now();
LocalDateTime time = s.get();

System.out.println(time);

Supplier<String> s1 = () -> dtf.format(LocalDateTime.now());
String time2 = s1.get();

System.out.println(time2);

// ---------------------------------------------------------- //

SupplierTest<String> obj = new SupplierTest<>();
List<String> list = obj.supplier().get();

// ---------------------------------------------------------- //

Developer dev = factory(Developer::new);
System.out.println(dev);

Developer dev2 = factory(() -> new Developer("vinkrish"));
System.out.println(dev2);

// ---------------------------------------------------------- //

System.out.println(new Developer().callRandomString.get());

}

public Supplier<List<T>> supplier() {

// lambda
// return () -> new ArrayList<>();

// constructor reference
return ArrayList::new;

}

public static Developer factory(Supplier<? extends Developer> s) {

Developer developer = s.get();
if (developer.getName() == null || "".equals(developer.getName())) {
developer.setName("default");
}
developer.setSalary(BigDecimal.ONE);
developer.setStart(LocalDate.of(2017, 8, 8));

return developer;

}
}

class DeveloperParent {

/*
return Config instead of String,
let calling child read config values (Typesafe Config).getString("some.field")
*/
public static String getRandomString() {
return "Hello From Supplier!";
}

}

class Developer extends DeveloperParent{

String name;
BigDecimal salary;
LocalDate start;

// for factory(Developer::new);
public Developer() {
}

// for factory(() -> new Developer("vinkrish"));
public Developer(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public void setSalary(BigDecimal salary) {
this.salary = salary;
}

public void setStart(LocalDate start) {
this.start = start;
}

public String getName() {
return this.name;
}

public String toString() {
return this.name + " - " + this.salary + " - " + this.start;
}

public Supplier<String> callRandomString = () -> getRandomString();

}
25 changes: 25 additions & 0 deletions src/main/java/java9/ListOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package java9;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListOf {

public static void main(String[] args) {

final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = List.of(list.toArray(new String[]{}));

/**
* unmodifiableList.add("four");
*
* Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:147)
at java9.ListOf.main(ListOf.java:13)
*/

}

}
68 changes: 68 additions & 0 deletions src/main/java/java9/MapOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package java9;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapOf {

public static void main(String[] args) {
// Immutable Map
Map<Integer, String> map = Map.of(101, "A", 102, "B", 103, "C");
map.forEach((k, v) -> System.out.println(k + " - " + v));

/**
* map.put(104, "SS");
* Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072)
at java9.MapOf.main(MapOf.java:11)
*/

// Immutable Map with Immutable List
List<String> imtList1 = List.of("A1", "B1");
List<String> imtList2 = List.of("A2", "B2");

Map<Integer, List<String>> map2 = Map.of(111, imtList1, 222, imtList2);
System.out.println(map2);

// unmodifiable Map with mutable List
List<String> list1 = new ArrayList<String>();
list1.add("A1");
list1.add("B1");
List<String> list2 = new ArrayList<String>();
list2.add("A2");
list2.add("B2");

Map<Integer, List<String>> map3 = Map.of(111, list1, 222, list2);
System.out.println("Mutable List :" + map3);

list1.add("R1");
System.out.println("Mutable List :" + map3);

// Immutable Map with custom Immutable class
CustomImmutableStudent s1 = new CustomImmutableStudent(33, "Vinay");
CustomImmutableStudent s2 = new CustomImmutableStudent(24, "Angel");

Map<String, CustomImmutableStudent> map4 = Map.of("one", s1, "two", s2);
map4.forEach((k, v) -> System.out.println(k + " - " + v.getName()));
}

}

final class CustomImmutableStudent {
final private int age;
final private String name;
public CustomImmutableStudent(final int age, final String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}
}
32 changes: 32 additions & 0 deletions src/main/java/java9/MapOfEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package java9;

import java.util.Map;
import static java.util.Map.entry;

import java.util.List;

public class MapOfEntries {

public static void main(String[] args) {
// Immutable Map
Map<Integer, String> map = Map.ofEntries(
entry(101, "A"),
entry(102, "B"),
entry(103, "C")
);
map.forEach((k, v) -> System.out.println(k + " - " + v));

// Immutable Map with Immutable List
List<String> imtList1 = List.of("A1", "B1");
List<String> imtList2 = List.of("A2", "B2");

Map<Integer, List<String>> map2 = Map.ofEntries(
entry(111, imtList1),
entry(222, imtList2)
);
System.out.println(map2);

// Similar approach to MapOf...
}

}
Loading