Skip to content

Commit 09ad4cc

Browse files
author
Aleksandr Gromov
committed
learning java 8
1 parent c35e816 commit 09ad4cc

27 files changed

+345
-32
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Created by aleks on 25.09.15.
3-
*/
4-
public interface AnotherTimeClient {
1+
import java.time.ZonedDateTime;
2+
3+
public interface AnotherTimeClient extends TimeClient {
4+
ZonedDateTime getZonedDateTime(String zoneString);
55
}

java8Module/src/main/java/Box.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
public class Box<T> {
5+
private T t; // T stands for "Type"
6+
7+
public void set(T t) {
8+
this.t = t;
9+
}
10+
11+
public T get() {
12+
return t;
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Type Inference
3+
*/
4+
public class BoxDemo {
5+
public static <U> void addBox(U u,
6+
java.util.List<Box<U>> boxes) {
7+
Box<U> box = new Box<>();
8+
box.set(u);
9+
boxes.add(box);
10+
}
11+
12+
public static <U> void outputBoxes(java.util.List<Box<U>> boxes) {
13+
int counter = 0;
14+
for (Box<U> box: boxes) {
15+
U boxContents = box.get();
16+
System.out.println("Box #" + counter + " contains [" +
17+
boxContents.toString() + "]");
18+
counter++;
19+
}
20+
}
21+
22+
public static void main(String[] args) {
23+
java.util.ArrayList<Box<String>> listOfIntegerBoxes =
24+
new java.util.ArrayList<>();
25+
BoxDemo.addBox("1", listOfIntegerBoxes);
26+
BoxDemo.addBox("2", listOfIntegerBoxes);
27+
BoxDemo.addBox("3", listOfIntegerBoxes);
28+
BoxDemo.outputBoxes(listOfIntegerBoxes);
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
14

25
public interface CheckPerson {
36
boolean test(Person p);
7+
8+
default void printPersonsEmail(List<Person> roster) {
9+
10+
List<Integer> stringList = new ArrayList<>();
11+
stringList.add(1);
12+
stringList.addAll(Arrays.asList(2));
13+
14+
roster
15+
.stream()
16+
.filter(
17+
p -> p.getGender() == Person.Sex.MALE
18+
&& p.getAge() >= 18
19+
&& p.getAge() <= 25)
20+
.map(p -> p.getEmailAddress())
21+
.forEach(email -> System.out.println(email));
22+
}
423
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
/**
2-
* Created by aleks on 25.09.15.
3-
*/
4-
public interface HandleInvalidTimeZoneClient {
1+
import java.time.DateTimeException;
2+
import java.time.ZoneId;
3+
import java.time.ZonedDateTime;
4+
5+
public interface HandleInvalidTimeZoneClient extends TimeClient {
6+
default ZonedDateTime getZonedDateTime(String zoneString) {
7+
try {
8+
return ZonedDateTime.of(getLocalDateTime(), ZoneId.of(zoneString));
9+
} catch (DateTimeException e) {
10+
System.err.println("Invalid zone ID: " + zoneString +
11+
"; using the default time zone instead.");
12+
return ZonedDateTime.of(getLocalDateTime(),ZoneId.systemDefault());
13+
}
14+
}
515
}

java8Module/src/main/java/Main.java

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import java.util.ArrayList;
2-
import java.util.Collections;
3-
import java.util.List;
1+
import java.util.*;
42
import java.util.function.Consumer;
53
import java.util.function.Function;
64
import java.util.function.Predicate;
75

86

97
public class Main {
108
public static void main(String[] args) {
11-
List roster = Collections.singletonList(new Person("Alex", Person.Sex.MALE, 25, "[email protected]"));
9+
// generic method invocation
10+
Pair<Integer, String> p1 = new Pair<>(1, "apple");
11+
Pair<Integer, String> p2 = new Pair<>(2, "apple");
12+
boolean same = Util.compare(p1, p2);
13+
System.out.println("same: " + same);
14+
15+
List<Person> roster = Collections.singletonList(new Person("Alex", Person.Sex.MALE, 25, "[email protected]"));
16+
MyClass<Integer> myClass = new MyClass<>("");
1217
//printPersonsOlderThan(Collections.singletonList(new Person("Alex", Person.Sex.MALE, 25)), 25);
1318

1419
//
@@ -41,14 +46,40 @@ public static void main(String[] args) {
4146
// && p.getAge() >= 18
4247
// && p.getAge() <= 25, p -> p.printPerson());
4348

44-
processElements(
45-
roster,
46-
(Person p) -> p.getGender() == Person.Sex.MALE
47-
&& p.getAge() >= 18
48-
&& p.getAge() <= 25,
49-
p -> p.getEmailAddress(),
50-
email -> System.out.println(email)
51-
);
49+
// processElements(
50+
// roster,
51+
// p -> p.getGender() == Person.Sex.MALE
52+
// && p.getAge() >= 18
53+
// && p.getAge() <= 25,
54+
// p -> p.getEmailAddress(),
55+
// email -> System.out.println(email)
56+
// );
57+
58+
;
59+
60+
// TimeClient myTimeClient = new SimpleTimeClient();
61+
// System.out.println("Current time: " + myTimeClient.toString());
62+
// System.out.println("Time in Togliatti: " +
63+
// myTimeClient.getZonedDateTime("Blah blah").toString());
64+
// System.out.println(returnForMain(new Person("Alex", null, 25,"[email protected]")));
65+
66+
/* Target Types */
67+
processStringList(Collections.emptyList());
68+
}
69+
70+
static void processStringList(List<String> stringList) {
71+
// process stringList
72+
}
73+
74+
public static Person.Sex returnForMain(Object person) {
75+
try {
76+
Person.Sex sex = ((Person) person).getGender();
77+
if(sex == null) return Person.Sex.MALE;
78+
return sex;
79+
} catch (NullPointerException | ClassCastException e) {
80+
System.out.println("e: " + e.getMessage());
81+
return Person.Sex.MALE;
82+
}
5283
}
5384

5485
public static void printPersonsOlderThan(List<Person> roster, int age) {
@@ -68,7 +99,7 @@ public static void printPersons(List<Person> roster, CheckPerson tester) {
6899
}
69100

70101
public static void printPersonsWithPredicate(
71-
List<Person> roster, Predicate<Person> tester) {
102+
List<Person> roster, Predicate<Person> tester) {
72103
for (Person p : roster) {
73104
if (tester.test(p)) {
74105
p.printPerson();
@@ -85,15 +116,73 @@ public static void printPersons(List<Person> roster, Predicate<Person> tester,Co
85116
}
86117

87118
public static <X, Y> void processElements(
88-
Iterable<X> source,
89-
Predicate<X> tester,
90-
Function<X, Y> mapper,
119+
Iterable<Person> source,
120+
Predicate<Person> tester,
121+
Function<Person, Y> mapper,
91122
Consumer<Y> block) {
92-
for (X p : source) {
123+
for (Person p : source) {
93124
if (tester.test(p)) {
94125
Y data = mapper.apply(p);
95126
block.accept(data);
96127
}
97128
}
98129
}
130+
131+
public static void testMethod() {
132+
List<String> temp = new ArrayList<>();
133+
// List<Integer> listOfIntegers =
134+
// new ArrayList<>(Arrays.asList(intArray));
135+
try {
136+
System.out.println("temp: " +
137+
temp
138+
.stream()
139+
.filter(l -> l.compareTo("111") == 0)
140+
.findFirst());
141+
} catch (NoSuchElementException e) {
142+
System.out.println("Hi! No value present!");
143+
try {
144+
throw new Exception();
145+
} catch (Exception e1) {
146+
e1.printStackTrace();
147+
}
148+
}
149+
}
150+
151+
public static void ex1() {
152+
List<String> temp = new ArrayList<>();
153+
temp.add("111gfggf");
154+
temp.add("222ggg");
155+
temp.add("333ggg");
156+
// List<Integer> listOfIntegers =
157+
// new ArrayList<>(Arrays.asList(intArray));
158+
String[] condition = {"111", "222", "333"};
159+
List<String> l = Arrays.asList(condition);
160+
System.out.println(temp.get(0).contains(l.get(0)));
161+
}
162+
163+
public static void testMethod2() {
164+
List<String> temp = new ArrayList<>();
165+
temp.add("111gfggf");
166+
temp.add("222ggg");
167+
temp.add("333ggg");
168+
// List<Integer> listOfIntegers =
169+
// new ArrayList<>(Arrays.asList(intArray));
170+
String[] condition = {"111", "222", "333"};
171+
System.out.println("temp: " +
172+
temp
173+
.stream()
174+
.filter(chain(x -> (y -> y.contains(x)), condition))
175+
.reduce("", (acc, str) -> acc + str + ". "));
176+
}
177+
public static void testMethod3() {
178+
String[] s = {"111", "222", "333"};
179+
Arrays.stream(s);
180+
}
181+
182+
public static <T> Predicate<T> chain (Function<T,Predicate<T>> mapFunction, T[] args) {
183+
return Arrays.asList(args)
184+
.stream()
185+
.map(mapFunction::apply)
186+
.reduce(p->false, Predicate::or);
187+
}
99188
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Created by aleks on 29.10.15.
3+
*/
4+
public class MyClass<X> {
5+
<T> MyClass(T t) {
6+
7+
}
8+
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Pair<K, V> {
2+
3+
private K key;
4+
private V value;
5+
6+
public Pair(K key, V value) {
7+
this.key = key;
8+
this.value = value;
9+
}
10+
11+
public void setKey(K key) { this.key = key; }
12+
public void setValue(V value) { this.value = value; }
13+
public K getKey() { return key; }
14+
public V getValue() { return value; }
15+
}
Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
1-
/**
2-
* Created by aleks on 25.09.15.
3-
*/
4-
public class SimpleTimeClient {
1+
import java.time.LocalDate;
2+
import java.time.LocalDateTime;
3+
import java.time.LocalTime;
4+
5+
public class SimpleTimeClient implements TimeClient {
6+
private LocalDateTime dateAndTime;
7+
8+
public SimpleTimeClient() {
9+
dateAndTime = LocalDateTime.now();
10+
}
11+
12+
public void setTime(int hour, int minute, int second) {
13+
LocalDate currentDate = LocalDate.from(dateAndTime);
14+
LocalTime timeToSet = LocalTime.of(hour, minute, second);
15+
dateAndTime = LocalDateTime.of(currentDate, timeToSet);
16+
}
17+
18+
public void setDate(int day, int month, int year) {
19+
LocalDate dateToSet = LocalDate.of(day, month, year);
20+
LocalTime currentTime = LocalTime.from(dateAndTime);
21+
dateAndTime = LocalDateTime.of(dateToSet, currentTime);
22+
}
23+
24+
public void setDateAndTime(int day, int month, int year,
25+
int hour, int minute, int second) {
26+
LocalDate dateToSet = LocalDate.of(day, month, year);
27+
LocalTime timeToSet = LocalTime.of(hour, minute, second);
28+
dateAndTime = LocalDateTime.of(dateToSet, timeToSet);
29+
}
30+
31+
public LocalDateTime getLocalDateTime() {
32+
return dateAndTime;
33+
}
34+
35+
public String toString() {
36+
return dateAndTime.toString();
37+
}
38+
39+
public static void main(String... args) {
40+
TimeClient myTimeClient = new SimpleTimeClient();
41+
System.out.println(myTimeClient.toString());
42+
}
543
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.IOException;
2+
import java.util.EmptyStackException;
3+
4+
/**
5+
* Created by aleks on 29.10.15.
6+
*/
7+
public class Tester1 {
8+
public static void test1() {
9+
String s = null;
10+
s.hashCode();
11+
}
12+
public static void test2() {
13+
14+
String s = null;
15+
throw new OutOfMemoryError();
16+
17+
}
18+
public static void test3() {
19+
20+
String s = null;
21+
throw new EmptyStackException();
22+
23+
}
24+
}

0 commit comments

Comments
 (0)