Skip to content

Instantly share code, notes, and snippets.

@rattanchauhan rattanchauhan/Java8.java
Last active Nov 24, 2016

Embed
What would you like to do?
Java8 features in practice
package com.practice.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class Java8 {
public static void main(String[] args) {
// Predicates 1
Predicate<String> stringLengthCheck = (s) -> s.length() > 5;
System.out.println(stringLengthCheck.test("Hellossss")); // true
System.out.println(stringLengthCheck.negate().test("hell")); // true
// Predicates 2
Predicate<String> nonNull = (o) -> Objects.nonNull(o);
System.out.println(stringLengthCheck.and(nonNull).test("213456")); // true
// Predicates 3
Predicate<String> nonNullStringGreaterThanFourChars = nonNull.and(stringLengthCheck);
System.out.println(nonNullStringGreaterThanFourChars.test("test")); // false
System.out.println(nonNullStringGreaterThanFourChars.test("test123")); // true
// Method reference
Predicate<Boolean> isNull = Objects::isNull;
Predicate<Boolean> nonNull2 = Objects::nonNull;
Boolean bool = null;
System.out.println(isNull.test(bool)); // true
System.out.println(nonNull2.test(bool)); // false
// Lambda 1
Converter<Integer, String> integerToStringConverter = (a) -> String.valueOf(a);
System.out.println(integerToStringConverter.convert(23)); // 23
System.out.println(integerToStringConverter.convert(23) instanceof String); // true
// Lambda 2
List<Person> persons = Helper.getPersons();
System.out.println(Helper.printList(persons));
// Sorting by Name
Collections.sort(persons, (a, b) -> a.getName().compareTo(b.getName()));
System.out.println(Helper.printList(persons));
// Sorting by age
Collections.sort(persons, (a, b) -> Integer.compare(a.getAge(), b.getAge()));
System.out.println(Helper.printList(persons));
// Lambda 3
Adder<Integer> adder = (a,b) -> (Integer)a + (Integer)b;
System.out.println(adder.add(0, Integer.MAX_VALUE));
// Streams 1
List<Person> list = Helper.getPersons();
Predicate<Person> ageGreaterThanTwenty = (a) -> a.getAge() > 20;
list.stream()
.filter(ageGreaterThanTwenty)
.forEach(System.out::println);
// Streams 2
Optional<Person> maxAgePerson = list
.stream()
.reduce((a,b) -> a.getAge() < b.getAge() ? b : a);
maxAgePerson.ifPresent(System.out::println); // max age person
// Streams 2
Stream.iterate(new long[]{ 1, 1 }, p->new long[]{ p[1], p[0]+p[1] })
.limit(15)
.forEach(p->System.out.println(p[0])); // prints fibonacci series
}
}
class Helper {
static List<Person> getPersons() {
List<Person> list = new ArrayList<>();
list.add(new Person("Vinod", 39));
list.add(new Person("Aman", 29));
list.add(new Person("Zeeshan", 19));
list.add(new Person("Ishan", 22));
return list;
}
static <T> String printList(List<T> list) {
StringBuilder builder = new StringBuilder();
for(Object o : list) {
T t = (T)o;
builder.append("{ ").append(t.toString()).append(" } ");
}
return builder.toString();
}
}
@FunctionalInterface
interface Converter<F, T> {
T convert(F obj);
}
@FunctionalInterface
interface Adder<T> {
T add(T a, T b);
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.