Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-7487-resolve-multiple-completable-futures
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.equilibriumindex;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class EquilibriumIndexFinder {
|
||||
|
||||
List<Integer> findEquilibriumIndexes(int[] array) {
|
||||
int[] partialSums = new int[array.length + 1];
|
||||
partialSums[0] = 0;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
partialSums[i+1] = partialSums[i] + array[i];
|
||||
}
|
||||
|
||||
List<Integer> equilibriumIndexes = new ArrayList<Integer>();
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (partialSums[i] == (partialSums[array.length] - (partialSums[i+1]))) {
|
||||
equilibriumIndexes.add(i);
|
||||
}
|
||||
}
|
||||
return equilibriumIndexes;
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.equilibriumindex;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class EquilibriumIndexFinderUnitTest {
|
||||
|
||||
@Test
|
||||
void givenArrayHasEquilibriumIndexes_whenFindEquilibriumIndexes_thenListAllEquilibriumIndexes() {
|
||||
int[] array = {1, -3, 0, 4, -5, 4, 0, 1, -2, -1};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).containsExactly(1, 4, 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutEquilibriumIndexes_whenFindEquilibriumIndexes_thenEmptyList() {
|
||||
int[] array = {1, 2, 3};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneElement_whenFindEquilibriumIndexes_thenListFirstIndex() {
|
||||
int[] array = {5};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).containsExactly(0);
|
||||
}
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.datetounixtimestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DateToUnixTimeStampUnitTest {
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingInstantClass_thenConvertToUnixTimeStamp() {
|
||||
Instant givenDate = Instant.parse("2020-09-08T12:16:40Z");
|
||||
|
||||
assertEquals(1599567400L, givenDate.getEpochSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingLocalDateTimeClass_thenConvertToUnixTimeStamp() {
|
||||
LocalDateTime givenDate = LocalDateTime.of(2023, 10, 19, 22, 45);
|
||||
|
||||
assertEquals(1697755500L, givenDate.toEpochSecond(ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingDateClass_thenConvertToUnixTimeStamp() throws ParseException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
Date givenDate = dateFormat.parse("2023-10-15 22:00:00");
|
||||
|
||||
assertEquals(1697407200L, givenDate.getTime() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingCalendarClass_thenConvertToUnixTimeStamp() {
|
||||
Calendar calendar = new GregorianCalendar(2023, Calendar.OCTOBER, 17);
|
||||
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
assertEquals(1697500800L, calendar.getTimeInMillis() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingJodaTimeInstantClass_thenConvertToUnixTimeStamp() {
|
||||
org.joda.time.Instant givenDate = org.joda.time.Instant.parse("2020-09-08T12:16:40Z");
|
||||
|
||||
assertEquals(1599567400L, givenDate.getMillis() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingJodaTimeDateTimeClass_thenConvertToUnixTimeStamp() {
|
||||
DateTime givenDate = new DateTime("2020-09-09T12:16:40Z");
|
||||
|
||||
assertEquals(1599653800L, givenDate.getMillis() / 1000);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-3
@@ -51,9 +51,10 @@ public class ReadInputCharByCharUnitTest {
|
||||
System.setIn(inputStream);
|
||||
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
char[] result = scanner.next().toCharArray();
|
||||
|
||||
assertArrayEquals("TestInput".toCharArray(), result);
|
||||
if (scanner.hasNext()) {
|
||||
char[] result = scanner.next().toCharArray();
|
||||
assertArrayEquals("TestInput".toCharArray(), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.objectmutability;
|
||||
|
||||
public final class ImmutablePerson {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public ImmutablePerson(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.objectmutability;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
public class ImmutableObjectExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImmutableString_whenConcatString_thenNotSameAndCorrectValues() {
|
||||
String originalString = "Hello";
|
||||
String modifiedString = originalString.concat(" World");
|
||||
|
||||
assertNotSame(originalString, modifiedString);
|
||||
|
||||
assertEquals("Hello", originalString);
|
||||
assertEquals("Hello World", modifiedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableInteger_whenAddInteger_thenNotSameAndCorrectValue() {
|
||||
Integer immutableInt = 42;
|
||||
Integer modifiedInt = immutableInt + 8;
|
||||
|
||||
assertNotSame(immutableInt, modifiedInt);
|
||||
|
||||
assertEquals(42, (int) immutableInt);
|
||||
assertEquals(50, (int) modifiedInt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.objectmutability;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ImmutablePersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImmutablePerson_whenAccessFields_thenCorrectValues() {
|
||||
ImmutablePerson person = new ImmutablePerson("John", 30);
|
||||
|
||||
assertEquals("John", person.getName());
|
||||
assertEquals(30, person.getAge());
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.objectmutability;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MutableObjectExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMutableString_whenAppendElement_thenCorrectValue() {
|
||||
StringBuilder mutableString = new StringBuilder("Hello");
|
||||
mutableString.append(" World");
|
||||
|
||||
assertEquals("Hello World", mutableString.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMutableList_whenAddElement_thenCorrectSize() {
|
||||
List<String> mutableList = new ArrayList<>();
|
||||
mutableList.add("Java");
|
||||
|
||||
assertEquals(1, mutableList.size());
|
||||
}
|
||||
}
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.infixpostfix;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class InfixToPostFixExpressionConversion {
|
||||
|
||||
private int getPrecedenceScore(char ch) {
|
||||
switch (ch) {
|
||||
case '^':
|
||||
return 3;
|
||||
|
||||
case '*':
|
||||
case '/':
|
||||
return 2;
|
||||
|
||||
case '+':
|
||||
case '-':
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean isOperand(char ch) {
|
||||
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
|
||||
}
|
||||
|
||||
private char associativity(char ch) {
|
||||
if (ch == '^')
|
||||
return 'R';
|
||||
return 'L';
|
||||
}
|
||||
|
||||
public String infixToPostfix(String infix) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
for (int i = 0; i < infix.length(); i++) {
|
||||
char ch = infix.charAt(i);
|
||||
|
||||
if (isOperand(ch)) {
|
||||
result.append(ch);
|
||||
} else if (ch == '(') {
|
||||
stack.push(ch);
|
||||
} else if (ch == ')') {
|
||||
while (!stack.isEmpty() && stack.peek() != '(') {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
stack.pop();
|
||||
} else {
|
||||
while (!stack.isEmpty() && (operatorPrecedenceCondition(infix, i, stack))) {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
stack.push(ch);
|
||||
}
|
||||
}
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private boolean operatorPrecedenceCondition(String infix, int i, Stack<Character> stack) {
|
||||
return getPrecedenceScore(infix.charAt(i)) < getPrecedenceScore(stack.peek()) || getPrecedenceScore(infix.charAt(i)) == getPrecedenceScore(stack.peek()) && associativity(infix.charAt(i)) == 'L';
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.infixpostfix;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
public class InfixToPostfixExpressionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleOp_whenNoParenthesis_thenProduceValidPostfix() {
|
||||
String infix = "a+b*c-d";
|
||||
String postfix = "abc*+d-";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSimpleOp_whenWithParenthesis_thenProduceValidPostfix() {
|
||||
String infix = "(a+b)*(c-d)";
|
||||
String postfix = "ab+cd-*";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComplexOp_whenInputIsInfix_thenProduceValidPostfix() {
|
||||
String infix = "a^b*(c^d-e)^(f+g*h)-i";
|
||||
String postfix = "ab^cd^e-fgh*+^*i-";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
public class CustomURLConnection extends URLConnection {
|
||||
private final String simulatedData = "This is the simulated data from the resource.";
|
||||
private URL url;
|
||||
private boolean connected = false;
|
||||
private String headerValue = "SimulatedHeaderValue";
|
||||
|
||||
protected CustomURLConnection(URL url) {
|
||||
super(url);
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws IOException {
|
||||
connected = true;
|
||||
System.out.println("Connection established to: " + url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
if (!connected) {
|
||||
connect();
|
||||
}
|
||||
return new ByteArrayInputStream(simulatedData.getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
ByteArrayOutputStream simulatedOutput = new ByteArrayOutputStream();
|
||||
return simulatedOutput;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getContentLength() {
|
||||
return simulatedData.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeaderField(String name) {
|
||||
if ("SimulatedHeader".equalsIgnoreCase(name)) { // Example header name
|
||||
return headerValue;
|
||||
} else {
|
||||
return null; // Header not found
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
|
||||
public class CustomURLStreamHandler extends URLStreamHandler {
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL u) throws IOException {
|
||||
// Create and return an instance of CustomURLConnection
|
||||
return new CustomURLConnection(u);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.URLStreamHandlerFactory;
|
||||
|
||||
public class CustomURLStreamHandlerFactory implements URLStreamHandlerFactory {
|
||||
|
||||
@Override
|
||||
public URLStreamHandler createURLStreamHandler(String protocol) {
|
||||
// Check if the requested protocol is our custom protocol
|
||||
if ("myprotocol".equals(protocol)) {
|
||||
return new CustomURLStreamHandler();
|
||||
}
|
||||
return null; // Let the default handler deal with other protocols
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
|
||||
|
||||
try {
|
||||
URL url = new URL("myprotocol://example.com/resource");
|
||||
|
||||
CustomURLConnection customConnection = (CustomURLConnection) url.openConnection();
|
||||
customConnection.connect();
|
||||
|
||||
InputStream inputStream = customConnection.getInputStream();
|
||||
|
||||
String content = new Scanner(inputStream).useDelimiter("\\A").next();
|
||||
System.out.println(content);
|
||||
|
||||
int contentLength = customConnection.getContentLength();
|
||||
System.out.println("Content Length: " + contentLength);
|
||||
|
||||
String headerValue = customConnection.getHeaderField("SimulatedHeader");
|
||||
System.out.println("Header Value: " + headerValue);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.urlencoder;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SpaceURLEncoderUnitTest {
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncoding_thenReturnPlusSign() {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString);
|
||||
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingUTF8Encoding_thenReturnPlusSign() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString, StandardCharsets.UTF_8.toString());
|
||||
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncodingAndReplace_thenReturnPct20() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString)
|
||||
.replace("+", "%20");
|
||||
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncodingAndReplaceAll_thenReturnPct20() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString)
|
||||
.replaceAll("\\+", "%20");
|
||||
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.securerandompositivelong;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SecureRandomPositiveLongUnitTest {
|
||||
|
||||
@Test
|
||||
void whenGenerateRandomPositiveLong_thenGetPositiveValue() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
long randomPositiveLong = Math.abs(secureRandom.nextLong());
|
||||
|
||||
assertThat(randomPositiveLong).isNotNegative();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
@@ -53,7 +53,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<hibernate.core.version>6.4.2.Final</hibernate.core.version>
|
||||
<json-path.version>5.3.2</json-path.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class HandleOptionalTypeExample {
|
||||
static Map<String, User> usersByName = new HashMap();
|
||||
static Map<String, User> usersByName = new HashMap<>();
|
||||
static {
|
||||
User user1 = new User();
|
||||
user1.setUserId(1l);
|
||||
|
||||
+3
-3
@@ -2,9 +2,9 @@ package com.baeldung.optionalreturntype;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
package com.baeldung.optionalreturntype;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample2 {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
package com.baeldung.optionalreturntype;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistUserExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@ package com.baeldung.optionalreturntype;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
|
||||
+3
-3
@@ -3,9 +3,9 @@ package com.baeldung.optionalreturntype;
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptional implements Serializable {
|
||||
|
||||
+2
-2
@@ -3,8 +3,8 @@ package com.baeldung.optionalreturntype;
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptionalField implements Serializable {
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.stream.range;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class StreamRangeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenRangeStreamUsingLimitSkip_thenPrintsRange() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
|
||||
|
||||
List<Integer> range = numbers.stream()
|
||||
.skip(2)
|
||||
.limit(5)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedRange, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRangeStreamUsingCollectingAndThen_thenPrintsRange() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
|
||||
|
||||
List<Integer> range = numbers.stream()
|
||||
.filter(n -> n >= 3 && n <= 7)
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
|
||||
|
||||
assertEquals(expectedRange, range);
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,7 @@
|
||||
<module>core-java-methods</module>
|
||||
<module>core-java-networking-3</module>
|
||||
<module>core-java-os</module>
|
||||
<module>core-java-os-2</module>
|
||||
<module>core-java-perf-2</module>
|
||||
<module>core-java-streams-4</module>
|
||||
<module>core-java-streams-5</module>
|
||||
|
||||
Reference in New Issue
Block a user