Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-2550
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
public class Bicycle {
|
||||
|
||||
private String brand;
|
||||
private Integer frameSize;
|
||||
|
||||
public Bicycle(String brand) {
|
||||
this.brand = brand;
|
||||
this.frameSize = 0;
|
||||
}
|
||||
|
||||
public Bicycle(String brand, Integer frameSize) {
|
||||
this.brand = brand;
|
||||
this.frameSize = frameSize;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public Integer getFrameSize() {
|
||||
return frameSize;
|
||||
}
|
||||
|
||||
public void setFrameSize(Integer frameSize) {
|
||||
this.frameSize = frameSize;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class BicycleComparator implements Comparator<Bicycle> {
|
||||
|
||||
@Override
|
||||
public int compare(Bicycle a, Bicycle b) {
|
||||
return a.getFrameSize()
|
||||
.compareTo(b.getFrameSize());
|
||||
}
|
||||
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MethodReferenceExamples {
|
||||
|
||||
private static <T> void doNothingAtAll(Object... o) {
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
@Test
|
||||
public void referenceToStaticMethod() {
|
||||
List<String> messages = Arrays.asList("Hello", "Baeldung", "readers!");
|
||||
messages.forEach((word) -> {
|
||||
System.out.println(word);
|
||||
});
|
||||
messages.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToInstanceMethodOfParticularObject() {
|
||||
BicycleComparator bikeFrameSizeComparator = new BicycleComparator();
|
||||
createBicyclesList().stream()
|
||||
.sorted((a, b) -> bikeFrameSizeComparator.compare(a, b));
|
||||
createBicyclesList().stream()
|
||||
.sorted(bikeFrameSizeComparator::compare);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToInstanceMethodOfArbitratyObjectOfParticularType() {
|
||||
List<Integer> numbers = Arrays.asList(5, 3, 50, 24, 40, 2, 9, 18);
|
||||
numbers.stream()
|
||||
.sorted((a, b) -> Integer.compare(a, b));
|
||||
numbers.stream()
|
||||
.sorted(Integer::compare);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToConstructor() {
|
||||
BiFunction<String, Integer, Bicycle> bikeCreator = (brand, frameSize) -> new Bicycle(brand, frameSize);
|
||||
BiFunction<String, Integer, Bicycle> bikeCreatorMethodReference = Bicycle::new;
|
||||
List<Bicycle> bikes = new ArrayList<>();
|
||||
bikes.add(bikeCreator.apply("Giant", 50));
|
||||
bikes.add(bikeCreator.apply("Scott", 20));
|
||||
bikes.add(bikeCreatorMethodReference.apply("Trek", 35));
|
||||
bikes.add(bikeCreatorMethodReference.apply("GT", 40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToConstructorSimpleExample() {
|
||||
List<String> bikeBrands = Arrays.asList("Giant", "Scott", "Trek", "GT");
|
||||
bikeBrands.stream()
|
||||
.map(Bicycle::new)
|
||||
.toArray(Bicycle[]::new);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void limitationsAndAdditionalExamples() {
|
||||
createBicyclesList().forEach(b -> System.out.printf("Bike brand is '%s' and frame size is '%d'%n", b.getBrand(), b.getFrameSize()));
|
||||
createBicyclesList().forEach((o) -> MethodReferenceExamples.doNothingAtAll(o));
|
||||
}
|
||||
|
||||
private List<Bicycle> createBicyclesList() {
|
||||
List<Bicycle> bikes = new ArrayList<>();
|
||||
bikes.add(new Bicycle("Giant", 50));
|
||||
bikes.add(new Bicycle("Scott", 20));
|
||||
bikes.add(new Bicycle("Trek", 35));
|
||||
bikes.add(new Bicycle("GT", 40));
|
||||
return bikes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,6 +36,22 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.trove4j</groupId>
|
||||
<artifactId>trove4j</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>it.unimi.dsi</groupId>
|
||||
<artifactId>fastutil</artifactId>
|
||||
<version>8.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>colt</groupId>
|
||||
<artifactId>colt</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.list.primitive;
|
||||
|
||||
import com.google.common.primitives.ImmutableIntArray;
|
||||
import com.google.common.primitives.Ints;
|
||||
import gnu.trove.list.array.TIntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class PrimitiveCollections {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] primitives = new int[] {5, 10, 0, 2, -8};
|
||||
|
||||
guavaPrimitives(primitives);
|
||||
|
||||
intStream(primitives);
|
||||
|
||||
TIntArrayList tList = new TIntArrayList(primitives);
|
||||
|
||||
cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(primitives);
|
||||
|
||||
IntArrayList fastUtilList = new IntArrayList(primitives);
|
||||
|
||||
System.out.println(tList);
|
||||
|
||||
System.out.println(coltList);
|
||||
|
||||
System.out.println(fastUtilList);
|
||||
}
|
||||
|
||||
private static void intStream(int[] primitives) {
|
||||
|
||||
IntStream stream = IntStream.of(5, 10, 0, 2, -8);
|
||||
|
||||
IntStream newStream = IntStream.of(primitives);
|
||||
|
||||
OptionalDouble average = stream.filter(i -> i > 0).average();
|
||||
}
|
||||
|
||||
|
||||
private static void guavaPrimitives(int[] primitives) {
|
||||
|
||||
ImmutableIntArray immutableIntArray = ImmutableIntArray.builder().addAll(primitives).build();
|
||||
System.out.println(immutableIntArray);
|
||||
}
|
||||
}
|
||||
@@ -36,4 +36,5 @@
|
||||
- [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array)
|
||||
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
|
||||
- [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension)
|
||||
- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
|
||||
- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
|
||||
- [Create a Directory in Java](https://www.baeldung.com/java-create-directory)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.files;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ListFiles {
|
||||
public static final int DEPTH = 1;
|
||||
|
||||
public Set<String> listFilesUsingJavaIO(String dir) {
|
||||
return Stream.of(new File(dir).listFiles())
|
||||
.filter(file -> !file.isDirectory())
|
||||
.map(File::getName)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
|
||||
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
|
||||
return stream.filter(file -> !Files.isDirectory(file))
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
|
||||
Set<String> fileList = new HashSet<>();
|
||||
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if (!Files.isDirectory(file)) {
|
||||
fileList.add(file.getFileName()
|
||||
.toString());
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
|
||||
Set<String> fileList = new HashSet<>();
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
|
||||
for (Path path : stream) {
|
||||
if (!Files.isDirectory(path)) {
|
||||
fileList.add(path.getFileName()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.files.ListFiles;
|
||||
|
||||
public class ListFilesUnitTest {
|
||||
|
||||
private ListFiles listFiles = new ListFiles();
|
||||
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
|
||||
private static final int DEPTH = 1;
|
||||
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
|
||||
{
|
||||
add("test.xml");
|
||||
add("employee.json");
|
||||
add("students.json");
|
||||
add("country.txt");
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenDir_whenUsingJAVAIO_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a sample txt file for unit test ListFilesUnitTest
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1 @@
|
||||
<xml></xml>
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.variable.scope.examples;
|
||||
|
||||
public class BracketScopeExample {
|
||||
|
||||
public void mathOperationExample() {
|
||||
Integer sum = 0;
|
||||
{
|
||||
Integer number = 2;
|
||||
sum = sum + number;
|
||||
}
|
||||
// compiler error, number cannot be solved as a variable
|
||||
// number++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.variable.scope.examples;
|
||||
|
||||
public class ClassScopeExample {
|
||||
|
||||
Integer amount = 0;
|
||||
|
||||
public void exampleMethod() {
|
||||
amount++;
|
||||
}
|
||||
|
||||
public void anotherExampleMethod() {
|
||||
Integer anotherAmount = amount + 4;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.variable.scope.examples;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class LoopScopeExample {
|
||||
|
||||
List<String> listOfNames = Arrays.asList("Joe", "Susan", "Pattrick");
|
||||
|
||||
public void iterationOfNames() {
|
||||
String allNames = "";
|
||||
for (String name : listOfNames) {
|
||||
allNames = allNames + " " + name;
|
||||
}
|
||||
// compiler error, name cannot be resolved to a variable
|
||||
// String lastNameUsed = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.variable.scope.examples;
|
||||
|
||||
public class MethodScopeExample {
|
||||
|
||||
public void methodA() {
|
||||
Integer area = 2;
|
||||
}
|
||||
|
||||
public void methodB() {
|
||||
// compiler error, area cannot be resolved to a variable
|
||||
// area = area + 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.baeldung.variable.scope.examples;
|
||||
|
||||
public class NestedScopesExample {
|
||||
|
||||
String title = "Baeldung";
|
||||
|
||||
public void printTitle() {
|
||||
System.out.println(title);
|
||||
String title = "John Doe";
|
||||
System.out.println(title);
|
||||
}
|
||||
}
|
||||
@@ -46,3 +46,4 @@
|
||||
- [Graphs in Java](https://www.baeldung.com/java-graphs)
|
||||
- [Console I/O in Java](http://www.baeldung.com/java-console-input-output)
|
||||
- [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
|
||||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
|
||||
|
||||
+9
-9
@@ -10,7 +10,7 @@ public class BitwiseOperatorUnitTest {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 & value2;
|
||||
assertEquals(result, 4);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -18,7 +18,7 @@ public class BitwiseOperatorUnitTest {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 | value2;
|
||||
assertEquals(result, 7);
|
||||
assertEquals(7, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -26,7 +26,7 @@ public class BitwiseOperatorUnitTest {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 ^ value2;
|
||||
assertEquals(result, 3);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -40,42 +40,42 @@ public class BitwiseOperatorUnitTest {
|
||||
public void givenOnePositiveInteger_whenSignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int rightShift = value >> 2;
|
||||
assertEquals(rightShift, 3);
|
||||
assertEquals(3, rightShift);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenSignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int rightShift = value >> 2;
|
||||
assertEquals(rightShift, -3);
|
||||
assertEquals(-3, rightShift);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOnePositiveInteger_whenLeftShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int leftShift = value << 2;
|
||||
assertEquals(leftShift, 48);
|
||||
assertEquals(48, leftShift);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenLeftShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int leftShift = value << 2;
|
||||
assertEquals(leftShift, -48);
|
||||
assertEquals(-48, leftShift);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOnePositiveInteger_whenUnsignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int unsignedRightShift = value >>> 2;
|
||||
assertEquals(unsignedRightShift, 3);
|
||||
assertEquals(3, unsignedRightShift);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenUnsignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int unsignedRightShift = value >>> 2;
|
||||
assertEquals(unsignedRightShift, 1073741821);
|
||||
assertEquals(1073741821, unsignedRightShift);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.commons.collections4.MultiMapUtils;
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
|
||||
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MultiValuedMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutMethod_thenReturningAllValues() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
|
||||
map.put("key", "value1");
|
||||
map.put("key", "value2");
|
||||
map.put("key", "value2");
|
||||
|
||||
assertThat((Collection<String>) map.get("key")).containsExactly("value1", "value2", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutAllMethod_thenReturningAllValues() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
|
||||
map.putAll("key", Arrays.asList("value1", "value2", "value2"));
|
||||
|
||||
assertThat((Collection<String>) map.get("key")).containsExactly("value1", "value2", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenGettingValueUsingGetMethod_thenReturningValue() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
|
||||
assertThat((Collection<String>) map.get("key")).containsExactly("value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingEntriesMethod_thenReturningMappings() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value1");
|
||||
map.put("key", "value2");
|
||||
|
||||
Collection<Entry<String, String>> entries = (Collection<Entry<String, String>>) map.entries();
|
||||
|
||||
for(Map.Entry<String,String> entry : entries) {
|
||||
assertThat(entry.getKey()).contains("key");
|
||||
assertTrue(entry.getValue().equals("value1") || entry.getValue().equals("value2") );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingKeysMethod_thenReturningAllKeys() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertThat(((Collection<String>) map.keys())).contains("key", "key1", "key2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingKeySetMethod_thenReturningAllKeys() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertThat((Collection<String>) map.keySet()).contains("key", "key1", "key2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingValuesMethod_thenReturningAllValues() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertThat(((Collection<String>) map.values())).contains("value", "value1", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingRemoveMethod_thenReturningUpdatedMap() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
assertThat(((Collection<String>) map.values())).contains("value", "value1", "value2");
|
||||
|
||||
map.remove("key");
|
||||
|
||||
assertThat(((Collection<String>) map.values())).contains("value1", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingRemoveMappingMethod_thenReturningUpdatedMapAfterMappingRemoved() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
assertThat(((Collection<String>) map.values())).contains("value", "value1", "value2");
|
||||
|
||||
map.removeMapping("key", "value");
|
||||
|
||||
assertThat(((Collection<String>) map.values())).contains("value1", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingClearMethod_thenReturningEmptyMap() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
assertThat(((Collection<String>) map.values())).contains("value", "value1", "value2");
|
||||
|
||||
map.clear();
|
||||
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingContainsKeyMethod_thenReturningTrue() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertTrue(map.containsKey("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingContainsValueMethod_thenReturningTrue() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertTrue(map.containsValue("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingIsEmptyMethod_thenReturningFalse() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertFalse(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiValuesMap_whenUsingSizeMethod_thenReturningElementCount() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
map.put("key", "value");
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
|
||||
assertEquals(3, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayListValuedHashMap_whenPuttingDoubleValues_thenReturningAllValues() {
|
||||
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
|
||||
|
||||
map.put("key", "value1");
|
||||
map.put("key", "value2");
|
||||
map.put("key", "value2");
|
||||
|
||||
assertThat((Collection<String>) map.get("key")).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("key", "value1");
|
||||
map.put("key", "value2");
|
||||
MultiValuedMap<String, String> immutableMap = MultiMapUtils.unmodifiableMultiValuedMap(map);
|
||||
|
||||
immutableMap.put("key", "value3");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -15,3 +15,4 @@
|
||||
- [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering)
|
||||
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
|
||||
- [Java Stream Filter with Lambda Expression](https://www.baeldung.com/java-stream-filter-lambda)
|
||||
- [Counting Matches on a Stream Filter](https://www.baeldung.com/java-stream-filter-count)
|
||||
|
||||
@@ -95,6 +95,12 @@
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.ahocorasick</groupId>
|
||||
<artifactId>ahocorasick</artifactId>
|
||||
<version>0.4.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.ahocorasick.trie.Emit;
|
||||
import org.ahocorasick.trie.Token;
|
||||
import org.ahocorasick.trie.Trie;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MatchWords {
|
||||
|
||||
public static boolean containsWordsIndexOf(String inputString, String[] words) {
|
||||
boolean found = true;
|
||||
for (String word : words) {
|
||||
if (inputString.indexOf(word) == -1) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWords(String inputString, String[] items) {
|
||||
boolean found = true;
|
||||
for (String item : items) {
|
||||
if (!inputString.contains(item)) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWordsAhoCorasick(String inputString, String[] words) {
|
||||
Trie trie = Trie.builder()
|
||||
.onlyWholeWords()
|
||||
.addKeywords(words)
|
||||
.build();
|
||||
|
||||
Collection<Emit> emits = trie.parseText(inputString);
|
||||
emits.forEach(System.out::println);
|
||||
|
||||
boolean found = true;
|
||||
for(String word : words) {
|
||||
boolean contains = Arrays.toString(emits.toArray()).contains(word);
|
||||
if (!contains) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWordsPatternMatch(String inputString, String[] words) {
|
||||
|
||||
StringBuilder regexp = new StringBuilder();
|
||||
for (String word : words) {
|
||||
regexp.append("(?=.*").append(word).append(")");
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile(regexp.toString());
|
||||
|
||||
return pattern.matcher(inputString).find();
|
||||
}
|
||||
|
||||
public static boolean containsWordsJava8(String inputString, String[] words) {
|
||||
List<String> inputStringList = Arrays.asList(inputString.split(" "));
|
||||
List<String> wordsList = Arrays.asList(words);
|
||||
|
||||
return wordsList.stream().allMatch(inputStringList::contains);
|
||||
}
|
||||
|
||||
public static boolean containsWordsArray(String inputString, String[] words) {
|
||||
List<String> inputStringList = Arrays.asList(inputString.split(" "));
|
||||
List<String> wordsList = Arrays.asList(words);
|
||||
|
||||
return inputStringList.containsAll(wordsList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MatchWordsUnitTest {
|
||||
|
||||
private final String[] words = {"hello", "Baeldung"};
|
||||
private final String inputString = "hello there, Baeldung";
|
||||
private final String wholeInput = "helloBaeldung";
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingStringContains_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWords(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingJava8_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsJava8(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingJava8_shouldNotMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsJava8(wholeInput, words);
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingPattern_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsPatternMatch(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingAhoCorasick_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsAhoCorasick(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingAhoCorasick_shouldNotMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsAhoCorasick(wholeInput, words);
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingIndexOf_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsIndexOf(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingArrayList_shouldMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsArray(inputString, words);
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenCallingArrayList_shouldNotMatchWords() {
|
||||
final boolean result = MatchWords.containsWordsArray(wholeInput, words);
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,5 @@
|
||||
- [Lombok @Builder with Inheritance](https://www.baeldung.com/lombok-builder-inheritance)
|
||||
- [Lombok Builder with Default Value](https://www.baeldung.com/lombok-builder-default-value)
|
||||
- [Lombok Builder with Custom Setter](https://www.baeldung.com/lombok-builder-custom-setter)
|
||||
- [Setting up Lombok with Eclipse and Intellij](https://www.baeldung.com/lombok-ide)
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Student extends Child {
|
||||
|
||||
private final String schoolName;
|
||||
|
||||
@Builder(builderMethodName = "studentBuilder")
|
||||
public Student(String parentName, int parentAge, String childName, int childAge, String schoolName) {
|
||||
super(parentName, parentAge, childName, childAge);
|
||||
this.schoolName = schoolName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Child extends Parent {
|
||||
private final String childName;
|
||||
private final int childAge;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Parent {
|
||||
private final String parentName;
|
||||
private final int parentAge;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user