Skip to content

Commit e6fb58f

Browse files
author
Dhananjay Nagargoje
committed
MISC JAVA 8 RECIPIES
1 parent 3d10b33 commit e6fb58f

File tree

90 files changed

+1379
-1079
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1379
-1079
lines changed

Diff for: .idea/workspace.xml

+578-879
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
2.03 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.63 KB
Binary file not shown.

Diff for: out/production/main/datastructures/graph/Graph.class

275 Bytes
Binary file not shown.

Diff for: out/production/main/datastructures/graph/Paths.class

249 Bytes
Binary file not shown.
Binary file not shown.
1.67 KB
Binary file not shown.
Binary file not shown.
310 Bytes
Binary file not shown.
1.36 KB
Binary file not shown.
294 Bytes
Binary file not shown.

Diff for: out/production/main/javarecipies/java8/DoStuff.class

138 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
719 Bytes
Binary file not shown.
3.22 KB
Binary file not shown.
2.45 KB
Binary file not shown.
1.26 KB
Binary file not shown.
Binary file not shown.

Diff for: out/production/main/javarecipies/java8/Person.class

1.5 KB
Binary file not shown.
148 Bytes
Binary file not shown.
Binary file not shown.
725 Bytes
Binary file not shown.
1.65 KB
Binary file not shown.
517 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.44 KB
Binary file not shown.
1.82 KB
Binary file not shown.
3.01 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
751 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.52 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.04 KB
Binary file not shown.

Diff for: out/production/main/problems/ongraph/FIndJudge.class

642 Bytes
Binary file not shown.
642 Bytes
Binary file not shown.
1.75 KB
Binary file not shown.
1.64 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.07 KB
Binary file not shown.
806 Bytes
Binary file not shown.
2.9 KB
Binary file not shown.
2.39 KB
Binary file not shown.
Binary file not shown.
2.74 KB
Binary file not shown.

Diff for: out/production/main/problems/test/Main.class

265 Bytes
Binary file not shown.
1.53 KB
Binary file not shown.
Binary file not shown.
2.77 KB
Binary file not shown.

Diff for: out/production/main/testingsystem/misccode/input.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
qasx
2-
qas x
1+
1
2+
7
3+
1 2
4+
1 3
5+
2 4
6+
2 5
7+
4 6
8+
4 7
9+
508 Bytes
Binary file not shown.

Diff for: out/production/main/websites/he/TestClass.class

1.27 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package javarecipies.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
public class ConcurrentModification {
8+
public static void main(String[] args) {
9+
10+
List<Integer> modify = new ArrayList<>();
11+
modify.add(1);
12+
modify.add(2);
13+
modify.add(3);
14+
15+
16+
Iterator<Integer> it = modify.iterator();
17+
while (it.hasNext()) {
18+
Integer x = it.next();
19+
System.out.println(x);
20+
if (x == 1){
21+
//below line will cause ConcurrentModificationException as we have modified collection
22+
//without telling iterator
23+
// modify.remove(2);
24+
25+
//to avoid exeception use iterator remove
26+
it.remove();
27+
}
28+
}
29+
30+
modify = new ArrayList<>();
31+
modify.add(1);
32+
modify.add(2);
33+
modify.add(3);
34+
35+
modify.removeIf(integer -> integer == 2);
36+
System.out.println(modify.toString());
37+
}
38+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package javarecipies.generics;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class GenericMethod {
7+
<T> void printType(T item) {
8+
9+
System.out.println(item);
10+
11+
}
12+
13+
public static void main( String args[] ) {
14+
GenericMethod demo = new GenericMethod();
15+
demo.<String>printType("string");
16+
demo.<Integer>printType(5);
17+
demo.printType(23.23f);
18+
19+
List<Employee> list = new ArrayList<>();
20+
save(list);
21+
22+
}
23+
24+
25+
//upper bound on generics
26+
static void save(List<? extends Person> person) {
27+
28+
}
29+
}
30+
class Person {
31+
32+
}
33+
class Employee extends Person {
34+
35+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package javarecipies.java8;
2+
3+
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.function.Consumer;
9+
import java.lang.Integer;
10+
11+
public class Java8ForEachExample {
12+
13+
public static void main(String[] args) {
14+
15+
//creating sample Collection
16+
List<Integer> myList = new ArrayList<Integer>();
17+
for(int i=0; i<10; i++) myList.add(i);
18+
19+
//traversing using Iterator
20+
Iterator<Integer> it = myList.iterator();
21+
while(it.hasNext()){
22+
Integer i = it.next();
23+
System.out.println("Iterator Value::"+i);
24+
}
25+
/**
26+
*
27+
* underlying implementation :
28+
default void forEach(Consumer<? super T> action) {
29+
Objects.requireNonNull(action);
30+
for (T t : this) {
31+
action.accept(t);
32+
}
33+
}
34+
35+
*/
36+
//traversing through forEach method of Iterable with anonymous class
37+
myList.forEach(new Consumer<Integer>() {
38+
39+
public void accept(Integer t) {
40+
System.out.println("forEach anonymous class Value::"+t);
41+
}
42+
43+
});
44+
45+
//traversing with Consumer interface implementation
46+
MyConsumer action = new MyConsumer();
47+
myList.forEach(action);
48+
49+
}
50+
51+
}
52+
53+
//Consumer implementation that can be reused
54+
class MyConsumer implements Consumer<Integer>{
55+
56+
public void accept(Integer t) {
57+
System.out.println("Consumer impl Value::"+t);
58+
}
59+
60+
61+
}

Diff for: src/main/java/javarecipies/java8/LambdaDemo.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package javarecipies.java8;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
import java.util.function.Function;
6+
7+
public class LambdaDemo {
8+
public static void main(String args[]) {
9+
10+
DoStuff ds1 = () -> System.out.println("I ran via lambda expression");
11+
DoStuff ds2 = new DoStuff() {
12+
public void work() {
13+
System.out.println("I ran via anonymous class");
14+
}
15+
};
16+
ds1.work();
17+
ds2.work();
18+
19+
20+
Comparator<Integer> descendingComparator = (i1, i2) -> {
21+
return i2 - i1;
22+
};
23+
PriorityQueue<Integer> q = new PriorityQueue<>(descendingComparator);
24+
25+
// add integers to q in ascending order
26+
for(int i=0;i<=10;i++) {
27+
q.add(i);
28+
}
29+
30+
// verify the numbers are printed in descending order
31+
for(int i=0;i<=10;i++) {
32+
System.out.println(q.poll());
33+
}
34+
35+
RaiseToPower square = (x) -> {return x*x;};
36+
RaiseToPower cube = (x) -> {return x*x*x;};
37+
System.out.println(square.raiseToX(3));
38+
39+
40+
Function<Integer, Integer> power = (a) -> a*a;
41+
System.out.println(power.apply(3));
42+
}
43+
}
44+
45+
// A functional interface
46+
interface DoStuff {
47+
void work();
48+
}
49+
interface RaiseToPower {
50+
51+
int raiseToX(int x);
52+
53+
}
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package javarecipies.java8;
2+
3+
import java.util.Comparator;
4+
import java.util.function.Consumer;
5+
import java.util.function.Function;
6+
7+
public class MethodReferences {
8+
9+
public static void main(String[] args) {
10+
11+
Function<Person, Integer> age = Person::getAge;
12+
Function<Person, Integer> height = Person::getHeight;
13+
14+
Person person = new Person();
15+
person.setAge(23);
16+
person.setHeight(170);
17+
18+
System.out.println(age.apply(person));
19+
System.out.println(height.apply(person));
20+
21+
Consumer<String> printer = System.out::println;
22+
printer.accept("Hello World");
23+
24+
25+
Comparator<Person> allComparator =
26+
Comparator.comparing(Person::getFirstName)
27+
.thenComparing(Person::getLastName)
28+
.thenComparing(Person::getHeight);
29+
30+
}
31+
32+
33+
}
34+
class Person implements Comparable{
35+
private int age;
36+
private int height;
37+
private String firstName;
38+
private String lastName;
39+
private Person dad;
40+
41+
public Person getDad() {
42+
return dad;
43+
}
44+
45+
public void setDad(Person dad) {
46+
this.dad = dad;
47+
}
48+
49+
public Person() {
50+
}
51+
52+
public String getFirstName() {
53+
return firstName;
54+
}
55+
56+
public void setFirstName(String firstName) {
57+
this.firstName = firstName;
58+
}
59+
60+
public String getLastName() {
61+
return lastName;
62+
}
63+
64+
public void setLastName(String lastName) {
65+
this.lastName = lastName;
66+
}
67+
68+
public int getHeight() {
69+
return height;
70+
}
71+
72+
public void setHeight(int height) {
73+
this.height = height;
74+
}
75+
76+
public int getAge() {
77+
return age;
78+
}
79+
80+
public void setAge(int age) {
81+
this.age = age;
82+
}
83+
84+
@Override
85+
public int compareTo(Object o) {
86+
return 1;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package javarecipies.java8;
2+
3+
import edu.princeton.cs.algs4.In;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.function.*;
8+
9+
public class NewFunctionalInterfaces {
10+
11+
public static void main(String[] args) {
12+
Consumer<Object> printer = System.out::println;
13+
printer.accept("Dhananjay");
14+
15+
DoubleConsumer doubleConsumer = System.out::println;
16+
doubleConsumer.accept(2.1d);
17+
18+
Supplier<ArrayList<String>> listSupplier = ArrayList::new;
19+
List<String> list = listSupplier.get();
20+
list.add("Dhananjay");
21+
list.add("Nagargoje");
22+
printer.accept(list);
23+
24+
BiFunction<Double, Double, Double> power = Math::pow;
25+
printer.accept(power.apply(2.0d, 3.0d));
26+
27+
Predicate<Integer> isGreaterThan10 = x-> x>10;
28+
printer.accept(isGreaterThan10.test(9));
29+
30+
31+
32+
33+
34+
}
35+
36+
}

0 commit comments

Comments
 (0)