Merge branch 'eugenp:master' into master
This commit is contained in:
+31
-27
@@ -1,46 +1,50 @@
|
||||
package com.baeldung.httpclient.cookies;
|
||||
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.cookie.ClientCookie;
|
||||
import org.apache.http.cookie.Cookie;
|
||||
import org.apache.http.impl.client.BasicCookieStore;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.cookie.BasicClientCookie;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
||||
import org.apache.hc.client5.http.cookie.Cookie;
|
||||
import org.apache.hc.client5.http.cookie.CookieStore;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
|
||||
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
public class HttpClientGettingCookieValueUnitTest {
|
||||
class HttpClientGettingCookieValueUnitTest {
|
||||
private static Logger log = LoggerFactory.getLogger(HttpClientGettingCookieValueUnitTest.class);
|
||||
|
||||
private static final String SAMPLE_URL = "http://www.baeldung.com/";
|
||||
|
||||
@Test
|
||||
public final void whenSettingCustomCookieOnTheRequest_thenGettingTheSameCookieFromTheResponse() throws IOException {
|
||||
void whenSettingCustomCookieOnTheRequest_thenGettingTheSameCookieFromTheResponse() throws IOException {
|
||||
|
||||
HttpClientContext context = HttpClientContext.create();
|
||||
context.setAttribute(HttpClientContext.COOKIE_STORE, createCustomCookieStore());
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
try (CloseableHttpResponse response = httpClient.execute(new HttpGet(SAMPLE_URL), context)) {
|
||||
CookieStore cookieStore = context.getCookieStore();
|
||||
Cookie customCookie = cookieStore.getCookies()
|
||||
.stream()
|
||||
.peek(cookie -> log.info("cookie name:{}", cookie.getName()))
|
||||
.filter(cookie -> "custom_cookie".equals(cookie.getName()))
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalStateException::new);
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
|
||||
assertEquals("test_value", customCookie.getValue());
|
||||
}
|
||||
try (CloseableHttpClient client = HttpClientBuilder.create()
|
||||
.build()) {
|
||||
client.execute(request, context, new BasicHttpClientResponseHandler());
|
||||
CookieStore cookieStore = context.getCookieStore();
|
||||
Cookie customCookie = cookieStore.getCookies()
|
||||
.stream()
|
||||
.peek(cookie -> log.info("cookie name:{}", cookie.getName()))
|
||||
.filter(cookie -> "custom_cookie".equals(cookie.getName()))
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalStateException::new);
|
||||
|
||||
assertEquals("test_value", customCookie.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +52,7 @@ public class HttpClientGettingCookieValueUnitTest {
|
||||
BasicCookieStore cookieStore = new BasicCookieStore();
|
||||
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
|
||||
cookie.setDomain("baeldung.com");
|
||||
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
|
||||
cookie.setAttribute("domain", "true");
|
||||
cookie.setPath("/");
|
||||
cookieStore.addCookie(cookie);
|
||||
return cookieStore;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
## Relevant Articles
|
||||
- [Record Patterns in Java 19](https://www.baeldung.com/java-19-record-patterns)
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.monad;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
class MonadBaseExample {
|
||||
|
||||
public double multiplyBy2(double n) {
|
||||
return n * 2;
|
||||
}
|
||||
|
||||
public double divideBy2(double n) {
|
||||
return n / 2;
|
||||
}
|
||||
|
||||
public double add3(double n) {
|
||||
return n + 3;
|
||||
}
|
||||
|
||||
public double subtract1(double n) {
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MonadSample1 extends MonadBaseExample {
|
||||
|
||||
public double apply(double n) {
|
||||
return subtract1(add3(divideBy2(multiplyBy2(multiplyBy2(2)))));
|
||||
}
|
||||
}
|
||||
|
||||
class MonadSample2 extends MonadBaseExample {
|
||||
public double apply(double n) {
|
||||
double n1 = multiplyBy2(n);
|
||||
double n2 = multiplyBy2(n1);
|
||||
double n3 = divideBy2(n2);
|
||||
double n4 = add3(n3);
|
||||
return subtract1(n4);
|
||||
}
|
||||
}
|
||||
|
||||
class MonadSample3 extends MonadBaseExample {
|
||||
|
||||
public double apply(double n) {
|
||||
return Optional.of(n)
|
||||
.flatMap(value -> Optional.of(multiplyBy2(value)))
|
||||
.flatMap(value -> Optional.of(multiplyBy2(value)))
|
||||
.flatMap(value -> Optional.of(divideBy2(value)))
|
||||
.flatMap(value -> Optional.of(add3(value)))
|
||||
.flatMap(value -> Optional.of(subtract1(value)))
|
||||
.get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MonadSample4 extends MonadBaseExample {
|
||||
public boolean leftIdentity() {
|
||||
Function<Integer, Optional<Integer>> mapping = value -> Optional.of(value + 1);
|
||||
return Optional.of(3).flatMap(mapping).equals(mapping.apply(3));
|
||||
}
|
||||
|
||||
public boolean rightIdentity() {
|
||||
return Optional.of(3).flatMap(Optional::of).equals(Optional.of(3));
|
||||
}
|
||||
|
||||
public boolean associativity() {
|
||||
Function<Integer, Optional<Integer>> mapping = value -> Optional.of(value + 1);
|
||||
Optional<Integer> leftSide = Optional.of(3).flatMap(mapping).flatMap(Optional::of);
|
||||
Optional<Integer> rightSide = Optional.of(3).flatMap(v -> mapping.apply(v).flatMap(Optional::of));
|
||||
return leftSide.equals(rightSide);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MonadSample5 extends MonadBaseExample {
|
||||
public boolean fail() {
|
||||
Function<Integer, Optional<Integer>> mapping = value -> Optional.of(value == null ? -1 : value + 1);
|
||||
return Optional.ofNullable((Integer) null).flatMap(mapping).equals(mapping.apply(null));
|
||||
}
|
||||
}
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.monad;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MonadSampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNotUsingMonad_shouldBeOk() {
|
||||
MonadSample1 test = new MonadSample1();
|
||||
Assert.assertEquals(6.0, test.apply(2), 0.000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotUsingMonadButUsingTempVars_shouldBeOk() {
|
||||
MonadSample2 test = new MonadSample2();
|
||||
Assert.assertEquals(6.0, test.apply(2), 0.000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingMonad_shouldBeOk() {
|
||||
MonadSample3 test = new MonadSample3();
|
||||
Assert.assertEquals(6.0, test.apply(2), 0.000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTestingMonadProperties_shouldBeOk() {
|
||||
MonadSample4 test = new MonadSample4();
|
||||
Assert.assertEquals(true, test.leftIdentity());
|
||||
Assert.assertEquals(true, test.rightIdentity());
|
||||
Assert.assertEquals(true, test.associativity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBreakingMonadProperties_shouldBeFalse() {
|
||||
MonadSample5 test = new MonadSample5();
|
||||
Assert.assertEquals(false, test.fail());
|
||||
}
|
||||
}
|
||||
@@ -6,3 +6,4 @@ This module contains articles about arrays conversion in Java
|
||||
- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)
|
||||
- [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array)
|
||||
- [Convert a Byte Array to a Numeric Representation in Java](https://www.baeldung.com/java-byte-array-to-number)
|
||||
- [Converting a String Array Into an int Array in Java](https://www.baeldung.com/java-convert-string-array-to-int-array)
|
||||
|
||||
@@ -13,3 +13,4 @@
|
||||
- [Getting the Size of an Iterable in Java](https://www.baeldung.com/java-iterable-size)
|
||||
- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections)
|
||||
- [Differences Between Iterator and Iterable and How to Use Them?](https://www.baeldung.com/java-iterator-vs-iterable)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-1) [[next -->]](/core-java-modules/core-java-collections-3)
|
||||
@@ -7,7 +7,6 @@
|
||||
- [ArrayList vs. LinkedList vs. HashMap in Java](https://www.baeldung.com/java-arraylist-vs-linkedlist-vs-hashmap)
|
||||
- [Java Deque vs. Stack](https://www.baeldung.com/java-deque-vs-stack)
|
||||
- [Collection.toArray(new T[0]) or .toArray(new T[size])](https://www.baeldung.com/java-collection-toarray-methods)
|
||||
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
|
||||
- [Sorting Objects in a List by Date](https://www.baeldung.com/java-sort-list-by-date)
|
||||
- [Fixed Size Queue Implementations in Java](https://www.baeldung.com/java-fixed-size-queue)
|
||||
- [Difference Between Java Enumeration and Iterator](https://www.baeldung.com/java-enumeration-vs-iterator)
|
||||
@@ -15,4 +14,4 @@
|
||||
- [Guide to Java PriorityQueue](https://www.baeldung.com/java-priorityqueue)
|
||||
- [Java Generics PECS – Producer Extends Consumer Super](https://www.baeldung.com/java-generics-pecs)
|
||||
- [Reversing a Stack in Java](https://www.baeldung.com/java-reversing-a-stack)
|
||||
- [Sorting a HashSet in Java](https://www.baeldung.com/java-sort-hashset)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-3)
|
||||
@@ -25,6 +25,12 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
-2
@@ -2,9 +2,7 @@ package com.baeldung.collections.comparation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
-2
@@ -2,10 +2,8 @@ package com.baeldung.collections.comparation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EmployeeSortingByDateUnitTest {
|
||||
|
||||
|
||||
+2
-1
@@ -5,8 +5,9 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ComparatorsUnitTest {
|
||||
@Test
|
||||
|
||||
+8
-6
@@ -1,10 +1,12 @@
|
||||
package com.baeldung.stackreversal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.baeldung.collections.sorting.Employee;
|
||||
import com.baeldung.collections.stackreversal.ReverseStackUsingQueue;
|
||||
import com.baeldung.collections.stackreversal.ReverseStackUsingRecursion;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -15,7 +17,7 @@ public class StackReversalUnitTest {
|
||||
ReverseStackUsingQueue reverseStack = new ReverseStackUsingQueue();
|
||||
Stack<Integer> originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack<Integer>());
|
||||
Stack<Integer> reverseList = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack<Integer>());
|
||||
Assert.assertEquals(reverseStack.reverseIntegerStack(originalStack), reverseList);
|
||||
assertEquals(reverseStack.reverseIntegerStack(originalStack), reverseList);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -26,7 +28,7 @@ public class StackReversalUnitTest {
|
||||
Collections.reverse(listOfWordsReversed);
|
||||
Stack<String> originalStack = generateStackFromGivenList(listOfWords, new Stack<String>());
|
||||
Stack<String> reversedStack = generateStackFromGivenList(listOfWordsReversed, new Stack<String>());
|
||||
Assert.assertEquals(stackReversal.reverseStringStack(originalStack), reversedStack);
|
||||
assertEquals(stackReversal.reverseStringStack(originalStack), reversedStack);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,7 +45,7 @@ public class StackReversalUnitTest {
|
||||
Collections.reverse(employeeReversed);
|
||||
Stack<Employee> originalStack = generateStackFromGivenList(employeeList, new Stack<Employee>());
|
||||
Stack<Employee> reverseStack = generateStackFromGivenList(employeeReversed, new Stack<Employee>());
|
||||
Assert.assertEquals(stackReversal.reverseEmployeeStack(originalStack), reverseStack);
|
||||
assertEquals(stackReversal.reverseEmployeeStack(originalStack), reverseStack);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -51,7 +53,7 @@ public class StackReversalUnitTest {
|
||||
ReverseStackUsingRecursion reverseStack = new ReverseStackUsingRecursion();
|
||||
Stack<Integer> originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack<Integer>());
|
||||
Stack<Integer> reversedStack = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack<Integer>());
|
||||
Assert.assertEquals(reverseStack.reverseIntegerStack(originalStack), reversedStack);
|
||||
assertEquals(reverseStack.reverseIntegerStack(originalStack), reversedStack);
|
||||
}
|
||||
|
||||
private Stack generateStackFromGivenList(List elements, Stack stack){
|
||||
|
||||
@@ -5,6 +5,33 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-array-list</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven-compiler-plugin.source}</source>
|
||||
<target>${maven-compiler-plugin.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
--add-opens java.base/java.util=ALL-UNNAMED
|
||||
</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven-compiler-plugin.source>16</maven-compiler-plugin.source>
|
||||
<maven-compiler-plugin.target>16</maven-compiler-plugin.target>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
<name>core-java-collections-array-list</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@@ -20,6 +47,12 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.1-jre</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.list.ignorecase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IgnoreCaseSearchUtil {
|
||||
public static boolean ignoreCaseContains(List<String> theList, String searchStr) {
|
||||
for (String s : theList) {
|
||||
if (searchStr.equalsIgnoreCase(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.list.ignorecase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class IgnoreCaseStringList extends ArrayList<String> {
|
||||
|
||||
public IgnoreCaseStringList() {
|
||||
|
||||
}
|
||||
|
||||
public IgnoreCaseStringList(Collection<? extends String> c) {
|
||||
super(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
String searchStr = (String) o;
|
||||
// Using Stream API:
|
||||
// return this.stream().anyMatch(searchStr::equalsIgnoreCase);
|
||||
for (String s : this) {
|
||||
if (searchStr.equalsIgnoreCase(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class Node {
|
||||
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
public Node(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Node (" + "x=" + x + ", y=" + y + ')';
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class User {
|
||||
private final String fullName;
|
||||
|
||||
public User(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User (" + "full name='" + fullName + ')';
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.triple;
|
||||
|
||||
public class Triple<L, M, R> {
|
||||
|
||||
private final L left;
|
||||
private final M middle;
|
||||
private final R right;
|
||||
|
||||
public Triple(L left, M middle, R right) {
|
||||
this.left = left;
|
||||
this.middle = middle;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public L getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public M getMiddle() {
|
||||
return middle;
|
||||
}
|
||||
|
||||
public R getRight() {
|
||||
return right;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.list.ignorecase;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class IgnoreCaseContainsUnitTest {
|
||||
private static final List<String> LANGUAGES = Arrays.asList("Java", "Python", "Kotlin", "Ruby", "Javascript", "Go");
|
||||
|
||||
@Test
|
||||
void givenStringList_whenCallTheStandardContains_shouldReturnFalse() {
|
||||
String searchStr = "jAvA";
|
||||
boolean result = LANGUAGES.contains(searchStr);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringList_whenSearchIgnoreCaseUsingStreamAPI_shouldReturnTrue() {
|
||||
String searchStr = "koTliN";
|
||||
boolean result = LANGUAGES.stream().anyMatch(searchStr::equalsIgnoreCase);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringList_whenUsingUtilClass_shouldReturnTrue() {
|
||||
String searchStr = "ruBY";
|
||||
boolean result = IgnoreCaseSearchUtil.ignoreCaseContains(LANGUAGES, searchStr);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringList_whenUsingIgnoreCaseStringList_shouldReturnTrue() {
|
||||
String searchStr = "pYtHoN";
|
||||
List<String> ignoreCaseList = new IgnoreCaseStringList(LANGUAGES);
|
||||
boolean result = ignoreCaseList.contains(searchStr);
|
||||
assertTrue(result);
|
||||
|
||||
boolean resultContainAll = ignoreCaseList.containsAll(Arrays.asList("pYtHon", "jAvA", "koTliN", "ruBY"));
|
||||
assertTrue(resultContainAll);
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConvertObjectListToStringListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenForEachUsedToConvert_thenReturnSuccess() {
|
||||
List<String> outputList = new ArrayList<>(objectListWithNull().size());
|
||||
for (Object obj : objectListWithNull()) {
|
||||
outputList.add(Objects.toString(obj, null));
|
||||
}
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toList());
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsUnmodifiableListToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingGuavaTransform_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = Lists.transform(objectListWithNull(), obj -> Objects.toString(obj, null));
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectListWithNoNull_whenUsingToList_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithoutNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.toList();
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithNull() {
|
||||
List<String> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add("1");
|
||||
listOfStrings.add("true");
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.toString(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4).toString());
|
||||
listOfStrings.add(new User("John Doe").toString());
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<Object> objectListWithNull() {
|
||||
List<Object> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add(1);
|
||||
listOfStrings.add(true);
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.valueOf(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4));
|
||||
listOfStrings.add(new User("John Doe"));
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithoutNull() {
|
||||
return List.of("1", "true", "hello", Double.toString(273773.98), new Node(2, 4).toString(), new User("John Doe").toString());
|
||||
}
|
||||
|
||||
private List<Object> objectListWithoutNull() {
|
||||
return List.of(1, true, "hello", Double.valueOf(273773.98), new Node(2, 4), new User("John Doe"));
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package com.baeldung.triple;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TripleInListUnitTest {
|
||||
|
||||
enum OP {
|
||||
PLUS("+"), MINUS("-"), MULTIPLY("x");
|
||||
final String opSign;
|
||||
|
||||
OP(String x) {
|
||||
this.opSign = x;
|
||||
}
|
||||
}
|
||||
|
||||
private String createQuestion(Long num1, OP operator, Long num2) {
|
||||
long result;
|
||||
switch (operator) {
|
||||
case PLUS:
|
||||
result = num1 + num2;
|
||||
break;
|
||||
case MINUS:
|
||||
result = num1 - num2;
|
||||
break;
|
||||
case MULTIPLY:
|
||||
result = num1 * num2;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown operator");
|
||||
}
|
||||
return String.format("%d %s %d = ? ( answer: %d )", num1, operator.opSign, num2, result);
|
||||
}
|
||||
|
||||
private static final List<String> EXPECTED_QUESTIONS = Arrays.asList(
|
||||
"100 - 42 = ? ( answer: 58 )",
|
||||
"100 + 42 = ? ( answer: 142 )",
|
||||
"100 x 42 = ? ( answer: 4200 )");
|
||||
|
||||
@Test
|
||||
void givenTripleValues_whenStoreAsList_thenTypeIsNotSafe() {
|
||||
|
||||
List myTriple1 = new ArrayList(3);
|
||||
myTriple1.add(100L);
|
||||
myTriple1.add(OP.MINUS);
|
||||
myTriple1.add(42L);
|
||||
|
||||
List myTriple2 = new ArrayList(3);
|
||||
myTriple2.add(100L);
|
||||
myTriple2.add(OP.PLUS);
|
||||
myTriple2.add(42L);
|
||||
|
||||
List myTriple3 = new ArrayList(3);
|
||||
myTriple3.add(100L);
|
||||
myTriple3.add(OP.MULTIPLY);
|
||||
myTriple3.add(42L);
|
||||
|
||||
List<List> listOfTriples = new ArrayList<>(Arrays.asList(myTriple1, myTriple2, myTriple3));
|
||||
|
||||
List oopsTriple = new ArrayList(3);
|
||||
oopsTriple.add("Oops");
|
||||
oopsTriple.add(911L);
|
||||
oopsTriple.add("The type is wrong");
|
||||
|
||||
listOfTriples.add(oopsTriple);
|
||||
assertEquals(4, listOfTriples.size());
|
||||
|
||||
List<String> questions = listOfTriples.stream()
|
||||
.filter(
|
||||
triple -> triple.size() == 3
|
||||
&& triple.get(0) instanceof Long
|
||||
&& triple.get(1) instanceof OP
|
||||
&& triple.get(2) instanceof Long
|
||||
).map(triple -> {
|
||||
Long left = (Long) triple.get(0);
|
||||
OP op = (OP) triple.get(1);
|
||||
Long right = (Long) triple.get(2);
|
||||
return createQuestion(left, op, right);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
assertEquals(EXPECTED_QUESTIONS, questions);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTripleValues_whenUsingTheTripleClass_thenTypeIsSafeAndNeat() {
|
||||
Triple<Long, OP, Long> triple1 = new Triple<>(100L, OP.MINUS, 42L);
|
||||
Triple<Long, OP, Long> triple2 = new Triple<>(100L, OP.PLUS, 42L);
|
||||
Triple<Long, OP, Long> triple3 = new Triple<>(100L, OP.MULTIPLY, 42L);
|
||||
Triple<String, Long, String> tripleOops = new Triple<>("Oops", 911L, "The type is wrong");
|
||||
|
||||
List<Triple<Long, OP, Long>> listOfTriples = new ArrayList<>(Arrays.asList(triple1, triple2, triple3));
|
||||
// listOfTriples.add(tripleOops); // Compiler error: "java: incompatible types ... "
|
||||
|
||||
List<String> questions = listOfTriples.stream()
|
||||
.map(triple -> createQuestion(triple.getLeft(), triple.getMiddle(), triple.getRight()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(EXPECTED_QUESTIONS, questions);
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ This module contains articles about Map data structures in Java.
|
||||
- [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)
|
||||
- [Guide to Apache Commons MultiValuedMap](https://www.baeldung.com/apache-commons-multi-valued-map)
|
||||
- [The Java HashMap Under the Hood](https://www.baeldung.com/java-hashmap-advanced)
|
||||
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
|
||||
- More articles: [[next -->]](/core-java-modules/core-java-collections-maps-2)
|
||||
|
||||
@@ -20,6 +20,12 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+4
-3
@@ -1,7 +1,5 @@
|
||||
package com.baeldung.maps.initialize;
|
||||
package com.baeldung.map;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -10,6 +8,9 @@ import java.util.NavigableMap;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class EmptyMapInitializer {
|
||||
|
||||
public static Map<String, String> articleMap;
|
||||
+5
-4
@@ -1,11 +1,12 @@
|
||||
package com.baeldung.maps.initialize;
|
||||
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
package com.baeldung.map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmptyMapInitializerUnitTest {
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
@@ -2,3 +2,5 @@
|
||||
|
||||
- [Using Streams to Collect Into a TreeSet](https://www.baeldung.com/java-stream-collect-into-treeset)
|
||||
- [A Guide to LinkedHashSet in Java](https://www.baeldung.com/java-linkedhashset)
|
||||
- [Sorting a HashSet in Java](https://www.baeldung.com/java-sort-hashset)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-set)
|
||||
@@ -15,6 +15,12 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.collections.sorting;
|
||||
package com.baeldung.hashset.sorting;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -13,5 +13,6 @@ This module contains articles about core Java input and output (IO)
|
||||
- [Generate the MD5 Checksum for a File in Java](https://www.baeldung.com/java-md5-checksum-file)
|
||||
- [Getting the Filename From a String Containing an Absolute File Path](https://www.baeldung.com/java-filename-full-path)
|
||||
- [Mocking Java InputStream Object](https://www.baeldung.com/java-mocking-inputstream)
|
||||
- [PrintStream vs PrintWriter in Java](https://www.baeldung.com/java-printstream-vs-printwriter)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-3)
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user