Skip to content

Commit 3a7c3af

Browse files
Merge pull request eugenp#4 from eugenp/master
merge
2 parents 4b5c120 + 831cdf2 commit 3a7c3af

18 files changed

Lines changed: 546 additions & 30 deletions

File tree

core-java/src/main/java/com/baeldung/heapsort/Heap.java renamed to algorithms/src/main/java/com/baeldung/algorithms/heapsort/Heap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.heapsort;
1+
package com.baeldung.algorithms.heapsort;
22

33
import java.util.ArrayList;
44
import java.util.Arrays;

core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java renamed to algorithms/src/test/java/com/baeldung/algorithms/heapsort/HeapUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.heapsort;
1+
package com.baeldung.algorithms.heapsort;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
public class CollectionRemoveIf {
7+
8+
public static void main(String args[]) {
9+
Collection<String> names = new ArrayList<>();
10+
names.add("John");
11+
names.add("Ana");
12+
names.add("Mary");
13+
names.add("Anthony");
14+
names.add("Mark");
15+
16+
names.removeIf(e -> e.startsWith("A"));
17+
System.out.println(String.join(",", names));
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
7+
public class Iterators {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Iterator<String> i = names.iterator();
18+
19+
while (i.hasNext()) {
20+
String e = i.next();
21+
if (e.startsWith("A")) {
22+
i.remove();
23+
}
24+
}
25+
26+
System.out.println(String.join(",", names));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamFilterAndCollector {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Collection<String> filteredCollection = names
18+
.stream()
19+
.filter(e -> !e.startsWith("A"))
20+
.collect(Collectors.toList());
21+
System.out.println(String.join(",", filteredCollection));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamPartitioningBy {
10+
11+
public static void main(String args[]) {
12+
Collection<String> names = new ArrayList<>();
13+
names.add("John");
14+
names.add("Ana");
15+
names.add("Mary");
16+
names.add("Anthony");
17+
names.add("Mark");
18+
19+
Map<Boolean, List<String>> classifiedElements = names
20+
.stream()
21+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
22+
23+
String matching = String.join(",", classifiedElements.get(Boolean.TRUE));
24+
String nonMatching = String.join(",", classifiedElements.get(Boolean.FALSE));
25+
System.out.println("Matching elements: " + matching);
26+
System.out.println("Non matching elements: " + nonMatching);
27+
}
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.removal;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
import static org.hamcrest.Matchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class RemovalUnitTest {
13+
14+
Collection<String> names;
15+
Collection<String> expected;
16+
Collection<String> removed;
17+
18+
@Before
19+
public void setupTestData() {
20+
names = new ArrayList<>();
21+
expected = new ArrayList<>();
22+
removed = new ArrayList<>();
23+
24+
names.add("John");
25+
names.add("Ana");
26+
names.add("Mary");
27+
names.add("Anthony");
28+
names.add("Mark");
29+
30+
expected.add("John");
31+
expected.add("Mary");
32+
expected.add("Mark");
33+
34+
removed.add("Ana");
35+
removed.add("Anthony");
36+
}
37+
38+
@Test
39+
public void givenCollectionOfNames_whenUsingIteratorToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
40+
Iterator<String> i = names.iterator();
41+
42+
while (i.hasNext()) {
43+
String e = i.next();
44+
if (e.startsWith("A")) {
45+
i.remove();
46+
}
47+
}
48+
49+
assertThat(names, is(expected));
50+
}
51+
52+
@Test
53+
public void givenCollectionOfNames_whenUsingRemoveIfToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
54+
names.removeIf(e -> e.startsWith("A"));
55+
assertThat(names, is(expected));
56+
}
57+
58+
@Test
59+
public void givenCollectionOfNames_whenUsingStreamToFilterAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
60+
Collection<String> filteredCollection = names
61+
.stream()
62+
.filter(e -> !e.startsWith("A"))
63+
.collect(Collectors.toList());
64+
assertThat(filteredCollection, is(expected));
65+
}
66+
67+
@Test
68+
public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() {
69+
Map<Boolean, List<String>> classifiedElements = names
70+
.stream()
71+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
72+
73+
assertThat(classifiedElements.get(Boolean.TRUE), is(expected));
74+
assertThat(classifiedElements.get(Boolean.FALSE), is(removed));
75+
}
76+
77+
}
Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.jackson.xmlToJson;
22

3-
4-
import com.fasterxml.jackson.core.JsonProcessingException;
53
import com.fasterxml.jackson.databind.JsonNode;
64
import com.fasterxml.jackson.databind.ObjectMapper;
75
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@@ -14,43 +12,32 @@
1412
public class XmlToJsonUnitTest {
1513

1614
@Test
17-
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
15+
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{
1816
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
1917

20-
try {
21-
XmlMapper xmlMapper = new XmlMapper();
22-
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
18+
XmlMapper xmlMapper = new XmlMapper();
19+
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
2320

24-
assertEquals(poppy.getName(), "Poppy");
25-
assertEquals(poppy.getColor(), Color.RED);
26-
assertEquals(poppy.getPetals(), new Integer(9));
21+
assertEquals(poppy.getName(), "Poppy");
22+
assertEquals(poppy.getColor(), Color.RED);
23+
assertEquals(poppy.getPetals(), new Integer(9));
2724

28-
ObjectMapper mapper = new ObjectMapper();
29-
String json = mapper.writeValueAsString(poppy);
25+
ObjectMapper mapper = new ObjectMapper();
26+
String json = mapper.writeValueAsString(poppy);
3027

31-
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
32-
System.out.println(json);
33-
} catch(IOException e) {
34-
e.printStackTrace();
35-
}
28+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
3629
}
3730

3831
@Test
39-
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
32+
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException {
4033
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
4134

42-
try {
43-
XmlMapper xmlMapper = new XmlMapper();
44-
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
45-
46-
ObjectMapper jsonMapper = new ObjectMapper();
47-
String json = jsonMapper.writeValueAsString(node);
35+
XmlMapper xmlMapper = new XmlMapper();
36+
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
4837

49-
System.out.println(json);
38+
ObjectMapper jsonMapper = new ObjectMapper();
39+
String json = jsonMapper.writeValueAsString(node);
5040

51-
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
52-
} catch(IOException e) {
53-
e.printStackTrace();
54-
}
41+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
5542
}
5643
}

‎jib/pom.xml‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>jib</artifactId>
5+
<version>0.1-SNAPSHOT</version>
6+
<name>jib</name>
7+
8+
<parent>
9+
<artifactId>parent-boot-2</artifactId>
10+
<groupId>com.baeldung</groupId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
<relativePath>../parent-boot-2</relativePath>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
</dependencies>
21+
22+
<properties>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
<plugin>
33+
<groupId>com.google.cloud.tools</groupId>
34+
<artifactId>jib-maven-plugin</artifactId>
35+
<version>0.9.10</version>
36+
<configuration>
37+
<to>
38+
<image>registry.hub.docker.com/baeldungjib/jib-spring-boot-app</image>
39+
</to>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
<repositories>
46+
<repository>
47+
<id>spring-releases</id>
48+
<url>https://repo.spring.io/libs-release</url>
49+
</repository>
50+
</repositories>
51+
<pluginRepositories>
52+
<pluginRepository>
53+
<id>spring-releases</id>
54+
<url>https://repo.spring.io/libs-release</url>
55+
</pluginRepository>
56+
</pluginRepositories>
57+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}

0 commit comments

Comments
 (0)