Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
fe31164645
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-genetic</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-genetic</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -61,4 +61,5 @@
|
|||
<commons-codec.version>1.11</commons-codec.version>
|
||||
</properties>
|
||||
|
||||
|
||||
</project>
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-miscellaneous-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-1</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-miscellaneous-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-sorting</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-sorting</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>annotation-processing</artifactId>
|
||||
<name>annotation-processing</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>annotation-user</artifactId>
|
||||
<name>annotation-user</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>annotations</artifactId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>annotations</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>annotations</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
|
|
@ -3,10 +3,9 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-avro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Apache Avro</name>
|
||||
<name>apache-avro</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>apache-bval</groupId>
|
||||
<artifactId>apache-bval</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-bval</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<artifactId>apache-curator</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>apache-curator</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-aegis</artifactId>
|
||||
<name>cxf-aegis</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-introduction</artifactId>
|
||||
<name>cxf-introduction</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-jaxrs-implementation</artifactId>
|
||||
<name>cxf-jaxrs-implementation</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-spring</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<name>cxf-spring</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-cxf</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sse-jaxrs</artifactId>
|
||||
<name>sse-jaxrs</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>sse-jaxrs-client</artifactId>
|
||||
<name>sse-jaxrs-client</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -10,8 +12,6 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>sse-jaxrs-client</artifactId>
|
||||
|
||||
<properties>
|
||||
<cxf-version>3.2.0</cxf-version>
|
||||
</properties>
|
||||
|
@ -21,7 +21,6 @@
|
|||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>singleEvent</id>
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>sse-jaxrs-server</artifactId>
|
||||
<name>sse-jaxrs-server</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -10,9 +13,6 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>sse-jaxrs-server</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
|
|
|
@ -3,10 +3,9 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-geode</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>apache-geode</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-opennlp</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>apache-opennlp</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-poi</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-poi</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,8 +3,9 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.pulsar</groupId>
|
||||
<artifactId>pulsar-java</artifactId>
|
||||
<artifactId>apache-pulsar</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<name>apache-pulsar</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-shiro</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>apache-shiro</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-thrift</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-thrift</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-tika</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-tika</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-zookeeper</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-zookeeper</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<groupId>com.baeldung.examples</groupId>
|
||||
<artifactId>asm</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>asm</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.atomix.io</groupId>
|
||||
<artifactId>atomix</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>atomix</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>axon</artifactId>
|
||||
<name>axon</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
|
|
@ -3,8 +3,9 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cas-server</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.0</version>
|
||||
<name>cas-server</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
### Relevant Articles:
|
||||
- [CDI Interceptor vs Spring AspectJ](http://www.baeldung.com/cdi-interceptor-vs-spring-aspectj)
|
||||
- [An Introduction to CDI (Contexts and Dependency Injection) in Java](http://www.baeldung.com/java-ee-cdi)
|
||||
- [Introduction to the Event Notification Model in CDI 2.0](https://www.baeldung.com/cdi-event-notification)
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cdi</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>cdi</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-groovy</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>core-groovy</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-9</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
/**
|
||||
* Demonstrates the different ways to loop over
|
||||
* the elements of a list.
|
||||
*/
|
||||
public class WaysToIterate {
|
||||
|
||||
List<String> countries = Arrays.asList("Germany", "Panama", "Australia");
|
||||
|
||||
/**
|
||||
* Iterate over a list using a basic for loop
|
||||
*/
|
||||
public void iterateWithForLoop() {
|
||||
for (int i = 0; i < countries.size(); i++) {
|
||||
System.out.println(countries.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the enhanced for loop
|
||||
*/
|
||||
public void iterateWithEnhancedForLoop() {
|
||||
for (String country : countries) {
|
||||
System.out.println(country);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using an Iterator
|
||||
*/
|
||||
public void iterateWithIterator() {
|
||||
Iterator<String> countriesIterator = countries.iterator();
|
||||
while(countriesIterator.hasNext()) {
|
||||
System.out.println(countriesIterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using a ListIterator
|
||||
*/
|
||||
public void iterateWithListIterator() {
|
||||
ListIterator<String> listIterator = countries.listIterator();
|
||||
while(listIterator.hasNext()) {
|
||||
System.out.println(listIterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the Iterable.forEach() method
|
||||
*/
|
||||
public void iterateWithForEach() {
|
||||
countries.forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the Stream.forEach() method
|
||||
*/
|
||||
public void iterateWithStreamForEach() {
|
||||
countries.stream().forEach((c) -> System.out.println(c));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.java.list;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WaysToIterateUnitTest {
|
||||
|
||||
List<String> globalCountries = new ArrayList<String>();
|
||||
List<String> europeanCountries = Arrays.asList("Germany", "Panama", "Australia");
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingForLoop_thenReturnThreeAsSizeOfList() {
|
||||
for (int i = 0; i < europeanCountries.size(); i++) {
|
||||
globalCountries.add(europeanCountries.get(i));
|
||||
}
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingEnhancedForLoop_thenReturnThreeAsSizeOfList() {
|
||||
for (String country : europeanCountries) {
|
||||
globalCountries.add(country);
|
||||
}
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingIterator_thenReturnThreeAsSizeOfList() {
|
||||
Iterator<String> countriesIterator = europeanCountries.iterator();
|
||||
while (countriesIterator.hasNext()) {
|
||||
globalCountries.add(countriesIterator.next());
|
||||
}
|
||||
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingListIterator_thenReturnThreeAsSizeOfList() {
|
||||
ListIterator<String> countriesIterator = europeanCountries.listIterator();
|
||||
while (countriesIterator.hasNext()) {
|
||||
globalCountries.add(countriesIterator.next());
|
||||
}
|
||||
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingForEach_thenReturnThreeAsSizeOfList() {
|
||||
europeanCountries.forEach(country -> globalCountries.add(country));
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingUsingStreamForEach_thenReturnThreeAsSizeOfList() {
|
||||
europeanCountries.stream().forEach((country) -> globalCountries.add(country));
|
||||
assertEquals(globalCountries.size(), 3);
|
||||
globalCountries.clear();
|
||||
}
|
||||
}
|
|
@ -57,4 +57,5 @@
|
|||
- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws)
|
||||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
|
||||
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
|
||||
- [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name)
|
||||
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
package com.baeldung.compoundoperators;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CompoundOperatorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAssignmentOperatorIsUsed_thenValueIsAssigned() {
|
||||
int x = 5;
|
||||
|
||||
assertEquals(5, x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompoundAssignmentUsed_thenSameAsSimpleAssignment() {
|
||||
int a = 3, b = 3, c = -2;
|
||||
a = a * c; // Simple assignment operator
|
||||
b *= c; // Compound assignment operator
|
||||
|
||||
assertEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssignmentOperatorIsUsed_thenValueIsReturned() {
|
||||
long x = 1;
|
||||
long y = (x+=2);
|
||||
|
||||
assertEquals(3, y);
|
||||
assertEquals(y, x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompoundOperatorsAreUsed_thenOperationsArePerformedAndAssigned() {
|
||||
//Simple assignment
|
||||
int x = 5; //x is 5
|
||||
|
||||
//Incrementation
|
||||
x += 5; //x is 10
|
||||
assertEquals(10, x);
|
||||
|
||||
//Decrementation
|
||||
x -= 2; //x is 8
|
||||
assertEquals(8, x);
|
||||
|
||||
//Multiplication
|
||||
x *= 2; //x is 16
|
||||
assertEquals(16, x);
|
||||
|
||||
//Division
|
||||
x /= 4; //x is 4
|
||||
assertEquals(4, x);
|
||||
|
||||
//Modulus
|
||||
x %= 3; //x is 1
|
||||
assertEquals(1, x);
|
||||
|
||||
|
||||
//Binary AND
|
||||
x &= 4; //x is 0
|
||||
assertEquals(0, x);
|
||||
|
||||
//Binary exclusive OR
|
||||
x ^= 4; //x is 4
|
||||
assertEquals(4, x);
|
||||
|
||||
//Binary inclusive OR
|
||||
x |= 8; //x is 12
|
||||
assertEquals(12, x);
|
||||
|
||||
|
||||
//Binary Left Shift
|
||||
x <<= 2; //x is 48
|
||||
assertEquals(48, x);
|
||||
|
||||
//Binary Right Shift
|
||||
x >>= 2; //x is 12
|
||||
assertEquals(12, x);
|
||||
|
||||
//Shift right zero fill
|
||||
x >>>= 1; //x is 6
|
||||
assertEquals(6, x);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenArrayIsNull_thenThrowNullException() {
|
||||
int[] numbers = null;
|
||||
|
||||
//Trying Incrementation
|
||||
numbers[2] += 5;
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void whenArrayIndexNotCorrect_thenThrowArrayIndexException() {
|
||||
int[] numbers = {0, 1};
|
||||
|
||||
//Trying Incrementation
|
||||
numbers[2] += 5;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArrayIndexIsCorrect_thenPerformOperation() {
|
||||
int[] numbers = {0, 1};
|
||||
|
||||
//Incrementation
|
||||
numbers[1] += 5;
|
||||
assertEquals(6, numbers[1]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
*.class
|
||||
|
||||
0.*
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
.resourceCache
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
|
@ -0,0 +1,3 @@
|
|||
=========
|
||||
|
||||
## Core Java Net
|
|
@ -0,0 +1,19 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-net</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-net</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-net</finalName>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -88,3 +88,4 @@
|
|||
- [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order)
|
||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
||||
- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class)
|
||||
- [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Graph {
|
||||
private Map<Vertex, List<Vertex>> adjVertices;
|
||||
|
||||
Graph() {
|
||||
this.adjVertices = new HashMap<Vertex, List<Vertex>>();
|
||||
}
|
||||
|
||||
void addVertex(String label) {
|
||||
adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>());
|
||||
}
|
||||
|
||||
void removeVertex(String label) {
|
||||
Vertex v = new Vertex(label);
|
||||
adjVertices.values().stream().map(e -> e.remove(v)).collect(Collectors.toList());
|
||||
adjVertices.remove(new Vertex(label));
|
||||
}
|
||||
|
||||
void addEdge(String label1, String label2) {
|
||||
Vertex v1 = new Vertex(label1);
|
||||
Vertex v2 = new Vertex(label2);
|
||||
adjVertices.get(v1).add(v2);
|
||||
adjVertices.get(v2).add(v1);
|
||||
}
|
||||
|
||||
void removeEdge(String label1, String label2) {
|
||||
Vertex v1 = new Vertex(label1);
|
||||
Vertex v2 = new Vertex(label2);
|
||||
List<Vertex> eV1 = adjVertices.get(v1);
|
||||
List<Vertex> eV2 = adjVertices.get(v2);
|
||||
if (eV1 != null)
|
||||
eV1.remove(v2);
|
||||
if (eV2 != null)
|
||||
eV2.remove(v1);
|
||||
}
|
||||
|
||||
List<Vertex> getAdjVertices(String label) {
|
||||
return adjVertices.get(new Vertex(label));
|
||||
}
|
||||
|
||||
String printGraph() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(Vertex v : adjVertices.keySet()) {
|
||||
sb.append(v);
|
||||
sb.append(adjVertices.get(v));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
class Vertex {
|
||||
String label;
|
||||
Vertex(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
Vertex vertex = (Vertex) obj;
|
||||
return vertex.label == label;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return label.hashCode();
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import com.baeldung.graph.Graph.Vertex;
|
||||
|
||||
public class GraphTraversal {
|
||||
static Set<String> depthFirstTraversal(Graph graph, String root) {
|
||||
Set<String> visited = new LinkedHashSet<String>();
|
||||
Stack<String> stack = new Stack<String>();
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
String vertex = stack.pop();
|
||||
if (!visited.contains(vertex)) {
|
||||
visited.add(vertex);
|
||||
for (Vertex v : graph.getAdjVertices(vertex)) {
|
||||
stack.push(v.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
return visited;
|
||||
}
|
||||
|
||||
static Set<String> breadthFirstTraversal(Graph graph, String root) {
|
||||
Set<String> visited = new LinkedHashSet<String>();
|
||||
Queue<String> queue = new LinkedList<String>();
|
||||
queue.add(root);
|
||||
visited.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
String vertex = queue.poll();
|
||||
for (Vertex v : graph.getAdjVertices(vertex)) {
|
||||
if (!visited.contains(v.label)) {
|
||||
visited.add(v.label);
|
||||
queue.add(v.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
return visited;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.printf;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PrintfExamples {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
printfNewLine();
|
||||
printfChar();
|
||||
printfString();
|
||||
printfNumber();
|
||||
printfDateTime();
|
||||
printfBoolean();
|
||||
}
|
||||
|
||||
private static void printfNewLine() {
|
||||
System.out.printf("baeldung%nline%nterminator");
|
||||
}
|
||||
|
||||
private static void printfString() {
|
||||
System.out.printf("'%s' %n", "baeldung");
|
||||
System.out.printf("'%S' %n", "baeldung");
|
||||
System.out.printf("'%15s' %n", "baeldung");
|
||||
System.out.printf("'%-10s' %n", "baeldung");
|
||||
}
|
||||
|
||||
private static void printfChar() {
|
||||
System.out.printf("%c%n", 's');
|
||||
System.out.printf("%C%n", 's');
|
||||
}
|
||||
|
||||
private static void printfNumber() {
|
||||
System.out.printf("simple integer: %d%n", 10000L);
|
||||
|
||||
System.out.printf(Locale.US, "%,d %n", 10000);
|
||||
System.out.printf(Locale.ITALY, "%,d %n", 10000);
|
||||
|
||||
System.out.printf("%f%n", 5.1473);
|
||||
System.out.printf("'%5.2f'%n", 5.1473);
|
||||
System.out.printf("'%5.2e'%n", 5.1473);
|
||||
}
|
||||
|
||||
private static void printfBoolean() {
|
||||
System.out.printf("%b%n", null);
|
||||
System.out.printf("%B%n", false);
|
||||
System.out.printf("%B%n", 5.3);
|
||||
System.out.printf("%b%n", "random text");
|
||||
}
|
||||
|
||||
private static void printfDateTime() {
|
||||
Date date = new Date();
|
||||
System.out.printf("%tT%n", date);
|
||||
System.out.printf("hours %tH: minutes %tM: seconds %tS%n", date, date, date);
|
||||
System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
|
||||
|
||||
System.out.printf("%1$tA %1$tB %1$tY %n", date);
|
||||
System.out.printf("%1$td.%1$tm.%1$ty %n", date);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GraphTraversalUnitTest {
|
||||
@Test
|
||||
public void givenAGraph_whenTraversingDepthFirst_thenExpectedResult() {
|
||||
Graph graph = createGraph();
|
||||
Assert.assertEquals("[Bob, Rob, Maria, Alice, Mark]",
|
||||
GraphTraversal.depthFirstTraversal(graph, "Bob").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAGraph_whenTraversingBreadthFirst_thenExpectedResult() {
|
||||
Graph graph = createGraph();
|
||||
Assert.assertEquals("[Bob, Alice, Rob, Mark, Maria]",
|
||||
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
|
||||
}
|
||||
|
||||
Graph createGraph() {
|
||||
Graph graph = new Graph();
|
||||
graph.addVertex("Bob");
|
||||
graph.addVertex("Alice");
|
||||
graph.addVertex("Mark");
|
||||
graph.addVertex("Rob");
|
||||
graph.addVertex("Maria");
|
||||
graph.addEdge("Bob", "Alice");
|
||||
graph.addEdge("Bob", "Rob");
|
||||
graph.addEdge("Alice", "Mark");
|
||||
graph.addEdge("Rob", "Mark");
|
||||
graph.addEdge("Alice", "Maria");
|
||||
graph.addEdge("Rob", "Maria");
|
||||
return graph;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OptimizedMatcherUnitTest {
|
||||
public class OptimizedMatcherManualTest {
|
||||
|
||||
private String action;
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-kotlin</artifactId>
|
||||
<name>core-kotlin</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigDecimal
|
||||
|
||||
enum class Currency {
|
||||
DOLLARS, EURO
|
||||
}
|
||||
|
||||
class Money(val amount: BigDecimal, val currency: Currency) : Comparable<Money> {
|
||||
|
||||
override fun compareTo(other: Money): Int =
|
||||
convert(Currency.DOLLARS).compareTo(other.convert(Currency.DOLLARS))
|
||||
|
||||
fun convert(currency: Currency): BigDecimal = TODO()
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Money) return false
|
||||
|
||||
if (amount != other.amount) return false
|
||||
if (currency != other.currency) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = amount.hashCode()
|
||||
result = 31 * result + currency.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
interface Page<T> {
|
||||
fun pageNumber(): Int
|
||||
fun pageSize(): Int
|
||||
fun elements(): MutableList<T>
|
||||
}
|
||||
|
||||
operator fun <T> Page<T>.get(index: Int): T = elements()[index]
|
||||
operator fun <T> Page<T>.get(start: Int, endExclusive: Int): List<T> = elements().subList(start, endExclusive)
|
||||
operator fun <T> Page<T>.set(index: Int, value: T) {
|
||||
elements()[index] = value
|
||||
}
|
||||
|
||||
operator fun <T> Page<T>.contains(element: T): Boolean = element in elements()
|
||||
operator fun <T> Page<T>.iterator() = elements().iterator()
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
data class Point(val x: Int, val y: Int)
|
||||
|
||||
operator fun Point.unaryMinus() = Point(-x, -y)
|
||||
operator fun Point.not() = Point(y, x)
|
||||
operator fun Point.inc() = Point(x + 1, y + 1)
|
||||
operator fun Point.dec() = Point(x - 1, y - 1)
|
||||
|
||||
operator fun Point.plus(other: Point): Point = Point(x + other.x, y + other.y)
|
||||
operator fun Point.minus(other: Point): Point = Point(x - other.x, y - other.y)
|
||||
operator fun Point.times(other: Point): Point = Point(x * other.x, y * other.y)
|
||||
operator fun Point.div(other: Point): Point = Point(x / other.x, y / other.y)
|
||||
operator fun Point.rem(other: Point): Point = Point(x % other.x, y % other.y)
|
||||
operator fun Point.times(factor: Int): Point = Point(x * factor, y * factor)
|
||||
operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this)
|
||||
|
||||
class Shape {
|
||||
val points = mutableListOf<Point>()
|
||||
|
||||
operator fun Point.unaryPlus() {
|
||||
points.add(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun shape(init: Shape.() -> Unit): Shape {
|
||||
val shape = Shape()
|
||||
shape.init()
|
||||
|
||||
return shape
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
operator fun <T> MutableCollection<T>.plusAssign(element: T) {
|
||||
add(element)
|
||||
}
|
||||
operator fun BigInteger.plus(other: Int): BigInteger = add(BigInteger("$other"))
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class PageTest {
|
||||
|
||||
private val page = PageImpl(1, 10, "Java", "Kotlin", "Scala")
|
||||
|
||||
@Test
|
||||
fun `Get convention should work as expected`() {
|
||||
assertEquals(page[1], "Kotlin")
|
||||
assertEquals(page[1, 3], listOf("Kotlin", "Scala"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `In convention should work on a page as expected`() {
|
||||
assertTrue("Kotlin" in page)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class PageImpl<T>(val page: Int, val size: Int, vararg val elements: T) : Page<T> {
|
||||
override fun pageNumber(): Int = page
|
||||
override fun pageSize(): Int = size
|
||||
override fun elements(): MutableList<T> = mutableListOf(*elements)
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class PointTest {
|
||||
|
||||
private val p1 = Point(1, 2)
|
||||
private val p2 = Point(2, 3)
|
||||
|
||||
@Test
|
||||
fun `We should be able to add two points together using +`() {
|
||||
assertEquals(p1 + p2, Point(3, 5))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `We shoud be able to subtract one point from another using -`() {
|
||||
assertEquals(p1 - p2, Point(-1, -1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `We should be able to multiply two points together with *`() {
|
||||
assertEquals(p1 * p2, Point(2, 6))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `We should be able to divide one point by another`() {
|
||||
assertEquals(p1 / p2, Point(0, 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `We should be able to scale a point by an integral factor`() {
|
||||
assertEquals(p1 * 2, Point(2, 4))
|
||||
assertEquals(2 * p1, Point(2, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `We should be able to add points to an empty shape`() {
|
||||
val line = shape {
|
||||
+Point(0, 0)
|
||||
+Point(1, 3)
|
||||
}
|
||||
|
||||
assertTrue(Point(0, 0) in line.points)
|
||||
assertTrue(Point(1, 3) in line.points)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigInteger
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class UtilsTest {
|
||||
|
||||
@Test
|
||||
fun `We should be able to add an int value to an existing BigInteger using +`() {
|
||||
assertEquals(BigInteger.ZERO + 1, BigInteger.ONE)
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-scala</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>core-scala</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>data-structures</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>data-structures</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>drools</artifactId>
|
||||
<name>drools</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<name>dubbo</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>flyway-cdi-extension</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>flyway-cdi-extension</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>guava-18</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>guava-18</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>guava-19</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>guava-19</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>guava-21</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>guava-21</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>core-java-9</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>core-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>core-java</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<groupId>com.stackify</groupId>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-kotlin</name>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>deep-jsf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>deep-jsf</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>junit5-example</groupId>
|
||||
<artifactId>junit5-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>junit5-example</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>log4j2-example</groupId>
|
||||
<artifactId>log4j2-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>log4j2-example</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>logback-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>logback-example</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>memory-leaks</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>memory-leaks</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>remote-debugging</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>remote-debugging</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -2,13 +2,12 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.stackify.slf4j.guide</groupId>
|
||||
<artifactId>slf4j-parent-module</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>slf4j-parent-module</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>slf4j-log4j</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>slf4j-log4j</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>slf4j-log4j2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>slf4j-log4j2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>slf4j-logback</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>slf4j-logback</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>spring-boot-app</groupId>
|
||||
<artifactId>spring-boot-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-app</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
<groupId>com.stackify.guest</groupId>
|
||||
<artifactId>spring-mvc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>spring-mvc</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring MVC sample project</description>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>thread-pools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>thread-pools</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>tomcat-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>tomcat-app</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>rest-client</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>rest-client</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>rest-server</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>rest-server</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<groupId>com.stackify</groupId>
|
||||
<artifactId>spring-rest-service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-rest-service</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>helidon-mp</artifactId>
|
||||
<name>helidon-mp</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>helidon-se</artifactId>
|
||||
<name>helidon-se</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
<artifactId>helidon</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>helidon</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>image-processing</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>image-processing</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>immutables</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>immutables</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -9,3 +9,4 @@
|
|||
- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set)
|
||||
- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string)
|
||||
- [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map)
|
||||
- [Array to String Conversions](https://www.baeldung.com/java-array-to-string)
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
package com.baeldung.java.map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
|
||||
public class ImmutableMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCollectionsUnModifiableMapMethod_thenOriginalCollectionChangesReflectInUnmodifiableMap() {
|
||||
|
||||
Map<String, String> mutableMap = new HashMap<>();
|
||||
mutableMap.put("USA", "North America");
|
||||
|
||||
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(mutableMap);
|
||||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableMap.put("Canada", "North America"));
|
||||
|
||||
mutableMap.remove("USA");
|
||||
assertFalse(unmodifiableMap.containsKey("USA"));
|
||||
|
||||
mutableMap.put("Mexico", "North America");
|
||||
assertTrue(unmodifiableMap.containsKey("Mexico"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void whenGuavaImmutableMapFromCopyOfMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() {
|
||||
|
||||
Map<String, String> mutableMap = new HashMap<>();
|
||||
mutableMap.put("USA", "North America");
|
||||
|
||||
ImmutableMap<String, String> immutableMap = ImmutableMap.copyOf(mutableMap);
|
||||
assertTrue(immutableMap.containsKey("USA"));
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America"));
|
||||
|
||||
mutableMap.remove("USA");
|
||||
assertTrue(immutableMap.containsKey("USA"));
|
||||
|
||||
mutableMap.put("Mexico", "North America");
|
||||
assertFalse(immutableMap.containsKey("Mexico"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void whenGuavaImmutableMapFromBuilderMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() {
|
||||
|
||||
Map<String, String> mutableMap = new HashMap<>();
|
||||
mutableMap.put("USA", "North America");
|
||||
|
||||
ImmutableMap<String, String> immutableMap = ImmutableMap.<String, String>builder()
|
||||
.putAll(mutableMap)
|
||||
.put("Costa Rica", "North America")
|
||||
.build();
|
||||
assertTrue(immutableMap.containsKey("USA"));
|
||||
assertTrue(immutableMap.containsKey("Costa Rica"));
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America"));
|
||||
|
||||
mutableMap.remove("USA");
|
||||
assertTrue(immutableMap.containsKey("USA"));
|
||||
|
||||
mutableMap.put("Mexico", "North America");
|
||||
assertFalse(immutableMap.containsKey("Mexico"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void whenGuavaImmutableMapFromOfMethod_thenOriginalCollectionChangesDoNotReflectInImmutableMap() {
|
||||
|
||||
ImmutableMap<String, String> immutableMap = ImmutableMap.of("USA", "North America", "Costa Rica", "North America");
|
||||
assertTrue(immutableMap.containsKey("USA"));
|
||||
assertTrue(immutableMap.containsKey("Costa Rica"));
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("Canada", "North America"));
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue