1- import java .util .ArrayList ;
2- import java .util .Collections ;
3- import java .util .List ;
1+ import java .util .*;
42import java .util .function .Consumer ;
53import java .util .function .Function ;
64import java .util .function .Predicate ;
75
86
97public 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}
0 commit comments