Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created August 8, 2020 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokon12/023361716c1961767d47af02ab16785a to your computer and use it in GitHub Desktop.
Save rokon12/023361716c1961767d47af02ab16785a to your computer and use it in GitHub Desktop.
AdvanceJava2020
package com.masterdevskills.playground;
import java.io.File;
import java.time.LocalDate;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
/**
* @author A N M Bazlur Rahman @bazlur_rahman
* @since 08 August 2020
*/
public class Playground {
private int x;
private void doSomething() {
int x;
}
public static void main(String[] args) {
// var x = ""; // Local Type inference
// print(x);
//Declarative vs Imperative
// var x = 99;
// var integers = List.of(1, 2, 4, 9, 5, 6, 5, 10);
//
// //==
// boolean found = false;
// for (int i = 0; i < integers.size(); i++) {
// if (integers.get(i).equals(x)) {
// found = true;
// }
// }
//System.out.println("exist: " + found);
//what we want
//how we want
//YakShaving
//var contains = integers.contains(x);
//select * from users u join something_esle e on e.id =u.id
//external iteration
// internal
// for (int i = 0; i < integers.size(); i++) {
// System.out.println(integers.get(i));
// }
//
//
// var executors = Executors.newFixedThreadPool(16);
// //100_000_000/16
// for (Integer integer : integers) {
// //serial
//
// }
//
//
//
//// LongStream.iterate(0, (value -> value++))
//// .parallel()
//// .forEach(value -> {
////
//// });
//
// integers.parallelStream().forEach(integer -> {
//
// });
// //Fork/Join Framework
//
//
// integers.forEach(integer -> {
// System.out.println(integer);
// });
//
// var cpuCount = Runtime.getRuntime().availableProcessors();
// System.out.println(" cpuCount: " + cpuCount);
//
// //What
// //How
processInt(5);
// int x =5;
// ///
// printIt(x == 3 ? "hello world" : "something else");
// new Thread(() -> System.out.println("Inside another thread: " + Thread.currentThread().getName())).start();
//// () -> { };
// BiFunction<String, String, String> fun = (String x, String y) -> {
// return x + "," + y;
// };
var people = Person.createPeople(20);
peopleOlderThan(people, 10);
peopleOlderThan(people, 18, Person.Gender.FEMALE);
peopleOlderThan(people, 18, Person.Gender.MALE);
var peopleOlderThan21 = filterPeople(people,
person -> person.getGender() == Person.Gender.FEMALE
&& person.age() > 21);
Filtrable<Person> filtrable = person ->
person.getGender() == Person.Gender.MALE && person.age() > 60;
Filtrable<Person> filter2 = p -> {
if (p.age() > 10) {
return true;
} else {
return false;
}
};
// peron older than 20 but not older than 30 and
List<Person> peopleOlderThan160 = filterPeople(people, (p) -> p.age() > 20 && p.age() <= 30);
Transformer<Person, String> transformer = (p1) -> p1.getFirstName();
Filtrable<Person> filtrable1 = (p) -> p.age() > 20 && p.age() <= 30;
// var strings = filterPeople(people,
// filtrable1,
// transformer
// );
// System.out.println(strings);
Transformer<String, Integer> t1 = (value) -> value.trim().length();
Transformer<Person, LocalDate> t2 = (pesron) -> pesron.getDob();
testTransformer(" hello world", t1);
Filtrable<String> stringFiltrable = (String s) -> !s.isEmpty();
Filtrable<Integer> integerFiltrable = a -> a > 20;
Runnable run = () -> {
for (int i = 0; i < 100; i++) {
if (i == 55) {
break;
}
}
};
new Thread(() -> {
while (true) {
System.out.println("hello world");
}
});
new Thread(() -> {
while (true) {
System.out.println("hello world");
}
});
File file = new File("/Users/rokonoid/projects/advancejava/src/main/java/com/masterdevskills/cha2");
var files = file.listFiles((pathname -> pathname.getName().endsWith(".java")));
Arrays.asList(files)
.forEach(f -> System.out.println(f.getName()));
var people1 = Person.createPeople(20);
Comparator<Person> ageComparator = (o1, o2) -> Integer.compare(o1.age(), o2.age());
Collections.sort(people1, ageComparator);
Comparator<Person> firstNameComparator = (o1, o2) -> o1.getFirstName().compareTo(o2.getFirstName());
Collections.sort(people1, firstNameComparator);
var nums = List.of(1, 2, 3, 4, -1);
final var y = 23;
nums.forEach(value -> {
if (value == y) {
System.out.println("matched");
}
});
final Person person = new Person();
Executable runnable = () -> {
};
Runnable executable = () -> {
};
executeAround(runnable);
UnaryOperator<String> toUpperCase = (String s) -> s.toUpperCase();
//UnaryOperator
var reduce = reduce(people, (p) -> p.getFirstName(), (p1, p2) -> p1 + ", " + p2);
System.out.println(
reduce
);
//map -> reduce
IntStream.range(0, people.size())
.mapToObj(index -> new IndexPair(index, people.get(index)))
.forEach(indexPair -> {
var index = indexPair.index;
var t = indexPair.t;
});
}
static class IndexPair<T>{
private final int index;
private final T t;
public IndexPair(int index, T t) {
this.index = index;
this.t = t;
}
public int getIndex() {
return index;
}
public T getT() {
return t;
}
}
public static String reduce(List<Person> personList, Function<Person, String> mapper,
BinaryOperator<String> unaryOperator) {
String acumulator = "";
for (Person person : personList) {
var mapped = mapper.apply(person);
acumulator = unaryOperator.apply(acumulator, mapped);
}
return acumulator;
}
public static void executeAround(Executable executable) {
}
public Runnable run() {
return () -> {
};
}
private static Integer testTransformer(String value, Transformer<String, Integer> transformer) {
return transformer.transform(value);
}
private static List<String> filterPeople(List<Person> personList,
Predicate<Person> filtrable,
Function<Person, String> transformer) {
List<String> peopleOlderThan = new ArrayList<>();
for (Person person : personList) {
if (filtrable.test(person)) {
peopleOlderThan.add(transformer.apply(person));
}
}
return peopleOlderThan;
}
private static List<Person> filterPeople(List<Person> personList,
Filtrable<Person> filtrable) {
List<Person> peopleOlderThan = new ArrayList<>();
for (Person person : personList) {
if (filtrable.test(person)) {
peopleOlderThan.add(person);
}
}
return peopleOlderThan;
}
private static List<Person> peopleOlderThan(List<Person> people, int age) {
List<Person> peopleOlderThan = new ArrayList<>();
for (Person person : people) {
if (person.age() > age) {
peopleOlderThan.add(person);
}
}
return peopleOlderThan;
}
private static List<Person> peopleOlderThan(List<Person> people, int age, Person.Gender gender) {
List<Person> peopleOlderThan = new ArrayList<>();
for (Person person : people) {
if (person.age() > age
&& person.getGender() == gender) {
peopleOlderThan.add(person);
}
}
return peopleOlderThan;
}
@FunctionalInterface
interface Filtrable<T> {
boolean test(T person);
//Single Abstract Method (SAM)
}
@FunctionalInterface
interface Transformer<T, R> {
R transform(T t);
}
private static void printIt(String msg) {
System.out.println(msg);
}
private static void processInt(Integer integer) {
//heavy operation
}
private static void print(String x) {
System.out.println(x);
}
interface Executable {
void execute();
default void doSomethingImportant() {
//100
x1();
x2();
}
private void x1() {
}
private void x2() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment