Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
9b89d51074
@ -4,7 +4,7 @@ before_install:
|
||||
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
|
||||
|
||||
install: skip
|
||||
script: travis_wait 60 mvn -q test
|
||||
script: travis_wait 60 mvn -q install
|
||||
|
||||
sudo: required
|
||||
|
||||
|
11
README.md
11
README.md
@ -17,6 +17,10 @@ This project is **a collection of small and focused tutorials** each covering a
|
||||
Most of the tutorial projects are focused on the `Spring Framework` (and `Spring Security`).
|
||||
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
||||
|
||||
Building the project
|
||||
====================
|
||||
To do the full build, do: `mvn install -Dgib.enabled=false`
|
||||
|
||||
|
||||
Working with the code in Eclipse
|
||||
================================
|
||||
@ -29,3 +33,10 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons
|
||||
CI - Jenkins
|
||||
================================
|
||||
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials-unit/)**
|
||||
|
||||
### Relevant Articles:
|
||||
================================
|
||||
|
||||
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
|
||||
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
|
||||
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
|
||||
|
@ -21,3 +21,4 @@
|
||||
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
||||
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum)
|
||||
|
3
apache-opennlp/README.md
Normal file
3
apache-opennlp/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Relevant Articles
|
||||
|
||||
- [Intro to Apache OpenNLP](http://www.baeldung.com/apache-open-nlp)
|
@ -2,3 +2,4 @@
|
||||
|
||||
- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor)
|
||||
- [Generating a Book with Asciidoctor](http://www.baeldung.com/asciidoctor-book)
|
||||
- [Introduction to Asciidoctor in Java](http://www.baeldung.com/asciidoctor)
|
||||
|
@ -18,10 +18,29 @@
|
||||
<artifactId>auto-value</artifactId>
|
||||
<version>${auto-value.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.auto.factory</groupId>
|
||||
<artifactId>auto-factory</artifactId>
|
||||
<version>${auto-factory.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<auto-value.version>1.3</auto-value.version>
|
||||
<auto-factory.version>1.0-beta5</auto-factory.version>
|
||||
<guice.version>4.2.0</guice.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
21
autovalue/src/main/java/com/baeldung/autofactory/App.java
Normal file
21
autovalue/src/main/java/com/baeldung/autofactory/App.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.baeldung.autofactory;
|
||||
|
||||
import com.baeldung.autofactory.model.Camera;
|
||||
import com.baeldung.autofactory.model.Phone;
|
||||
import com.baeldung.autofactory.model.PhoneFactory;
|
||||
import com.baeldung.autofactory.modules.SonyCameraModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
PhoneFactory phoneFactory = new PhoneFactory(() -> new Camera("Unknown", "XXX"));
|
||||
Phone simplePhone = phoneFactory.create("other parts");
|
||||
|
||||
Injector injector = Guice.createInjector(new SonyCameraModule());
|
||||
PhoneFactory injectedFactory = injector.getInstance(PhoneFactory.class);
|
||||
Phone xperia = injectedFactory.create("Xperia");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baeldung.autofactory;
|
||||
|
||||
import com.baeldung.autofactory.custom.SmartPhone;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public interface CustomStorage {
|
||||
|
||||
SmartPhone customROMInGB(int romSize);
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.autofactory.custom;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public abstract class AbstractFactory {
|
||||
|
||||
abstract CustomPhone newInstance(String brand);
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.autofactory.custom;
|
||||
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@AutoFactory(extending = AbstractFactory.class)
|
||||
public class CustomPhone {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public CustomPhone(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.autofactory.custom;
|
||||
|
||||
import com.baeldung.autofactory.CustomStorage;
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@AutoFactory(className = "SamsungFactory", allowSubclasses = true, implementing = CustomStorage.class)
|
||||
public class SmartPhone {
|
||||
|
||||
private int romSize;
|
||||
|
||||
public SmartPhone(int romSize) {
|
||||
this.romSize = romSize;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.autofactory.model;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class Camera {
|
||||
|
||||
private final String manufacturer;
|
||||
private final String serial;
|
||||
|
||||
public Camera(String manufacturer, String serial) {
|
||||
this.manufacturer = manufacturer;
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
public String getManufacturer() {
|
||||
return manufacturer;
|
||||
}
|
||||
|
||||
public String getSerial() {
|
||||
return serial;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.autofactory.model;
|
||||
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
import com.google.auto.factory.Provided;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class ClassicPhone {
|
||||
|
||||
private final String dialpad;
|
||||
private final String ringer;
|
||||
private String otherParts;
|
||||
|
||||
@AutoFactory
|
||||
public ClassicPhone(@Provided String dialpad, @Provided String ringer) {
|
||||
this.dialpad = dialpad;
|
||||
this.ringer = ringer;
|
||||
}
|
||||
|
||||
@AutoFactory
|
||||
public ClassicPhone(String otherParts) {
|
||||
this("defaultDialPad", "defaultRinger");
|
||||
this.otherParts = otherParts;
|
||||
}
|
||||
|
||||
public String getDialpad() {
|
||||
return dialpad;
|
||||
}
|
||||
|
||||
public String getRinger() {
|
||||
return ringer;
|
||||
}
|
||||
|
||||
public String getOtherParts() {
|
||||
return otherParts;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.autofactory.model;
|
||||
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
import com.google.auto.factory.Provided;
|
||||
|
||||
import javax.inject.Named;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@AutoFactory
|
||||
public class Phone {
|
||||
|
||||
private Camera camera;
|
||||
private String otherParts;
|
||||
|
||||
public Phone(@Provided @Named("Sony") Camera camera, String otherParts) {
|
||||
this.camera = camera;
|
||||
this.otherParts = otherParts;
|
||||
}
|
||||
|
||||
/* required when used as a base class for AutoFactory */
|
||||
public Phone() {
|
||||
}
|
||||
|
||||
public Camera getCamera() {
|
||||
return camera;
|
||||
}
|
||||
|
||||
public String getOtherParts() {
|
||||
return otherParts;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.autofactory.modules;
|
||||
|
||||
import com.baeldung.autofactory.model.Camera;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
import javax.inject.Named;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class SonyCameraModule extends AbstractModule {
|
||||
|
||||
private static int SONY_CAMERA_SERIAL = 1;
|
||||
|
||||
@Named("Sony")
|
||||
@Provides
|
||||
Camera cameraProvider() {
|
||||
return new Camera("Sony", String.format("%03d", SONY_CAMERA_SERIAL++));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.autofactory.provided;
|
||||
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
import com.google.auto.factory.Provided;
|
||||
import javafx.scene.Camera;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@AutoFactory
|
||||
public class IntermediateAssembler {
|
||||
|
||||
private final Provider<Camera> camera;
|
||||
private final String otherParts;
|
||||
|
||||
public IntermediateAssembler(@Provided Provider<Camera> camera, String otherParts) {
|
||||
this.camera = camera;
|
||||
this.otherParts = otherParts;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.autofactory.provider;
|
||||
|
||||
import com.baeldung.autofactory.model.Camera;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class SonyCameraProvider implements Provider<Camera> {
|
||||
|
||||
private static int sonyCameraSerial = 1;
|
||||
|
||||
@Override
|
||||
public Camera get() {
|
||||
return new Camera("Sony", String.format("%03d", sonyCameraSerial++));
|
||||
}
|
||||
|
||||
}
|
@ -5,4 +5,8 @@
|
||||
- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda)
|
||||
- [Managing EC2 Instances in Java](http://www.baeldung.com/ec2-java)
|
||||
- [http://www.baeldung.com/aws-s3-multipart-upload](https://github.com/eugenp/tutorials/tree/master/aws)
|
||||
- [Multipart Uploads in Amazon S3 with Java](http://www.baeldung.com/aws-s3-multipart-upload)
|
||||
- [Integration Testing with a Local DynamoDB Instance](http://www.baeldung.com/dynamodb-local-integration-tests)
|
||||
- [Using the JetS3t Java Client With Amazon S3](http://www.baeldung.com/jets3t-amazon-s3)
|
||||
- [Managing Amazon SQS Queues in Java](http://www.baeldung.com/aws-queues-java)
|
||||
|
||||
|
@ -1,2 +1,3 @@
|
||||
## Relevant articles:
|
||||
- [CAS SSO With Spring Security](http://www.baeldung.com/spring-security-cas-sso)
|
||||
- [Code Analysis with SonarQube](http://www.baeldung.com/sonar-qube)
|
||||
|
10
cdi/pom.xml
10
cdi/pom.xml
@ -8,16 +8,12 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-spring</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
|
@ -28,12 +28,6 @@
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>${groovy-sql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
|
45
core-java-10/pom.xml
Normal file
45
core-java-10/pom.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<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-10</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-10</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>10</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>10</maven.compiler.target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
13
core-java-10/src/main/java/com/baeldung/App.java
Normal file
13
core-java-10/src/main/java/com/baeldung/App.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.baeldung;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
38
core-java-10/src/test/java/com/baeldung/AppTest.java
Normal file
38
core-java-10/src/test/java/com/baeldung/AppTest.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.baeldung;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.baeldung.java10;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java10FeaturesUnitTest {
|
||||
|
||||
private List<Integer> someIntList;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
someIntList = new ArrayList<>();
|
||||
|
||||
someIntList.add(1);
|
||||
someIntList.add(2);
|
||||
someIntList.add(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVarInitWithString_thenGetStringTypeVar() {
|
||||
var message = "Hello, Java 10";
|
||||
assertTrue(message instanceof String);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVarInitWithAnonymous_thenGetAnonymousType() {
|
||||
var obj = new Object() {};
|
||||
assertFalse(obj.getClass().equals(Object.class));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenModifyCopyOfList_thenThrowsException() {
|
||||
List<Integer> copyList = List.copyOf(someIntList);
|
||||
copyList.add(4);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenModifyToUnmodifiableList_thenThrowsException() {
|
||||
List<Integer> evenList = someIntList.stream()
|
||||
.filter(i -> i % 2 == 0)
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
evenList.add(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListContainsInteger_OrElseThrowReturnsInteger() {
|
||||
Integer firstEven = someIntList.stream()
|
||||
.filter(i -> i % 2 == 0)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
is(firstEven).equals(Integer.valueOf(2));
|
||||
}
|
||||
}
|
45
core-java-11/pom.xml
Normal file
45
core-java-11/pom.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<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-11</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
13
core-java-11/src/main/java/com/baeldung/App.java
Normal file
13
core-java-11/src/main/java/com/baeldung/App.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.baeldung;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
38
core-java-11/src/test/java/com/baeldung/AppTest.java
Normal file
38
core-java-11/src/test/java/com/baeldung/AppTest.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.baeldung;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
@ -44,3 +44,10 @@
|
||||
- [Java 8 StringJoiner](http://www.baeldung.com/java-string-joiner)
|
||||
- [Introduction to Spliterator in Java](http://www.baeldung.com/java-spliterator)
|
||||
- [Java 8 Math New Methods](http://www.baeldung.com/java-8-math)
|
||||
- [Overview of Java Built-in Annotations](http://www.baeldung.com/java-default-annotations)
|
||||
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
|
||||
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
|
||||
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
|
||||
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
|
||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||
|
||||
|
@ -9,17 +9,12 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Customer(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id * 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Customer) {
|
||||
Customer otherCustomer = (Customer) obj;
|
||||
if (id == otherCustomer.id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
public class FindACustomerInGivenList {
|
||||
|
||||
public Customer findUsingGivenIndex(int indexOfCustomer, List<Customer> customers) {
|
||||
if (indexOfCustomer >= 0 && indexOfCustomer < customers.size())
|
||||
return customers.get(indexOfCustomer);
|
||||
return null;
|
||||
}
|
||||
|
||||
public int findUsingIndexOf(Customer customer, List<Customer> customers) {
|
||||
return customers.indexOf(customer);
|
||||
}
|
||||
|
||||
public boolean findUsingContains(Customer customer, List<Customer> customers) {
|
||||
return customers.contains(customer);
|
||||
}
|
||||
|
||||
public Customer findUsingIterator(String name, List<Customer> customers) {
|
||||
Iterator<Customer> iterator = customers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Customer customer = iterator.next();
|
||||
if (customer.getName().equals(name)) {
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Customer findUsingEnhancedForLoop(String name, List<Customer> customers) {
|
||||
for (Customer customer : customers) {
|
||||
if (customer.getName().equals(name)) {
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Customer findUsingStream(String name, List<Customer> customers) {
|
||||
return customers.stream()
|
||||
.filter(customer -> customer.getName().equals(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public Customer findUsingParallelStream(String name, List<Customer> customers) {
|
||||
return customers.parallelStream()
|
||||
.filter(customer -> customer.getName().equals(name))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public Customer findUsingGuava(String name, List<Customer> customers) {
|
||||
return Iterables.tryFind(customers, new Predicate<Customer>() {
|
||||
public boolean apply(Customer customer) {
|
||||
return customer.getName().equals(name);
|
||||
}
|
||||
}).orNull();
|
||||
}
|
||||
|
||||
public Customer findUsingApacheCommon(String name, List<Customer> customers) {
|
||||
return IterableUtils.find(customers, new org.apache.commons.collections4.Predicate<Customer>() {
|
||||
public boolean evaluate(Customer customer) {
|
||||
return customer.getName().equals(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
public class FindElementInAList<T> {
|
||||
|
||||
public T findUsingIndexOf(T element, List<T> list) {
|
||||
int index = list.indexOf(element);
|
||||
if (index >= 0) {
|
||||
return element;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean findUsingListIterator(T element, List<T> list) {
|
||||
ListIterator<T> listIterator = list.listIterator();
|
||||
while (listIterator.hasNext()) {
|
||||
T elementFromList = listIterator.next();
|
||||
if (elementFromList.equals(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean findUsingEnhancedForLoop(T element, List<T> list) {
|
||||
for (T elementFromList : list) {
|
||||
if (element.equals(elementFromList)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public T findUsingStream(T element, List<T> list) {
|
||||
return list.stream()
|
||||
.filter(integer -> integer.equals(element))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public T findUsingParallelStream(T element, List<T> list) {
|
||||
return list.parallelStream()
|
||||
.filter(integer -> integer.equals(element))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public T findUsingGuava(T element, List<T> list) {
|
||||
T foundElement = Iterables.tryFind(list, new Predicate<T>() {
|
||||
public boolean apply(T input) {
|
||||
return element.equals(input);
|
||||
}
|
||||
}).orNull();
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
public T findUsingApacheCommon(T element, List<T> list) {
|
||||
T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate<T>() {
|
||||
public boolean evaluate(T input) {
|
||||
return element.equals(input);
|
||||
}
|
||||
});
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindACustomerInGivenListTest {
|
||||
|
||||
private static List<Customer> customers = new ArrayList<>();
|
||||
|
||||
static {
|
||||
customers.add(new Customer(1, "Jack"));
|
||||
customers.add(new Customer(2, "James"));
|
||||
customers.add(new Customer(3, "Sam"));
|
||||
}
|
||||
|
||||
private static FindACustomerInGivenList findACustomerInGivenList = new FindACustomerInGivenList();
|
||||
|
||||
@Test
|
||||
public void givenAnIndex_whenFoundUsingGivenIndex_thenReturnCustomer() {
|
||||
Customer customer = findACustomerInGivenList.findUsingGivenIndex(0, customers);
|
||||
|
||||
assertEquals(1, customer.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIndex_whenNotFoundUsingGivenIndex_thenReturnNull() {
|
||||
Customer customer = findACustomerInGivenList.findUsingGivenIndex(5, customers);
|
||||
|
||||
assertNull(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACustomer_whenFoundUsingContains_thenReturnTrue() {
|
||||
Customer james = new Customer(2, "James");
|
||||
boolean isJamesPresent = findACustomerInGivenList.findUsingContains(james, customers);
|
||||
|
||||
assertEquals(true, isJamesPresent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACustomer_whenNotFoundUsingContains_thenReturnFalse() {
|
||||
Customer john = new Customer(5, "John");
|
||||
boolean isJohnPresent = findACustomerInGivenList.findUsingContains(john, customers);
|
||||
|
||||
assertEquals(false, isJohnPresent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACustomer_whenFoundUsingIndexOf_thenReturnItsIndex() {
|
||||
Customer james = new Customer(2, "James");
|
||||
int indexOfJames = findACustomerInGivenList.findUsingIndexOf(james, customers);
|
||||
|
||||
assertEquals(1, indexOfJames);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACustomer_whenNotFoundUsingIndexOf_thenReturnMinus1() {
|
||||
Customer john = new Customer(5, "John");
|
||||
int indexOfJohn = findACustomerInGivenList.findUsingIndexOf(john, customers);
|
||||
|
||||
assertEquals(-1, indexOfJohn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingIterator_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingIterator("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingIterator_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingIterator("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingEnhancedFor_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingEnhancedForLoop("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingEnhancedFor_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingEnhancedForLoop("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingStream_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingStream("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingStream_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingStream("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingParallelStream_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingParallelStream("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingParallelStream_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingParallelStream("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingApacheCommon_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingApacheCommon("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingApacheCommon_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingApacheCommon("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameFoundUsingGuava_thenReturnCustomer() {
|
||||
Customer james = findACustomerInGivenList.findUsingGuava("James", customers);
|
||||
|
||||
assertEquals("James", james.getName());
|
||||
assertEquals(2, james.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() {
|
||||
Customer john = findACustomerInGivenList.findUsingGuava("John", customers);
|
||||
|
||||
assertNull(john);
|
||||
}
|
||||
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindAnElementTest {
|
||||
|
||||
private static List<Integer> scores = new ArrayList<>();
|
||||
static {
|
||||
scores.add(0);
|
||||
scores.add(1);
|
||||
scores.add(2);
|
||||
}
|
||||
|
||||
private static FindElementInAList<Integer> findElementInAList = new FindElementInAList<>();
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingIndexOf_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingIndexOf(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingListIterator_thenReturnNull() {
|
||||
boolean found = findElementInAList.findUsingListIterator(5, scores);
|
||||
assertTrue(!found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundListIterator_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
boolean found = findElementInAList.findUsingListIterator(scoreToFind, scores);
|
||||
assertTrue(found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingIndexOf_thenReturnNull() {
|
||||
Integer score = findElementInAList.findUsingIndexOf(5, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingEnhancedForLoop_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
|
||||
assertTrue(found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingEnhancedForLoop_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
|
||||
assertTrue(!found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingStream_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingStream_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingParallelStream_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingParallelStream_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingGuava_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingGuava_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingApacheCommons_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingApacheCommons_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
}
|
@ -23,3 +23,4 @@
|
||||
- [Exploring the New HTTP Client in Java 9](http://www.baeldung.com/java-9-http-client)
|
||||
- [Method Handles in Java](http://www.baeldung.com/java-method-handles)
|
||||
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
|
||||
- [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity)
|
||||
|
30
core-java-collections/README.md
Normal file
30
core-java-collections/README.md
Normal file
@ -0,0 +1,30 @@
|
||||
=========
|
||||
|
||||
## Core Java Collections Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list)
|
||||
- [Converting between an Array and a List in Java](http://www.baeldung.com/convert-array-to-list-and-list-to-array)
|
||||
- [Converting between an Array and a Set in Java](http://www.baeldung.com/convert-array-to-set-and-set-to-array)
|
||||
- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
||||
- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set)
|
||||
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
|
||||
- [Random List Element](http://www.baeldung.com/java-random-list-element)
|
||||
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||
- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap)
|
||||
- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap)
|
||||
- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap)
|
||||
- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap)
|
||||
- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap)
|
||||
- [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list)
|
||||
- [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list)
|
||||
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map)
|
||||
- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection)
|
||||
- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string)
|
||||
- [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set)
|
||||
- [Java TreeMap vs HashMap](http://www.baeldung.com/java-treemap-vs-hashmap)
|
||||
- [How to TDD a List Implementation in Java](http://www.baeldung.com/java-test-driven-list)
|
60
core-java-collections/pom.xml
Normal file
60
core-java-collections/pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
<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-collections</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1,124 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections4.MultiMap;
|
||||
import org.apache.commons.collections4.MultiMapUtils;
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
import org.apache.commons.collections4.map.MultiValueMap;
|
||||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
|
||||
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.TreeMultimap;
|
||||
|
||||
public class MapMultipleValuesTest {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MapMultipleValuesTest.class);
|
||||
|
||||
@Test
|
||||
public void givenHashMap_whenPuttingTwice_thenReturningFirstValue() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
assertThat(map.put("key1", "value1")).isEqualTo(null);
|
||||
assertThat(map.put("key1", "value2")).isEqualTo("value1");
|
||||
assertThat(map.get("key1")).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionAsValue_whenPuttingTwice_thenReturningCollection() {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
map.put("key1", list);
|
||||
map.get("key1").add("value1");
|
||||
map.get("key1").add("value2");
|
||||
assertThat(map.get("key1").get(0)).isEqualTo("value1");
|
||||
assertThat(map.get("key1").get(1)).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionAsValueAndJava8_whenPuttingTwice_thenReturningCollection() {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
map.computeIfAbsent("key1", k -> new ArrayList<>()).add("value1");
|
||||
map.computeIfAbsent("key1", k -> new ArrayList<>()).add("value2");
|
||||
assertThat(map.get("key1").get(0)).isEqualTo("value1");
|
||||
assertThat(map.get("key1").get(1)).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValueMap_whenPuttingTwice_thenReturningValues() {
|
||||
MultiMap<String, String> map = new MultiValueMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value2");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.contains("value1", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayListValuedHashMap_whenPuttingDoubleValues_thenReturningAllValues() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value2");
|
||||
map.put("key1", "value2");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.containsExactly("value1", "value2", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashSetValuedHashMap_whenPuttingTwiceTheSame_thenReturningOneValue() {
|
||||
MultiValuedMap<String, String> map = new HashSetValuedHashMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value1");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.containsExactly("value1");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenUnmodifiableMultiValuedMap_whenInserting_thenThrowingException() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value2");
|
||||
MultiValuedMap<String, String> immutableMap =
|
||||
MultiMapUtils.unmodifiableMultiValuedMap(map);
|
||||
immutableMap.put("key1", "value3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayListMultiMap_whenInserting_thenCorrectOutput() {
|
||||
Multimap<String, String> map = ArrayListMultimap.create();
|
||||
map.put("key1", "value2");
|
||||
map.put("key1", "value1");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.containsExactly("value2", "value1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedHashMultiMap_whenInserting_thenReturningValuesInInsertionOrder() {
|
||||
Multimap<String, String> map = LinkedHashMultimap.create();
|
||||
map.put("key1", "value3");
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value2");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.containsExactly("value3", "value1", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTreeMultimap_whenInserting_thenReturningValuesInNaturalOrder() {
|
||||
Multimap<String, String> map = TreeMultimap.create();
|
||||
map.put("key1", "value3");
|
||||
map.put("key1", "value1");
|
||||
map.put("key1", "value2");
|
||||
assertThat((Collection<String>) map.get("key1"))
|
||||
.containsExactly("value1", "value2", "value3");
|
||||
}
|
||||
|
||||
}
|
@ -9,17 +9,12 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -25,3 +25,5 @@
|
||||
- [Zipping and Unzipping in Java](http://www.baeldung.com/java-compress-and-uncompress)
|
||||
- [Java NIO2 Path API](http://www.baeldung.com/java-nio-2-path)
|
||||
- [A Guide to WatchService in Java NIO2](http://www.baeldung.com/java-nio2-watchservice)
|
||||
- [Guide to Java NIO2 Asynchronous Channel APIs](http://www.baeldung.com/java-nio-2-async-channels)
|
||||
- [A Guide to NIO2 Asynchronous Socket Channel](http://www.baeldung.com/java-nio2-async-socket-channel)
|
||||
|
@ -9,8 +9,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -20,11 +21,6 @@
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -8,8 +8,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -19,11 +20,6 @@
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -3,21 +3,14 @@
|
||||
## Core Java Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list)
|
||||
- [Converting between an Array and a List in Java](http://www.baeldung.com/convert-array-to-list-and-list-to-array)
|
||||
- [Converting between an Array and a Set in Java](http://www.baeldung.com/convert-array-to-set-and-set-to-array)
|
||||
- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
||||
- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set)
|
||||
- [Java – Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double)
|
||||
- [Java – Generate Random String](http://www.baeldung.com/java-random-string)
|
||||
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
||||
- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java)
|
||||
- [MD5 Hashing in Java](http://www.baeldung.com/java-md5)
|
||||
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)
|
||||
- [Convert char to String in Java](http://www.baeldung.com/java-convert-char-to-string)
|
||||
- [Random List Element](http://www.baeldung.com/java-random-list-element)
|
||||
- [Convert String to int or Integer in Java](http://www.baeldung.com/java-convert-string-to-int-or-integer)
|
||||
- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources)
|
||||
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
|
||||
@ -29,7 +22,6 @@
|
||||
- [Sorting in Java](http://www.baeldung.com/java-sorting)
|
||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||
- [Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||
- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [Slope One Algorithm: Collaborative Filtering Recommendation Systems](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Pattern Search with Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||
@ -38,28 +30,19 @@
|
||||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
|
||||
- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm)
|
||||
- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap)
|
||||
- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers)
|
||||
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
|
||||
- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda)
|
||||
- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn)
|
||||
- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap)
|
||||
- [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions)
|
||||
- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap)
|
||||
- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap)
|
||||
- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap)
|
||||
- [A Quick JUnit vs TestNG Comparison](http://www.baeldung.com/junit-vs-testng)
|
||||
- [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions)
|
||||
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
||||
- [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list)
|
||||
- [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list)
|
||||
- [Using Math.pow in Java](http://www.baeldung.com/java-math-pow)
|
||||
- [Converting Strings to Enums in Java](http://www.baeldung.com/java-string-to-enum)
|
||||
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
|
||||
- [Quick Guide to the Java StringTokenizer](http://www.baeldung.com/java-stringtokenizer)
|
||||
- [JVM Log Forging](http://www.baeldung.com/jvm-log-forging)
|
||||
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java)
|
||||
@ -71,7 +54,6 @@
|
||||
- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null)
|
||||
- [Changing the Order in a Sum Operation Can Produce Different Results?](http://www.baeldung.com/java-floating-point-sum-order)
|
||||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
||||
- [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map)
|
||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||
- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy)
|
||||
- [Introduction to JDBC](http://www.baeldung.com/java-jdbc)
|
||||
@ -87,10 +69,8 @@
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||
- [Guide to hashCode() in Java](http://www.baeldung.com/java-hashcode)
|
||||
- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection)
|
||||
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
|
||||
- [Broadcasting and Multicasting in Java](http://www.baeldung.com/java-broadcast-multicast)
|
||||
- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string)
|
||||
- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
|
||||
@ -108,7 +88,6 @@
|
||||
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break)
|
||||
- [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque)
|
||||
- [Guide to java.util.Formatter](http://www.baeldung.com/java-string-formatter)
|
||||
- [Batch Processing in JDBC](http://www.baeldung.com/jdbc-batch-processing)
|
||||
- [Check if a Java Array Contains a Value](http://www.baeldung.com/java-array-contains-value)
|
||||
@ -122,34 +101,48 @@
|
||||
- [Nested Classes in Java](http://www.baeldung.com/java-nested-classes)
|
||||
- [A Guide to Java Loops](http://www.baeldung.com/java-loops)
|
||||
- [Varargs in Java](http://www.baeldung.com/java-varargs)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to Inner Interfaces in Java](http://www.baeldung.com/java-inner-interfaces)
|
||||
- [Polymorphism in Java](http://www.baeldung.com/java-polymorphism)
|
||||
- [Recursion In Java](http://www.baeldung.com/java-recursion)
|
||||
- [A Guide to the finalize Method in Java](http://www.baeldung.com/java-finalize)
|
||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||
- [Method Overloading and Overriding in Java](http://www.baeldung.com/java-method-overload-override)
|
||||
- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set)
|
||||
- [Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
|
||||
- [Java TreeMap vs HashMap](http://www.baeldung.com/java-treemap-vs-hashmap)
|
||||
- [A Guide to Iterator in Java](http://www.baeldung.com/java-iterator)
|
||||
- [The Trie Data Structure in Java](http://www.baeldung.com/trie-java)
|
||||
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
|
||||
- [How to TDD a List Implementation](http://jira.baeldung.com/browse/BAEL-1537)
|
||||
- [How to Make a Deep Copy of an Object in Java](http://www.baeldung.com/java-deep-copy)
|
||||
- [Check if a String is a Palindrome](http://www.baeldung.com/java-palindrome)
|
||||
- [Comparing Strings in Java](http://www.baeldung.com/java-compare-strings)
|
||||
- [Guide to Inheritance in Java](http://www.baeldung.com/java-inheritance)
|
||||
- [Guide to Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
||||
- [The Observer Pattern in Java](https://github.com/eugenp/tutorials/tree/master/core-java)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [Object Type Casting in Java](http://www.baeldung.com/java-type-casting)
|
||||
- [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat)
|
||||
- [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os)
|
||||
- [How to TDD a List Implementation in Java](http://www.baeldung.com/java-test-driven-list)
|
||||
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
||||
- [An Advanced Tagging Implementation with JPA](http://www.baeldung.com/jpa-tagging-advanced)
|
||||
- [Handling Daylight Savings Time in Java](http://www.baeldung.com/java-daylight-savings)
|
||||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
|
||||
- [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max)
|
||||
- [The "final" Keyword in Java](http://www.baeldung.com/java-final)
|
||||
- [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
|
||||
- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java)
|
||||
- [A Guide to the Java LinkedList](http://www.baeldung.com/java-linkedlist)
|
||||
- [A Guide to Java Enums](http://www.baeldung.com/a-guide-to-java-enums)
|
||||
- [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
|
||||
- [Quick Guide to java.lang.System](http://www.baeldung.com/java-lang-system)
|
||||
- [Class Loaders in Java](http://www.baeldung.com/java-classloaders)
|
||||
- [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average)
|
||||
- [Java List UnsupportedOperationException](http://www.baeldung.com/java-list-unsupported-operation-exception)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure)
|
||||
- [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger)
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
- [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split)
|
||||
- [Check If Two Lists are Equal in Java](http://www.baeldung.com/java-test-a-list-for-ordinality-and-equality)
|
||||
- [Sending Emails with Java](http://www.baeldung.com/java-email)
|
||||
- [Introduction to SSL in Java](http://www.baeldung.com/java-ssl)
|
||||
- [Java KeyStore API](http://www.baeldung.com/java-keystore)
|
||||
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
|
||||
|
@ -9,27 +9,12 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
@ -148,12 +133,6 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
@ -454,15 +433,12 @@
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>22.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<decimal4j.version>1.0.3</decimal4j.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
@ -475,7 +451,6 @@
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<mockito.version>2.8.9</mockito.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
|
@ -25,3 +25,4 @@
|
||||
- [Objects in Kotlin](http://www.baeldung.com/kotlin-objects)
|
||||
- [Reading from a File in Kotlin](http://www.baeldung.com/kotlin-read-file)
|
||||
- [Guide to Kotlin @JvmField](http://www.baeldung.com/kotlin-jvm-field-annotation)
|
||||
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
|
||||
|
@ -25,12 +25,6 @@
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import java.io.File
|
||||
|
||||
class FileWriter {
|
||||
|
||||
fun writeFileUsingPrintWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).printWriter().use { out -> out.print(fileContent) }
|
||||
|
||||
fun writeFileUsingBufferedWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).bufferedWriter().use { out -> out.write(fileContent) }
|
||||
|
||||
fun writeFileDirectly(fileName: String, fileContent: String) =
|
||||
File(fileName).writeText(fileContent)
|
||||
|
||||
fun writeFileDirectlyAsBytes(fileName: String, fileContent: String) =
|
||||
File(fileName).writeBytes(fileContent.toByteArray())
|
||||
|
||||
}
|
84
core-kotlin/src/main/kotlin/com/baeldung/lambda/Lambda.kt
Normal file
84
core-kotlin/src/main/kotlin/com/baeldung/lambda/Lambda.kt
Normal file
@ -0,0 +1,84 @@
|
||||
package com.baeldung.lambda
|
||||
|
||||
fun inferredType(input: Int): Int {
|
||||
val square = { number: Int -> number * number }
|
||||
|
||||
return square(input)
|
||||
}
|
||||
|
||||
fun intToBiggerString(argument: Int): String {
|
||||
|
||||
val magnitude100String = { input: Int ->
|
||||
val magnitude = input * 100
|
||||
magnitude.toString()
|
||||
}
|
||||
|
||||
return magnitude100String(argument)
|
||||
}
|
||||
|
||||
fun manyLambda(nums: Array<Int>): List<String> {
|
||||
val newList = nums.map { intToBiggerString(it) }
|
||||
|
||||
return newList
|
||||
}
|
||||
|
||||
fun empty() {
|
||||
val noReturn: (Int) -> Unit = { num -> println(num) }
|
||||
|
||||
noReturn(5)
|
||||
}
|
||||
|
||||
fun invokeLambda(lambda: (Double) -> Boolean): Boolean {
|
||||
return lambda(4.329)
|
||||
}
|
||||
|
||||
fun extendString(arg: String, num: Int): String {
|
||||
val another: String.(Int) -> String = { this + it }
|
||||
|
||||
return arg.another(num)
|
||||
}
|
||||
|
||||
fun getCalculationLambda(): (Int) -> Any {
|
||||
val calculateGrade = { grade: Int ->
|
||||
when (grade) {
|
||||
in 0..40 -> "Fail"
|
||||
in 41..70 -> "Pass"
|
||||
in 71..100 -> "Distinction"
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
||||
|
||||
fun getCalculationLambdaWithReturn(): (Int) -> String {
|
||||
val calculateGrade: Int.() -> String = lambda@{
|
||||
if (this < 0 || this > 100) {
|
||||
return@lambda "Error"
|
||||
} else if (this < 40) {
|
||||
return@lambda "Fail"
|
||||
} else if (this < 70) {
|
||||
return@lambda "Pass"
|
||||
}
|
||||
|
||||
"Distinction"
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
||||
|
||||
fun getCalculationAnonymousFunction(): (Int) -> String {
|
||||
val calculateGrade = fun(grade: Int): String {
|
||||
if (grade < 0 || grade > 100) {
|
||||
return "Error"
|
||||
} else if (grade < 40) {
|
||||
return "Fail"
|
||||
} else if (grade < 70) {
|
||||
return "Pass"
|
||||
}
|
||||
|
||||
return "Distinction"
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.lambda;
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by Paul Jervis on 24/04/2018.
|
||||
*/
|
||||
class LambdaKotlinTest {
|
||||
|
||||
@Test
|
||||
void givenJava6_whenUsingAnonnymousClass_thenReturnLambdaResult() {
|
||||
assertTrue(LambdaKt.invokeLambda(new Function1<Double, Boolean>() {
|
||||
@Override
|
||||
public Boolean invoke(Double c) {
|
||||
return c >= 0;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJava8_whenUsingLambda_thenReturnLambdaResult() {
|
||||
assertTrue(LambdaKt.invokeLambda(c -> c >= 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJava8_whenCallingMethodWithStringExtension_thenImplementExtension() {
|
||||
String actual = LambdaKt.extendString("Word", 90);
|
||||
String expected = "Word90";
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal class FileWriterTest {
|
||||
|
||||
private val fileName = "src/test/resources/Kotlin.out"
|
||||
|
||||
private val fileContent = "Kotlin\nConcise, Safe, Interoperable, Tool-friendly"
|
||||
|
||||
private val fileWriter = FileWriter()
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithPrintWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingPrintWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithBufferedWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingBufferedWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectly_thenCorrect() {
|
||||
fileWriter.writeFileDirectly(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectlyAsBytes_thenCorrect() {
|
||||
fileWriter.writeFileDirectlyAsBytes(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.lambda
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LambdaTest {
|
||||
@Test
|
||||
fun whenCallingALambda_thenPerformTheAction() {
|
||||
assertEquals(9, inferredType(3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenCallingAMoreComplicatedLambda_thenPerformTheAction() {
|
||||
assertEquals("500", intToBiggerString(5))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaObject_thenCallTriggerLambda() {
|
||||
val lambda = { arg: Double ->
|
||||
arg == 4.329
|
||||
}
|
||||
|
||||
val result = invokeLambda(lambda)
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaLiteral_thenCallTriggerLambda() {
|
||||
val result = invokeLambda({
|
||||
true
|
||||
})
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaLiteralOutsideBrackets_thenCallTriggerLambda() {
|
||||
val result = invokeLambda { arg -> arg.isNaN() }
|
||||
|
||||
assertFalse(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingAnAnonymousFunction_thenCallTriggerLambda() {
|
||||
val result = invokeLambda(fun(arg: Double): Boolean {
|
||||
return arg >= 0
|
||||
})
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingLambda_thenCalculateGrade() {
|
||||
val gradeCalculation = getCalculationLambda()
|
||||
|
||||
assertEquals(false, gradeCalculation(-40))
|
||||
assertEquals("Pass", gradeCalculation(50))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingReturnStatementLambda_thenCalculateGrade() {
|
||||
val gradeCalculation: Int.() -> String = getCalculationLambdaWithReturn()
|
||||
|
||||
assertEquals("Distinction", 80.gradeCalculation())
|
||||
assertEquals("Error", 244_234_324.gradeCalculation())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingAnonymousFunction_thenCalculateGrade() {
|
||||
val gradeCalculation = getCalculationAnonymousFunction()
|
||||
|
||||
assertEquals("Error", gradeCalculation(244_234_324))
|
||||
assertEquals("Pass", gradeCalculation(50))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingAFunctionReference_thenCallTriggerLambda() {
|
||||
val reference = Double::isFinite
|
||||
val result = invokeLambda(reference)
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArray_whenMappingArray_thenPerformCalculationOnAllElements() {
|
||||
val expected = listOf("100", "200", "300", "400", "500")
|
||||
val actual = manyLambda(arrayOf(1, 2, 3, 4, 5))
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
}
|
@ -6,8 +6,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-spring</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -47,18 +48,13 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${apache-poi-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<http-component-version>4.4.6</http-component-version>
|
||||
<drools-version>7.4.1.Final</drools-version>
|
||||
<apache-poi-version>3.13</apache-poi-version>
|
||||
<spring-core.version>4.3.6.RELEASE</spring-core.version>
|
||||
<spring.version>4.3.6.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -2,3 +2,4 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to EthereumJ](http://www.baeldung.com/ethereumj)
|
||||
- [Creating and Deploying Smart Contracts with Solidity](http://www.baeldung.com/smart-contracts-ethereum-solidity)
|
||||
|
10
gson/pom.xml
10
gson/pom.xml
@ -8,8 +8,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -19,11 +20,6 @@
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
|
@ -8,19 +8,11 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guava.version>18.0</guava.version>
|
||||
</properties>
|
||||
|
@ -8,19 +8,11 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guava.version>19.0</guava.version>
|
||||
</properties>
|
||||
|
@ -7,18 +7,12 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jool</artifactId>
|
||||
|
@ -8,17 +8,13 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -1,2 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Testing with Hamcrest](http://www.baeldung.com/java-junit-hamcrest-guide)
|
||||
- [Hamcrest File Matchers](http://www.baeldung.com/hamcrest-file-matchers)
|
||||
|
@ -41,11 +41,18 @@
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<log4j2.version>2.8.2</log4j2.version>
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
public class MultiVitamin {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
public class MultiVitaminAllowingNulls {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
public class MultiVitaminOverloading {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
public class MultiVitaminStaticFactoryMethods {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
public class MultiVitaminWithBuilder {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.optionalparams;
|
||||
package com.stackify.optionalparams;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -11,11 +11,6 @@
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.0.0-M4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
@ -62,4 +57,8 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
||||
</properties>
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user