Skip to content

Commit 5f8df93

Browse files
item 45 added
1 parent be582be commit 5f8df93

File tree

9 files changed

+186
-5
lines changed

9 files changed

+186
-5
lines changed

src/com/example/Item10And11OverridingEqualsAndHashCode/main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public static void main(String[] args) {
2424

2525
DenemeSinif d = new DenemeSinif(1,2);
2626
DenemeSinif d2 = new DenemeSinif(1,2);
27-
System.out.println(d==d2); //false stack de ayrı sekilde referans tutulur
28-
System.out.println(d.equals(d2));
27+
System.out.println(d==d2); //false stack de ayrı sekilde referans tutulur.
28+
System.out.println(d.equals(d2)); //false
2929
System.out.println(d.hashCode());//different to d2 hashcode
30-
System.out.println(d2.hashCode());
30+
System.out.println(d2.hashCode());//different to d1 hashcode
3131

3232
DenemeSinif deneme= new DenemeSinif(10,11);
3333
DenemeSinif deneme2= new DenemeSinif(10,11);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.Item42LambdasToAnonymous;
2+
3+
public class Main {
4+
5+
public String exampleFour(){
6+
return "ornek";
7+
}
8+
public static void main(String[] args) {
9+
10+
//lambda : kendi başına tanımlanabilen , parametre kabul eden ve döndürülebilen fonksiyondur.
11+
12+
Yazdir y= ()->{
13+
return "yazdim";
14+
};
15+
System.out.println(y.yazBeni());
16+
17+
18+
String param ="Parametre";
19+
YazdirParametre y2=(paramAlacak)->{
20+
return paramAlacak + " selam";
21+
};
22+
System.out.println(y2.yazdirParametreyi(param));
23+
24+
25+
26+
Main ma=new Main();
27+
Yazdir write =ma::exampleFour;
28+
System.out.println(write.yazBeni());
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.Item42LambdasToAnonymous;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import static java.util.Comparator.comparingInt;
8+
9+
// Sorting with function objects (Pages 193-4)
10+
public class SortFourWays {
11+
12+
public static void main(String[] args) {
13+
14+
String[] nameList = {"ali","veli","furkan","berna","yennefer","geralt","vesemir","ciri"};
15+
List<String> words = Arrays.asList(nameList);
16+
17+
// Anonymous class instance as a function object - obsolete! (Page 193)
18+
Collections.sort(words, new Comparator<String>() {
19+
public int compare(String s1, String s2) {
20+
return Integer.compare(s1.length(), s2.length());
21+
}
22+
});
23+
System.out.println(words);
24+
Collections.shuffle(words);
25+
26+
// Lambda expression as function object (replaces anonymous class) (Page 194)
27+
Collections.sort(words, (s1, s2) -> Integer.compare(s1.length(), s2.length()));
28+
System.out.println(words);
29+
Collections.shuffle(words);
30+
31+
// Comparator construction method (with method reference) in place of lambda
32+
// (Page 194) Bu terminoloji madde 43 te anlatılıyor burada değinmeyeceğim.
33+
Collections.sort(words, comparingInt(String::length));
34+
System.out.println(words);
35+
Collections.shuffle(words);
36+
37+
// Default method List.sort in conjunction with comparator construction method
38+
// (Page 194)
39+
words.sort(comparingInt(String::length));
40+
System.out.println(words);
41+
42+
}
43+
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.Item42LambdasToAnonymous;
2+
3+
public interface Yazdir {
4+
5+
public String yazBeni();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.Item42LambdasToAnonymous;
2+
3+
public interface YazdirParametre {
4+
5+
public String yazdirParametreyi(String param);
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.Item43MethodReferencesToLambdas;
2+
3+
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
// Frequency table implemented with map.merge, using lambda and method reference (Page 197)
8+
public class Freq {
9+
public static void main(String[] args) {
10+
11+
12+
Map<String, Integer> frequencyTable = new TreeMap<>();
13+
14+
for (String s : args)
15+
frequencyTable.merge(s, 1, (count, incr) -> count + incr ); // Lambda
16+
System.out.println(frequencyTable);
17+
18+
19+
20+
frequencyTable.clear();
21+
for (String s : args)
22+
frequencyTable.merge(s, 1, Integer::sum); // Method reference
23+
System.out.println(frequencyTable);
24+
25+
26+
}
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.Item43MethodReferencesToLambdas;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
6+
}
7+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.Item45Streams;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.IntStream;
9+
10+
public class TestDrive {
11+
12+
public static void main(String[] args) {
13+
14+
// Stream nedir? -> Java8 + sıralı paralel toplu işlemleri kolaylaştırmak için gelmiştir.
15+
/*
16+
* collections, files, veya başka streamler kaynak olabilir.
17+
*
18+
* terminal - intermediate
19+
*/
20+
21+
List<String> myList = Arrays.asList("jose saramago", "stefan zweig", "tess gerritsen","selami");
22+
23+
24+
myList
25+
.stream()
26+
.filter(s -> s.startsWith("s"))
27+
.map(String::toUpperCase)
28+
.forEach(System.out::println);
29+
30+
31+
// stream()
32+
// .intemediateOperation1()
33+
// .intemediateOperation2()
34+
// ...
35+
// .intemediateOperationN()
36+
// .terminalOperation();
37+
38+
39+
Arrays.stream(new int[] {1, 2})
40+
.map(n -> 2 * n + 1)
41+
.average()
42+
.ifPresent(System.out::println);
43+
44+
45+
for(int i = 0 ; i < 5 ; i++)
46+
{
47+
System.out.println(i);
48+
}
49+
50+
IntStream.range(0,5).forEach(System.out::println);
51+
52+
System.out.println(IntStream.range(0,10).max().getAsInt());
53+
54+
55+
Collection<Integer> a = Collections.emptyList();
56+
a.add(10);
57+
List<Integer> result = a.stream().
58+
filter(s -> s > 0).
59+
collect(Collectors.toList());
60+
61+
}
62+
63+
}

src/com/example/Main.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.example;
22

3-
import java.security.SecureRandom;
4-
53
public class Main {
64

75
public static void main(String[] args) {

0 commit comments

Comments
 (0)