Skip to content

Commit 2937515

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 102ac02 + b5c7ffe commit 2937515

91 files changed

Lines changed: 2631 additions & 544 deletions

File tree

Some content is hidden

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

‎core-java-8/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
3434
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
3535
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)
36+
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.defaultstaticinterfacemethods.application;
2+
3+
import com.baeldung.defaultstaticinterfacemethods.model.Car;
4+
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
5+
6+
public class Application {
7+
8+
public static void main(String[] args) {
9+
10+
Vehicle car = new Car("BMW");
11+
System.out.println(car.getBrand());
12+
System.out.println(car.speedUp());
13+
System.out.println(car.slowDown());
14+
System.out.println(car.turnAlarmOn());
15+
System.out.println(car.turnAlarmOff());
16+
System.out.println(Vehicle.getHorsePower(2500, 480));
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public interface Alarm {
4+
5+
default String turnAlarmOn() {
6+
return "Turning the alarm on.";
7+
}
8+
9+
default String turnAlarmOff() {
10+
return "Turning the alarm off.";
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class Car implements Vehicle {
4+
5+
private final String brand;
6+
7+
public Car(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The car is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The car is slowing down.";
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class Motorbike implements Vehicle {
4+
5+
private final String brand;
6+
7+
public Motorbike(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The motorbike is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The motorbike is slowing down.";
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class MultiAlarmCar implements Vehicle, Alarm {
4+
5+
private final String brand;
6+
7+
public MultiAlarmCar(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The motorbike is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The mootorbike is slowing down.";
24+
}
25+
26+
@Override
27+
public String turnAlarmOn() {
28+
return Vehicle.super.turnAlarmOn() + " " + Alarm.super.turnAlarmOn();
29+
}
30+
31+
@Override
32+
public String turnAlarmOff() {
33+
return Vehicle.super.turnAlarmOff() + " " + Alarm.super.turnAlarmOff();
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public interface Vehicle {
4+
5+
String getBrand();
6+
7+
String speedUp();
8+
9+
String slowDown();
10+
11+
default String turnAlarmOn() {
12+
return "Turning the vehice alarm on.";
13+
}
14+
15+
default String turnAlarmOff() {
16+
return "Turning the vehicle alarm off.";
17+
}
18+
19+
static int getHorsePower(int rpm, int torque) {
20+
return (rpm * torque) / 5252;
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.iterators;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
/**
8+
* Source code https://github.com/eugenp/tutorials
9+
*
10+
* @author Santosh Thakur
11+
*/
12+
13+
public class Iterators {
14+
15+
public static int failFast1() {
16+
ArrayList<Integer> numbers = new ArrayList<>();
17+
18+
numbers.add(10);
19+
numbers.add(20);
20+
numbers.add(30);
21+
numbers.add(40);
22+
23+
Iterator<Integer> iterator = numbers.iterator();
24+
while (iterator.hasNext()) {
25+
Integer number = iterator.next();
26+
numbers.add(50);
27+
}
28+
29+
return numbers.size();
30+
}
31+
32+
public static int failFast2() {
33+
ArrayList<Integer> numbers = new ArrayList<>();
34+
numbers.add(10);
35+
numbers.add(20);
36+
numbers.add(30);
37+
numbers.add(40);
38+
39+
Iterator<Integer> iterator = numbers.iterator();
40+
while (iterator.hasNext()) {
41+
if (iterator.next() == 30) {
42+
// will not throw Exception
43+
iterator.remove();
44+
}
45+
}
46+
47+
System.out.println("using iterator's remove method = " + numbers);
48+
49+
iterator = numbers.iterator();
50+
while (iterator.hasNext()) {
51+
if (iterator.next() == 40) {
52+
// will throw Exception on
53+
// next call of next() method
54+
numbers.remove(2);
55+
}
56+
}
57+
58+
return numbers.size();
59+
}
60+
61+
public static int failSafe1() {
62+
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
63+
64+
map.put("First", 10);
65+
map.put("Second", 20);
66+
map.put("Third", 30);
67+
map.put("Fourth", 40);
68+
69+
Iterator<String> iterator = map.keySet()
70+
.iterator();
71+
72+
while (iterator.hasNext()) {
73+
String key = iterator.next();
74+
map.put("Fifth", 50);
75+
}
76+
77+
return map.size();
78+
}
79+
80+
}

‎core-java-8/src/test/java/com/baeldung/counter/CounterStatistics.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Random;
56

67
import org.openjdk.jmh.annotations.Benchmark;
78
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -18,8 +19,18 @@ public class CounterStatistics {
1819
private static final Map<String, MutableInteger> counterWithMutableIntMap = new HashMap<>();
1920
private static final Map<String, int[]> counterWithIntArrayMap = new HashMap<>();
2021
private static final Map<String, Long> counterWithLongWrapperMap = new HashMap<>();
22+
private static final Map<String, Long> counterWithLongWrapperStreamMap = new HashMap<>();
23+
24+
static {
25+
CounterUtil.COUNTRY_NAMES = new String[10000];
26+
final String prefix = "NewString";
27+
Random random = new Random();
28+
for (int i=0; i<10000; i++) {
29+
CounterUtil.COUNTRY_NAMES[i] = new String(prefix + random.nextInt(1000));
30+
}
31+
}
2132

22-
@Benchmark
33+
@Benchmark
2334
public void wrapperAsCounter() {
2435
CounterUtil.counterWithWrapperObject(counterMap);
2536
}
@@ -28,6 +39,11 @@ public void wrapperAsCounter() {
2839
public void lambdaExpressionWithWrapper() {
2940
CounterUtil.counterWithLambdaAndWrapper(counterWithLongWrapperMap);
3041
}
42+
43+
@Benchmark
44+
public void parallelStreamWithWrapper() {
45+
CounterUtil.counterWithParallelStreamAndWrapper(counterWithLongWrapperStreamMap);
46+
}
3147

3248
@Benchmark
3349
public void mutableIntegerAsCounter() {

‎core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class CounterUtil {
88

9-
private final static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
9+
public static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
1010

1111
public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
1212
for (String country : COUNTRY_NAMES) {
@@ -15,9 +15,14 @@ public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
1515
}
1616

1717
public static void counterWithLambdaAndWrapper(Map<String, Long> counterMap) {
18-
counterMap.putAll(Stream.of(COUNTRY_NAMES)
18+
Stream.of(COUNTRY_NAMES)
19+
.collect(Collectors.groupingBy(k -> k, () -> counterMap, Collectors.counting()));
20+
}
21+
22+
public static void counterWithParallelStreamAndWrapper(Map<String, Long> counterMap) {
23+
Stream.of(COUNTRY_NAMES)
1924
.parallel()
20-
.collect(Collectors.groupingBy(k -> k, Collectors.counting())));
25+
.collect(Collectors.groupingBy(k -> k, () -> counterMap, Collectors.counting()));
2126
}
2227

2328
public static class MutableInteger {
@@ -49,4 +54,4 @@ public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
4954
}
5055
}
5156

52-
}
57+
}

0 commit comments

Comments
 (0)