Skip to content

Commit 9c8a505

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents acfc540 + c2a300b commit 9c8a505

66 files changed

Lines changed: 2817 additions & 1028 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.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung
2+
3+
class Car implements VehicleTrait {}

‎core-groovy/src/main/groovy/com/baeldung/traits/UserTrait.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait UserTrait implements Human {
2222
msg
2323
}
2424

25-
def whoAmI() {
25+
def self() {
2626
return this
2727
}
2828

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung
2+
3+
trait VehicleTrait extends WheelTrait {
4+
5+
String showWheels() {
6+
return "Num of Wheels $noOfWheels"
7+
}
8+
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung
2+
3+
trait WheelTrait {
4+
5+
int noOfWheels
6+
7+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.map
2+
3+
import static org.junit.Assert.*
4+
import org.junit.Test
5+
6+
class MapUnitTest {
7+
8+
@Test
9+
void whenUsingEach_thenMapIsIterated() {
10+
def map = [
11+
'FF0000' : 'Red',
12+
'00FF00' : 'Lime',
13+
'0000FF' : 'Blue',
14+
'FFFF00' : 'Yellow'
15+
]
16+
17+
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
18+
}
19+
20+
@Test
21+
void whenUsingEachWithEntry_thenMapIsIterated() {
22+
def map = [
23+
'E6E6FA' : 'Lavender',
24+
'D8BFD8' : 'Thistle',
25+
'DDA0DD' : 'Plum',
26+
]
27+
28+
map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
29+
}
30+
31+
@Test
32+
void whenUsingEachWithKeyAndValue_thenMapIsIterated() {
33+
def map = [
34+
'000000' : 'Black',
35+
'FFFFFF' : 'White',
36+
'808080' : 'Gray'
37+
]
38+
39+
map.each { key, val ->
40+
println "Hex Code: $key = Color Name $val"
41+
}
42+
}
43+
44+
@Test
45+
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() {
46+
def map = [
47+
'800080' : 'Purple',
48+
'4B0082' : 'Indigo',
49+
'6A5ACD' : 'Slate Blue'
50+
]
51+
52+
map.eachWithIndex { entry, index ->
53+
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
54+
println "$indent Hex Code: $entry.key = Color Name: $entry.value"
55+
}
56+
}
57+
58+
@Test
59+
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() {
60+
def map = [
61+
'FFA07A' : 'Light Salmon',
62+
'FF7F50' : 'Coral',
63+
'FF6347' : 'Tomato',
64+
'FF4500' : 'Orange Red'
65+
]
66+
67+
map.eachWithIndex { key, val, index ->
68+
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
69+
println "$indent Hex Code: $key = Color Name: $val"
70+
}
71+
}
72+
73+
@Test
74+
void whenUsingForLoop_thenMapIsIterated() {
75+
def map = [
76+
'2E8B57' : 'Seagreen',
77+
'228B22' : 'Forest Green',
78+
'008000' : 'Green'
79+
]
80+
81+
for (entry in map) {
82+
println "Hex Code: $entry.key = Color Name: $entry.value"
83+
}
84+
}
85+
}

‎core-groovy/src/test/groovy/com/baeldung/traits/TraitsUnitTest.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TraitsUnitTest extends Specification {
5757

5858
def 'Should return employee instance when using Employee.whoAmI method' () {
5959
when:
60-
def emp = employee.whoAmI()
60+
def emp = employee.self()
6161
then:
6262
emp
6363
emp instanceof Employee

‎core-java-collections-list/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
2828
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)
2929
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
30+
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)

‎core-java-collections-list/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
<artifactId>colt</artifactId>
5353
<version>${colt.version}</version>
5454
</dependency>
55+
56+
<dependency>
57+
<groupId>org.openjdk.jmh</groupId>
58+
<artifactId>jmh-core</artifactId>
59+
<version>${jmh-core.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.openjdk.jmh</groupId>
63+
<artifactId>jmh-generator-annprocess</artifactId>
64+
<version>${jmh-core.version}</version>
65+
</dependency>
5566
</dependencies>
5667

5768
<properties>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.list.primitive;
2+
3+
import it.unimi.dsi.fastutil.ints.IntArrayList;
4+
import gnu.trove.list.array.TIntArrayList;
5+
import org.openjdk.jmh.annotations.*;
6+
import org.openjdk.jmh.runner.Runner;
7+
import org.openjdk.jmh.runner.options.Options;
8+
import org.openjdk.jmh.runner.options.OptionsBuilder;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.concurrent.TimeUnit;
13+
14+
@BenchmarkMode(Mode.SingleShotTime)
15+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
16+
@Measurement(batchSize = 100000, iterations = 10)
17+
@Warmup(batchSize = 100000, iterations = 10)
18+
@State(Scope.Thread)
19+
public class PrimitivesListPerformance {
20+
21+
private List<Integer> arrayList = new ArrayList<>();
22+
private TIntArrayList tList = new TIntArrayList();
23+
private cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();
24+
private IntArrayList fastUtilList = new IntArrayList();
25+
26+
private int getValue = 10;
27+
28+
@Benchmark
29+
public boolean addArrayList() {
30+
return arrayList.add(getValue);
31+
}
32+
33+
@Benchmark
34+
public boolean addTroveIntList() {
35+
return tList.add(getValue);
36+
}
37+
38+
@Benchmark
39+
public void addColtIntList() {
40+
coltList.add(getValue);
41+
}
42+
43+
@Benchmark
44+
public boolean addFastUtilIntList() {
45+
return fastUtilList.add(getValue);
46+
}
47+
48+
@Benchmark
49+
public int getArrayList() {
50+
return arrayList.get(getValue);
51+
}
52+
53+
@Benchmark
54+
public int getTroveIntList() {
55+
return tList.get(getValue);
56+
}
57+
58+
@Benchmark
59+
public int getColtIntList() {
60+
return coltList.get(getValue);
61+
}
62+
63+
@Benchmark
64+
public int getFastUtilIntList() {
65+
return fastUtilList.getInt(getValue);
66+
}
67+
68+
@Benchmark
69+
public boolean containsArrayList() {
70+
return arrayList.contains(getValue);
71+
}
72+
73+
@Benchmark
74+
public boolean containsTroveIntList() {
75+
return tList.contains(getValue);
76+
}
77+
78+
@Benchmark
79+
public boolean containsColtIntList() {
80+
return coltList.contains(getValue);
81+
}
82+
83+
@Benchmark
84+
public boolean containsFastUtilIntList() {
85+
return fastUtilList.contains(getValue);
86+
}
87+
88+
public static void main(String[] args) throws Exception {
89+
Options options = new OptionsBuilder()
90+
.include(PrimitivesListPerformance.class.getSimpleName()).threads(1)
91+
.forks(1).shouldFailOnError(true)
92+
.shouldDoGC(true)
93+
.jvmArgs("-server").build();
94+
new Runner(options).run();
95+
}
96+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.gson.conversion;
2+
3+
import com.google.gson.*;
4+
import org.junit.Assert;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class JsonObjectConversionsUnitTest {
8+
9+
@Test
10+
void whenUsingJsonParser_thenConvertToJsonObject() throws Exception {
11+
// Example 1: Using JsonParser
12+
String json = "{ \"name\": \"Baeldung\", \"java\": true }";
13+
14+
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
15+
16+
Assert.assertTrue(jsonObject.isJsonObject());
17+
Assert.assertTrue(jsonObject.get("name").getAsString().equals("Baeldung"));
18+
Assert.assertTrue(jsonObject.get("java").getAsBoolean() == true);
19+
}
20+
21+
@Test
22+
void whenUsingGsonInstanceFromJson_thenConvertToJsonObject() throws Exception {
23+
// Example 2: Using fromJson
24+
String json = "{ \"name\": \"Baeldung\", \"java\": true }";
25+
26+
JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);
27+
28+
Assert.assertTrue(convertedObject.isJsonObject());
29+
Assert.assertTrue(convertedObject.get("name").getAsString().equals("Baeldung"));
30+
Assert.assertTrue(convertedObject.get("java").getAsBoolean() == true);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)