Merge branch 'master' into BAEL-1711-move-code-snippets
This commit is contained in:
@@ -34,3 +34,9 @@ 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)
|
||||
|
||||
@@ -20,4 +20,5 @@
|
||||
- [A Maze Solver in Java](http://www.baeldung.com/java-solve-maze)
|
||||
- [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)
|
||||
- [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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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)
|
||||
|
||||
+3
-7
@@ -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>
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
|
||||
+3
-8
@@ -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,28 @@
|
||||
package com.baeldung.hashtable;
|
||||
|
||||
public class Word {
|
||||
private String name;
|
||||
|
||||
public Word(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Word))
|
||||
return false;
|
||||
|
||||
Word word = (Word) o;
|
||||
return word.getName().equals(this.name) ? true : false;
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package com.baeldung.hashtable;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashtableUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenPutAndGet_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
|
||||
String definition = table.get(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
|
||||
definition = table.remove(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenThesameInstanceOfKey_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(word);
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEqualsOverridden_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(new Word("cat"));
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenNullKey_thenException() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(null, "an animal");
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenIterate_thenFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "an animal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
|
||||
Iterator<Word> it = table.keySet().iterator();
|
||||
System.out.println("iterator created");
|
||||
|
||||
table.remove(new Word("dog"));
|
||||
System.out.println("element removed");
|
||||
|
||||
while (it.hasNext()) {
|
||||
Word key = it.next();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEnumerate_thenNotFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Enumeration<Word> enumKey = table.keys();
|
||||
System.out.println("Enumeration created");
|
||||
table.remove(new Word("1"));
|
||||
System.out.println("element removed");
|
||||
while (enumKey.hasMoreElements()) {
|
||||
Word key = enumKey.nextElement();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddElements_thenIterationOrderUnpredicable() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Word, String> entry = it.next();
|
||||
System.out.println(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetOrDefault_thenDefaultGot() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
Word key = new Word("dog");
|
||||
String definition;
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(key)) {
|
||||
definition = table.get(key);
|
||||
} else {
|
||||
definition = "not found";
|
||||
}*/
|
||||
|
||||
// new way
|
||||
definition = table.getOrDefault(key, "not found");
|
||||
|
||||
assertThat(definition, is("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPutifAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
// old way
|
||||
/* if (!table.containsKey(new Word("cat"))) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.putIfAbsent(new Word("cat"), definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovePair_thenCheckKeyAndValue() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (table.get(new Word("cat")).equals("an animal")) {
|
||||
table.remove(new Word("cat"));
|
||||
}*/
|
||||
|
||||
// new way
|
||||
boolean result = table.remove(new Word("cat"), "an animal");
|
||||
|
||||
assertThat(result, is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplacePair_thenValueChecked() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("an animal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (!table.containsKey(cat)) {
|
||||
String definition = "an animal";// calculate
|
||||
table.put(new Word("cat"), definition);
|
||||
}
|
||||
*/
|
||||
// new way
|
||||
|
||||
table.computeIfAbsent(new Word("cat"), key -> "an animal");
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenComputeIfPresent() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
Word cat = new Word("cat");
|
||||
// old way
|
||||
/* if (table.containsKey(cat)) {
|
||||
String concatination = cat.getName() + " - " + table.get(cat);
|
||||
table.put(cat, concatination);
|
||||
}*/
|
||||
|
||||
// new way
|
||||
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
|
||||
|
||||
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompute_thenForAllKeys() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsteadOfCompute_thenMerge() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenForeach_thenIterate() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceall_thenNoIterationNeeded() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.replaceAll((k, v) -> k.getName() + " - " + v);
|
||||
|
||||
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
<name>core-java-collections</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -25,11 +26,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>
|
||||
@@ -55,7 +51,6 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guava.version>22.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user