Merge remote-tracking branch 'upstream/master'
This commit is contained in:
		
						commit
						9e1d35c42d
					
				| @ -2,7 +2,7 @@ | |||||||
| The "REST with Spring" Classes | The "REST with Spring" Classes | ||||||
| ============================== | ============================== | ||||||
| 
 | 
 | ||||||
| After 5 months of work, here's the Master Class of REST With Spring: <br/> | Here's the Master Class of REST With Spring (price changes permanently next Friday): <br/> | ||||||
| **[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)** | **[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)** | ||||||
| 
 | 
 | ||||||
| And here's the Master Class of Learn Spring Security: <br/> | And here's the Master Class of Learn Spring Security: <br/> | ||||||
|  | |||||||
| @ -87,7 +87,7 @@ public class State { | |||||||
|     void randomPlay() { |     void randomPlay() { | ||||||
|         List<Position> availablePositions = this.board.getEmptyPositions(); |         List<Position> availablePositions = this.board.getEmptyPositions(); | ||||||
|         int totalPossibilities = availablePositions.size(); |         int totalPossibilities = availablePositions.size(); | ||||||
|         int selectRandom = (int) (Math.random() * ((totalPossibilities - 1) + 1)); |         int selectRandom = (int) (Math.random() * totalPossibilities); | ||||||
|         this.board.performMove(this.playerNo, availablePositions.get(selectRandom)); |         this.board.performMove(this.playerNo, availablePositions.get(selectRandom)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -65,7 +65,7 @@ public class Node { | |||||||
| 
 | 
 | ||||||
|     public Node getRandomChildNode() { |     public Node getRandomChildNode() { | ||||||
|         int noOfPossibleMoves = this.childArray.size(); |         int noOfPossibleMoves = this.childArray.size(); | ||||||
|         int selectRandom = (int) (Math.random() * ((noOfPossibleMoves - 1) + 1)); |         int selectRandom = (int) (Math.random() * noOfPossibleMoves); | ||||||
|         return this.childArray.get(selectRandom); |         return this.childArray.get(selectRandom); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,35 @@ | |||||||
|  | package com.baeldung.algorithms.string; | ||||||
|  | 
 | ||||||
|  | public class EnglishAlphabetLetters { | ||||||
|  | 
 | ||||||
|  |     public static boolean checkStringForAllTheLetters(String input) { | ||||||
|  |         boolean[] visited = new boolean[26]; | ||||||
|  | 
 | ||||||
|  |         int index = 0; | ||||||
|  | 
 | ||||||
|  |         for (int id = 0; id < input.length(); id++) { | ||||||
|  |             if ('a' <= input.charAt(id) && input.charAt(id) <= 'z') { | ||||||
|  |                 index = input.charAt(id) - 'a'; | ||||||
|  |             } else if ('A' <= input.charAt(id) && input.charAt(id) <= 'Z') { | ||||||
|  |                 index = input.charAt(id) - 'A'; | ||||||
|  |             } | ||||||
|  |             visited[index] = true; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         for (int id = 0; id < 26; id++) { | ||||||
|  |             if (!visited[id]) { | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static boolean checkStringForAllLetterUsingStream(String input) { | ||||||
|  |         long c = input.toLowerCase().chars().filter(ch -> ch >= 'a' && ch <= 'z').distinct().count(); | ||||||
|  |         return c == 26; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |         checkStringForAllLetterUsingStream("intit"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.algorithms.hillclimbing.HillClimbing; | import com.baeldung.algorithms.hillclimbing.HillClimbing; | ||||||
| import com.baeldung.algorithms.hillclimbing.State; | import com.baeldung.algorithms.hillclimbing.State; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup; | import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup; | ||||||
| import com.baeldung.algorithms.middleelementlookup.Node; | import com.baeldung.algorithms.middleelementlookup.Node; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.algorithms.automata.*; | import com.baeldung.algorithms.automata.*; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms; | package com.baeldung.algorithms; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms.binarysearch; | package com.baeldung.algorithms.binarysearch; | ||||||
| 
 | 
 | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms.mcts; | package com.baeldung.algorithms.mcts; | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||||
| import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package algorithms.minimax; | package com.baeldung.algorithms.minimax; | ||||||
| 
 | 
 | ||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -0,0 +1,20 @@ | |||||||
|  | package com.baeldung.algorithms.string; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.Assertions; | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | 
 | ||||||
|  | public class EnglishAlphabetLettersUnitTest { | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenString_whenContainsAllCharacter_thenTrue() { | ||||||
|  |         String input = "Farmer jack realized that big yellow quilts were expensive"; | ||||||
|  |         Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllTheLetters(input)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenString_whenContainsAllCharacter_thenUsingStreamExpectTrue() { | ||||||
|  |         String input = "Farmer jack realized that big yellow quilts were expensive"; | ||||||
|  |         Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllLetterUsingStream(input)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -5,29 +5,37 @@ import org.apache.avro.io.DatumReader; | |||||||
| import org.apache.avro.io.Decoder; | import org.apache.avro.io.Decoder; | ||||||
| import org.apache.avro.io.DecoderFactory; | import org.apache.avro.io.DecoderFactory; | ||||||
| import org.apache.avro.specific.SpecificDatumReader; | import org.apache.avro.specific.SpecificDatumReader; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| public class AvroDeSerealizer { | public class AvroDeSerealizer { | ||||||
| 
 | 
 | ||||||
|  |     private static Logger logger = LoggerFactory.getLogger(AvroDeSerealizer.class); | ||||||
|  | 
 | ||||||
|     public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data) { |     public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data) { | ||||||
|         DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class); |         DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class); | ||||||
|         Decoder decoder = null; |         Decoder decoder = null; | ||||||
|         try { |         try { | ||||||
|         decoder = DecoderFactory.get().jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data)); |             decoder = DecoderFactory.get() | ||||||
|  |                 .jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data)); | ||||||
|             return reader.read(null, decoder); |             return reader.read(null, decoder); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|         return null; |             logger.error("Deserialization error" + e.getMessage()); | ||||||
|         } |         } | ||||||
|  |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data) { |     public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data) { | ||||||
|         DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class); |         DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class); | ||||||
|     Decoder decoder = DecoderFactory.get().binaryDecoder(data, null); |         Decoder decoder = DecoderFactory.get() | ||||||
|  |             .binaryDecoder(data, null); | ||||||
|         try { |         try { | ||||||
|             return employeeReader.read(null, decoder); |             return employeeReader.read(null, decoder); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|  |             logger.error("Deserialization error" + e.getMessage()); | ||||||
|  |         } | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| } |  | ||||||
|  | |||||||
| @ -3,24 +3,29 @@ package com.baeldung.avro.util.serealization; | |||||||
| import com.baeldung.avro.util.model.AvroHttpRequest; | import com.baeldung.avro.util.model.AvroHttpRequest; | ||||||
| import org.apache.avro.io.*; | import org.apache.avro.io.*; | ||||||
| import org.apache.avro.specific.SpecificDatumWriter; | import org.apache.avro.specific.SpecificDatumWriter; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
| 
 | 
 | ||||||
| import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| public class AvroSerealizer { | public class AvroSerealizer { | ||||||
| 
 | 
 | ||||||
|  |     private static final Logger logger = LoggerFactory.getLogger(AvroSerealizer.class); | ||||||
|  | 
 | ||||||
|     public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request) { |     public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request) { | ||||||
|         DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); |         DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); | ||||||
|         byte[] data = new byte[0]; |         byte[] data = new byte[0]; | ||||||
|         ByteArrayOutputStream stream = new ByteArrayOutputStream(); |         ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||||||
|         Encoder jsonEncoder = null; |         Encoder jsonEncoder = null; | ||||||
|         try { |         try { | ||||||
|         jsonEncoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.getClassSchema(), stream); |             jsonEncoder = EncoderFactory.get() | ||||||
|  |                 .jsonEncoder(AvroHttpRequest.getClassSchema(), stream); | ||||||
|             writer.write(request, jsonEncoder); |             writer.write(request, jsonEncoder); | ||||||
|             jsonEncoder.flush(); |             jsonEncoder.flush(); | ||||||
|             data = stream.toByteArray(); |             data = stream.toByteArray(); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|         data =null; |             logger.error("Serialization error " + e.getMessage()); | ||||||
|         } |         } | ||||||
|         return data; |         return data; | ||||||
|     } |     } | ||||||
| @ -29,13 +34,14 @@ public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request){ | |||||||
|         DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); |         DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); | ||||||
|         byte[] data = new byte[0]; |         byte[] data = new byte[0]; | ||||||
|         ByteArrayOutputStream stream = new ByteArrayOutputStream(); |         ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||||||
|     Encoder jsonEncoder = EncoderFactory.get().binaryEncoder(stream,null); |         Encoder jsonEncoder = EncoderFactory.get() | ||||||
|  |             .binaryEncoder(stream, null); | ||||||
|         try { |         try { | ||||||
|             writer.write(request, jsonEncoder); |             writer.write(request, jsonEncoder); | ||||||
|             jsonEncoder.flush(); |             jsonEncoder.flush(); | ||||||
|             data = stream.toByteArray(); |             data = stream.toByteArray(); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|         data = null; |             logger.error("Serialization error " + e.getMessage()); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return data; |         return data; | ||||||
|  | |||||||
| @ -24,8 +24,10 @@ public class AvroSerealizerDeSerealizerTest { | |||||||
|         serealizer = new AvroSerealizer(); |         serealizer = new AvroSerealizer(); | ||||||
|         deSerealizer = new AvroDeSerealizer(); |         deSerealizer = new AvroDeSerealizer(); | ||||||
| 
 | 
 | ||||||
|         ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder(). |         ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder() | ||||||
|                 setHostName("localhost").setIpAddress("255.255.255.0").build(); |             .setHostName("localhost") | ||||||
|  |             .setIpAddress("255.255.255.0") | ||||||
|  |             .build(); | ||||||
| 
 | 
 | ||||||
|         List<CharSequence> employees = new ArrayList(); |         List<CharSequence> employees = new ArrayList(); | ||||||
|         employees.add("James"); |         employees.add("James"); | ||||||
| @ -33,9 +35,12 @@ public class AvroSerealizerDeSerealizerTest { | |||||||
|         employees.add("David"); |         employees.add("David"); | ||||||
|         employees.add("Han"); |         employees.add("Han"); | ||||||
| 
 | 
 | ||||||
|         request = AvroHttpRequest.newBuilder().setRequestTime(01l) |         request = AvroHttpRequest.newBuilder() | ||||||
|                 .setActive(Active.YES).setClientIdentifier(clientIdentifier) |             .setRequestTime(01l) | ||||||
|                 .setEmployeeNames(employees).build(); |             .setActive(Active.YES) | ||||||
|  |             .setClientIdentifier(clientIdentifier) | ||||||
|  |             .setEmployeeNames(employees) | ||||||
|  |             .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @After |     @After | ||||||
| @ -43,33 +48,36 @@ public class AvroSerealizerDeSerealizerTest { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| public void WhenSerialized_UsingJSONEncoder_ObjectGetsSerialized(){ |     public void WhenSerializedUsingJSONEncoder_thenObjectGetsSerialized() { | ||||||
|         byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); |         byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); | ||||||
|         assertTrue(Objects.nonNull(data)); |         assertTrue(Objects.nonNull(data)); | ||||||
|         assertTrue(data.length > 0); |         assertTrue(data.length > 0); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| public void WhenSerialized_UsingBinaryEncoder_ObjectGetsSerialized(){ |     public void WhenSerializedUsingBinaryEncoder_thenObjectGetsSerialized() { | ||||||
|         byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); |         byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); | ||||||
|         assertTrue(Objects.nonNull(data)); |         assertTrue(Objects.nonNull(data)); | ||||||
|         assertTrue(data.length > 0); |         assertTrue(data.length > 0); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| public void WhenDeserialize_UsingJSONDecoder_ActualAndExpectedObjectsAreEqual(){ |     public void WhenDeserializeUsingJSONDecoder_thenActualAndExpectedObjectsAreEqual() { | ||||||
|         byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); |         byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); | ||||||
|         AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data); |         AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data); | ||||||
|         assertEquals(actualRequest, request); |         assertEquals(actualRequest, request); | ||||||
|     assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); |         assertTrue(actualRequest.getRequestTime() | ||||||
|  |             .equals(request.getRequestTime())); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| public void WhenDeserialize_UsingBinaryecoder_ActualAndExpectedObjectsAreEqual(){ |     public void WhenDeserializeUsingBinaryecoder_thenActualAndExpectedObjectsAreEqual() { | ||||||
|         byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); |         byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); | ||||||
|         AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data); |         AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data); | ||||||
|         assertEquals(actualRequest, request); |         assertEquals(actualRequest, request); | ||||||
|     assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); |         assertTrue(actualRequest.getRequestTime() | ||||||
| } |             .equals(request.getRequestTime())); | ||||||
|  |     } | ||||||
|  |      | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| <configuration> | <configuration> | ||||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|         <encoder> |         <encoder> | ||||||
|             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|             </pattern> |             </pattern> | ||||||
|         </encoder> |         </encoder> | ||||||
|     </appender> |     </appender> | ||||||
|  | |||||||
| @ -39,6 +39,7 @@ | |||||||
|             <plugin> |             <plugin> | ||||||
|                 <groupId>org.codehaus.mojo</groupId> |                 <groupId>org.codehaus.mojo</groupId> | ||||||
|                 <artifactId>build-helper-maven-plugin</artifactId> |                 <artifactId>build-helper-maven-plugin</artifactId> | ||||||
|  |                 <version>${build-helper-maven-plugin.version}</version> | ||||||
|                 <executions> |                 <executions> | ||||||
|                     <execution> |                     <execution> | ||||||
|                         <phase>generate-sources</phase> |                         <phase>generate-sources</phase> | ||||||
| @ -60,6 +61,7 @@ | |||||||
|         <thrift.version>0.10.0</thrift.version> |         <thrift.version>0.10.0</thrift.version> | ||||||
|         <maven-thrift.version>0.1.11</maven-thrift.version> |         <maven-thrift.version>0.1.11</maven-thrift.version> | ||||||
|         <org.slf4j.slf4j-simple.version>1.7.12</org.slf4j.slf4j-simple.version> |         <org.slf4j.slf4j-simple.version>1.7.12</org.slf4j.slf4j-simple.version> | ||||||
|  |         <build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| <configuration> | <configuration> | ||||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|         <encoder> |         <encoder> | ||||||
|             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|             </pattern> |             </pattern> | ||||||
|         </encoder> |         </encoder> | ||||||
|     </appender> |     </appender> | ||||||
|  | |||||||
| @ -137,7 +137,7 @@ | |||||||
|         <aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version> |         <aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version> | ||||||
|         <gson.version>2.8.0</gson.version> |         <gson.version>2.8.0</gson.version> | ||||||
|         <aws-java-sdk.version>1.11.290</aws-java-sdk.version> |         <aws-java-sdk.version>1.11.290</aws-java-sdk.version> | ||||||
|         <mockito-core.version>2.8.9</mockito-core.version> |         <mockito-core.version>2.21.0</mockito-core.version> | ||||||
|         <assertj-core.version>3.8.0</assertj-core.version> |         <assertj-core.version>3.8.0</assertj-core.version> | ||||||
|         <dynamodblocal.version>1.11.86</dynamodblocal.version> |         <dynamodblocal.version>1.11.86</dynamodblocal.version> | ||||||
|         <dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url> |         <dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url> | ||||||
|  | |||||||
| @ -9,54 +9,30 @@ | |||||||
| - [Java 8 New Features](http://www.baeldung.com/java-8-new-features) | - [Java 8 New Features](http://www.baeldung.com/java-8-new-features) | ||||||
| - [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) | - [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) | ||||||
| - [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) | - [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) | ||||||
| - [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams) |  | ||||||
| - [Introduction to Java 8 Streams](http://www.baeldung.com/java-8-streams-introduction) |  | ||||||
| - [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector) | - [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector) | ||||||
| - [Strategy Design Pattern in Java 8](http://www.baeldung.com/java-strategy-pattern) | - [Strategy Design Pattern in Java 8](http://www.baeldung.com/java-strategy-pattern) | ||||||
| - [Java 8 and Infinite Streams](http://www.baeldung.com/java-inifinite-streams) |  | ||||||
| - [String Operations with Java Streams](http://www.baeldung.com/java-stream-operations-on-strings) |  | ||||||
| - [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions) | - [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions) | ||||||
| - [Java 8 Stream findFirst() vs. findAny()](http://www.baeldung.com/java-stream-findfirst-vs-findany) |  | ||||||
| - [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing) | - [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing) | ||||||
| - [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element) |  | ||||||
| - [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro) |  | ||||||
| - [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api) |  | ||||||
| - [Guide To Java 8 Optional](http://www.baeldung.com/java-optional) | - [Guide To Java 8 Optional](http://www.baeldung.com/java-optional) | ||||||
| - [Guide to the Java 8 forEach](http://www.baeldung.com/foreach-java) | - [Guide to the Java 8 forEach](http://www.baeldung.com/foreach-java) | ||||||
| - [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8) |  | ||||||
| - [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster) |  | ||||||
| - [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max) | - [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max) | ||||||
| - [Java Base64 Encoding and Decoding](http://www.baeldung.com/java-base64-encode-and-decode) | - [Java Base64 Encoding and Decoding](http://www.baeldung.com/java-base64-encode-and-decode) | ||||||
| - [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap) | - [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap) | ||||||
| - [Merging Streams in Java](http://www.baeldung.com/java-merge-streams) |  | ||||||
| - [“Stream has already been operated upon or closed” Exception in Java](http://www.baeldung.com/java-stream-operated-upon-or-closed-exception) |  | ||||||
| - [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones) |  | ||||||
| - [Copy a File with Java](http://www.baeldung.com/java-copy-file) | - [Copy a File with Java](http://www.baeldung.com/java-copy-file) | ||||||
| - [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods) | - [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods) | ||||||
| - [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) |  | ||||||
| - [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream) |  | ||||||
| - [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) |  | ||||||
| - [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency) | - [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency) | ||||||
| - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) |  | ||||||
| - [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) | - [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) | ||||||
| - [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection) | - [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection) | ||||||
| - [Java 8 StringJoiner](http://www.baeldung.com/java-string-joiner) |  | ||||||
| - [Introduction to Spliterator in Java](http://www.baeldung.com/java-spliterator) | - [Introduction to Spliterator in Java](http://www.baeldung.com/java-spliterator) | ||||||
| - [Java 8 Math New Methods](http://www.baeldung.com/java-8-math) | - [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) | - [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) | - [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) | - [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization) | ||||||
| - [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java) | - [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) |  | ||||||
| - [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get) | - [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get) | ||||||
| - [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table) | - [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table) | ||||||
| - [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) | - [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) | ||||||
| - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) | - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) | ||||||
| - [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end) |  | ||||||
| - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) | - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) | ||||||
| - [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string) |  | ||||||
| - [Calculate Age in Java](http://www.baeldung.com/java-get-age) |  | ||||||
| - [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) | - [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) | ||||||
| - [Increment Date in Java](http://www.baeldung.com/java-increment-date) |  | ||||||
| - [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date) |  | ||||||
| - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) | - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) | ||||||
|  | |||||||
| @ -89,11 +89,6 @@ | |||||||
|             <artifactId>vavr</artifactId> |             <artifactId>vavr</artifactId> | ||||||
|             <version>${vavr.version}</version> |             <version>${vavr.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |  | ||||||
|             <groupId>one.util</groupId> |  | ||||||
|             <artifactId>streamex</artifactId> |  | ||||||
|             <version>${streamex.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>joda-time</groupId> |             <groupId>joda-time</groupId> | ||||||
|             <artifactId>joda-time</artifactId> |             <artifactId>joda-time</artifactId> | ||||||
| @ -151,6 +146,7 @@ | |||||||
|             <plugin> |             <plugin> | ||||||
|                 <groupId>org.springframework.boot</groupId> |                 <groupId>org.springframework.boot</groupId> | ||||||
|                 <artifactId>spring-boot-maven-plugin</artifactId> |                 <artifactId>spring-boot-maven-plugin</artifactId> | ||||||
|  |                 <version>${spring-boot-maven-plugin.version}</version> | ||||||
|                 <executions> |                 <executions> | ||||||
|                     <execution> |                     <execution> | ||||||
|                         <goals> |                         <goals> | ||||||
| @ -176,7 +172,6 @@ | |||||||
|         <lombok.version>1.16.12</lombok.version> |         <lombok.version>1.16.12</lombok.version> | ||||||
|         <vavr.version>0.9.0</vavr.version> |         <vavr.version>0.9.0</vavr.version> | ||||||
|         <protonpack.version>1.13</protonpack.version> |         <protonpack.version>1.13</protonpack.version> | ||||||
|         <streamex.version>0.6.5</streamex.version> |  | ||||||
|         <joda.version>2.10</joda.version> |         <joda.version>2.10</joda.version> | ||||||
|         <!-- testing --> |         <!-- testing --> | ||||||
|         <assertj.version>3.6.1</assertj.version> |         <assertj.version>3.6.1</assertj.version> | ||||||
| @ -184,6 +179,6 @@ | |||||||
|         <avaitility.version>1.7.0</avaitility.version> |         <avaitility.version>1.7.0</avaitility.version> | ||||||
|         <jmh-core.version>1.19</jmh-core.version> |         <jmh-core.version>1.19</jmh-core.version> | ||||||
|         <jmh-generator.version>1.19</jmh-generator.version> |         <jmh-generator.version>1.19</jmh-generator.version> | ||||||
|  |         <spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 |  | ||||||
| </project> | </project> | ||||||
| @ -0,0 +1,9 @@ | |||||||
|  | package com.baeldung.optional; | ||||||
|  | 
 | ||||||
|  | public class PersonRepository { | ||||||
|  | 
 | ||||||
|  |     public String findNameById(String id) { | ||||||
|  |         return id == null ? null : "Name"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,23 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | public class BenchmarkRunner { | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) throws Exception { | ||||||
|  |         new IntPrimitiveLookup().run(); | ||||||
|  |         new IntegerWrapperLookup().run(); | ||||||
|  |         new FloatPrimitiveLookup().run(); | ||||||
|  |         new FloatWrapperLookup().run(); | ||||||
|  |         new DoublePrimitiveLookup().run(); | ||||||
|  |         new DoubleWrapperLookup().run(); | ||||||
|  |         new ShortPrimitiveLookup().run(); | ||||||
|  |         new ShortWrapperLookup().run(); | ||||||
|  |         new BooleanPrimitiveLookup().run(); | ||||||
|  |         new BooleanWrapperLookup().run(); | ||||||
|  |         new CharPrimitiveLookup().run(); | ||||||
|  |         new CharacterWrapperLookup().run(); | ||||||
|  |         new BytePrimitiveLookup().run(); | ||||||
|  |         new ByteWrapperLookup().run(); | ||||||
|  |         new LongPrimitiveLookup().run(); | ||||||
|  |         new LongWrapperLookup().run(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class BooleanPrimitiveLookup extends Lookup { | ||||||
|  | 
 | ||||||
|  |     private boolean[] elements; | ||||||
|  |     private final boolean pivot = false; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         elements = new boolean[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = true; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return BooleanPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class BooleanWrapperLookup extends Lookup { | ||||||
|  |     private Boolean[] elements; | ||||||
|  |     private final boolean pivot = false; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         elements = new Boolean[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = true; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Boolean pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return BooleanWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class BytePrimitiveLookup extends Lookup { | ||||||
|  | 
 | ||||||
|  |     private byte[] elements; | ||||||
|  |     private final byte pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         byte common = 1; | ||||||
|  |         elements = new byte[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return BytePrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class ByteWrapperLookup extends Lookup { | ||||||
|  |     private Byte[] elements; | ||||||
|  |     private final byte pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         byte common = 1; | ||||||
|  |         elements = new Byte[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Byte pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return ByteWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class CharPrimitiveLookup extends Lookup { | ||||||
|  | 
 | ||||||
|  |     private char[] elements; | ||||||
|  |     private final char pivot = 'b'; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         char common = 'a'; | ||||||
|  |         elements = new char[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return CharPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class CharacterWrapperLookup extends Lookup { | ||||||
|  |     private Character[] elements; | ||||||
|  |     private final char pivot = 'b'; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         char common = 'a'; | ||||||
|  |         elements = new Character[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Character pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return CharacterWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,42 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class DoublePrimitiveLookup extends Lookup { | ||||||
|  |     private double[] elements; | ||||||
|  |     private final double pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         double common = 1; | ||||||
|  |         elements = new double[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return DoublePrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class DoubleWrapperLookup extends Lookup { | ||||||
|  |     private Double[] elements; | ||||||
|  |     private final double pivot = 2d; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         double common = 1; | ||||||
|  |         elements = new Double[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Double pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return DoubleWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,42 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class FloatPrimitiveLookup extends Lookup { | ||||||
|  |     private float[] elements; | ||||||
|  |     private final float pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         int common = 1; | ||||||
|  |         elements = new float[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return FloatPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,43 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class FloatWrapperLookup extends Lookup { | ||||||
|  |     private Float[] elements; | ||||||
|  |     private final float pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         float common = 1; | ||||||
|  |         elements = new Float[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Float pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return FloatWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class IntPrimitiveLookup extends Lookup { | ||||||
|  | 
 | ||||||
|  |     private int[] elements; | ||||||
|  |     private final int pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         int common = 1; | ||||||
|  |         elements = new int[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return IntPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class IntegerWrapperLookup extends Lookup { | ||||||
|  |     private Integer[] elements; | ||||||
|  |     private final int pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         int common = 1; | ||||||
|  |         elements = new Integer[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Integer pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return IntegerWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,42 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class LongPrimitiveLookup extends Lookup { | ||||||
|  |     private long[] elements; | ||||||
|  |     private final long pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         long common = 1; | ||||||
|  |         elements = new long[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return LongPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class LongWrapperLookup extends Lookup{ | ||||||
|  |     private Long[] elements; | ||||||
|  |     private final long pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         long common = 1; | ||||||
|  |         elements = new Long[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Long pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return LongWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										56
									
								
								core-java-8/src/main/java/com/baeldung/primitive/Lookup.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								core-java-8/src/main/java/com/baeldung/primitive/Lookup.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,56 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.results.RunResult; | ||||||
|  | import org.openjdk.jmh.runner.Runner; | ||||||
|  | import org.openjdk.jmh.runner.RunnerException; | ||||||
|  | import org.openjdk.jmh.runner.options.Options; | ||||||
|  | import org.openjdk.jmh.runner.options.OptionsBuilder; | ||||||
|  | 
 | ||||||
|  | import java.util.Collection; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * An abstract class that is to be extended by the classes that | ||||||
|  |  * perform lookup in the arrays either of Java primitive types or their wrappers. | ||||||
|  |  */ | ||||||
|  | public abstract class Lookup { | ||||||
|  |     /** | ||||||
|  |      * the array size | ||||||
|  |      */ | ||||||
|  |     final protected int s = 50000000; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Initialize the array: fill in the array with the same | ||||||
|  |      * elements except for the last one. | ||||||
|  |      */ | ||||||
|  |     abstract public void prepare(); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Free the array's reference. | ||||||
|  |      */ | ||||||
|  |     abstract public void clean(); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Find the position of the element that is different from the others. | ||||||
|  |      * By construction, it is the last array element. | ||||||
|  |      * | ||||||
|  |      * @return array's last element index | ||||||
|  |      */ | ||||||
|  |     abstract public int findPosition(); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Get the name of the class that extends this one. It is needed in order | ||||||
|  |      * to set up the benchmark. | ||||||
|  |      * | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     abstract public String getSimpleClassName(); | ||||||
|  | 
 | ||||||
|  |     Collection<RunResult> run() throws RunnerException { | ||||||
|  |         Options opt = new OptionsBuilder() | ||||||
|  |                 .include(getSimpleClassName()) | ||||||
|  |                 .forks(1) | ||||||
|  |                 .build(); | ||||||
|  |         return new Runner(opt).run(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class ShortPrimitiveLookup extends Lookup { | ||||||
|  | 
 | ||||||
|  |     private short[] elements; | ||||||
|  |     private final short pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Setup | ||||||
|  |     @Override | ||||||
|  |     public void prepare() { | ||||||
|  |         short common = 1; | ||||||
|  |         elements = new short[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @TearDown | ||||||
|  |     @Override | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         while (pivot != elements[index]) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return ShortPrimitiveLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.primitive; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | 
 | ||||||
|  | @State(Scope.Thread) | ||||||
|  | public class ShortWrapperLookup extends Lookup { | ||||||
|  |     private Short[] elements; | ||||||
|  |     private final short pivot = 2; | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Setup | ||||||
|  |     public void prepare() { | ||||||
|  |         short common = 1; | ||||||
|  |         elements = new Short[s]; | ||||||
|  |         for (int i = 0; i < s - 1; i++) { | ||||||
|  |             elements[i] = common; | ||||||
|  |         } | ||||||
|  |         elements[s - 1] = pivot; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @TearDown | ||||||
|  |     public void clean() { | ||||||
|  |         elements = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     @Benchmark | ||||||
|  |     @BenchmarkMode(Mode.AverageTime) | ||||||
|  |     public int findPosition() { | ||||||
|  |         int index = 0; | ||||||
|  |         Short pivotWrapper = pivot; | ||||||
|  |         while (!pivotWrapper.equals(elements[index])) { | ||||||
|  |             index++; | ||||||
|  |         } | ||||||
|  |         return index; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getSimpleClassName() { | ||||||
|  |         return ShortWrapperLookup.class.getSimpleName(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,43 @@ | |||||||
|  | package com.baeldung.optional; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import java.util.Optional; | ||||||
|  | 
 | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertAll; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||
|  | 
 | ||||||
|  | public class PersonRepositoryUnitTest { | ||||||
|  | 
 | ||||||
|  |     PersonRepository personRepository = new PersonRepository(); | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenIdIsNull_thenExceptionIsThrown() { | ||||||
|  |         assertThrows(IllegalArgumentException.class, | ||||||
|  |                 () -> | ||||||
|  |                         Optional | ||||||
|  |                                 .ofNullable(personRepository.findNameById(null)) | ||||||
|  |                                 .orElseThrow(IllegalArgumentException::new)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenIdIsNonNull_thenNoExceptionIsThrown() { | ||||||
|  |         assertAll( | ||||||
|  |                 () -> | ||||||
|  |                         Optional | ||||||
|  |                                 .ofNullable(personRepository.findNameById("id")) | ||||||
|  |                                 .orElseThrow(RuntimeException::new)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenIdNonNull_thenReturnsNameUpperCase() { | ||||||
|  |         String name = Optional | ||||||
|  |                 .ofNullable(personRepository.findNameById("id")) | ||||||
|  |                 .map(String::toUpperCase) | ||||||
|  |                 .orElseThrow(RuntimeException::new); | ||||||
|  | 
 | ||||||
|  |         assertEquals("NAME", name); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,97 @@ | |||||||
|  | package com.baeldung.streamordering; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | import org.openjdk.jmh.infra.Blackhole; | ||||||
|  | import org.openjdk.jmh.runner.Runner; | ||||||
|  | import org.openjdk.jmh.runner.options.Options; | ||||||
|  | import org.openjdk.jmh.runner.options.OptionsBuilder; | ||||||
|  | import org.openjdk.jmh.runner.options.TimeValue; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Random; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | import java.util.stream.IntStream; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | public class BenchmarkUnitTest | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void | ||||||
|  |   launchBenchmark() throws Exception { | ||||||
|  | 
 | ||||||
|  |     Options opt = new OptionsBuilder() | ||||||
|  |       // Specify which benchmarks to run. | ||||||
|  |       // You can be more specific if you'd like to run only one benchmark per test. | ||||||
|  |       .include(this.getClass().getName() + ".*") | ||||||
|  |       // Set the following options as needed | ||||||
|  |       .mode (Mode.AverageTime) | ||||||
|  |       .timeUnit(TimeUnit.MICROSECONDS) | ||||||
|  |       .warmupTime(TimeValue.seconds(1)) | ||||||
|  |       .warmupIterations(2) | ||||||
|  |       .measurementTime(TimeValue.seconds(1)) | ||||||
|  |       .measurementIterations(2) | ||||||
|  |       .threads(2) | ||||||
|  |       .forks(1) | ||||||
|  |       .shouldFailOnError(true) | ||||||
|  |       .shouldDoGC(true) | ||||||
|  |       //.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining") | ||||||
|  |       //.addProfiler(WinPerfAsmProfiler.class) | ||||||
|  |       .build(); | ||||||
|  | 
 | ||||||
|  |     new Runner(opt).run(); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Benchmark | ||||||
|  |   public void givenOrderedStreamInput_whenStreamFiltered_showOpsPerMS(){ | ||||||
|  |     IntStream.range(1, 100_000_000).parallel().filter(i -> i % 10 == 0).toArray(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Benchmark | ||||||
|  |   public void givenUnorderedStreamInput_whenStreamFiltered_showOpsPerMS(){ | ||||||
|  |     IntStream.range(1,100_000_000).unordered().parallel().filter(i -> i % 10 == 0).toArray(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Benchmark | ||||||
|  |   public void givenUnorderedStreamInput_whenStreamDistinct_showOpsPerMS(){ | ||||||
|  |     IntStream.range(1, 1_000_000).unordered().parallel().distinct().toArray(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Benchmark | ||||||
|  |   public void givenOrderedStreamInput_whenStreamDistinct_showOpsPerMS() { | ||||||
|  |     //section 5.1. | ||||||
|  |     IntStream.range(1, 1_000_000).parallel().distinct().toArray(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   // The JMH samples are the best documentation for how to use it | ||||||
|  |   // http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/ | ||||||
|  |   @State(Scope.Thread) | ||||||
|  |   public static class BenchmarkState | ||||||
|  |   { | ||||||
|  |     List<Integer> list; | ||||||
|  | 
 | ||||||
|  |     @Setup(Level.Trial) public void | ||||||
|  |     initialize() { | ||||||
|  | 
 | ||||||
|  |       Random rand = new Random(); | ||||||
|  | 
 | ||||||
|  |       list = new ArrayList<>(); | ||||||
|  |       for (int i = 0; i < 1000; i++) | ||||||
|  |         list.add (rand.nextInt()); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Benchmark public void | ||||||
|  |   benchmark1 (BenchmarkState state, Blackhole bh) { | ||||||
|  | 
 | ||||||
|  |     List<Integer> list = state.list; | ||||||
|  | 
 | ||||||
|  |     for (int i = 0; i < 1000; i++) | ||||||
|  |       bh.consume (list.get (i)); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,149 @@ | |||||||
|  | package com.baeldung.streamordering; | ||||||
|  | 
 | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import java.util.*; | ||||||
|  | import java.util.function.Function; | ||||||
|  | import java.util.logging.Level; | ||||||
|  | import java.util.logging.Logger; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  | import java.util.stream.IntStream; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | public class StreamsOrderingUnitTest { | ||||||
|  | 
 | ||||||
|  |   Logger logger = Logger.getLogger( StreamsOrderingUnitTest.class.getName()); | ||||||
|  | 
 | ||||||
|  |   @Before | ||||||
|  |   public void setUp() throws Exception { | ||||||
|  |     logger.setLevel(Level.ALL); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenTwoCollections_whenStreamed_thenCheckOutputDifferent(){ | ||||||
|  | 
 | ||||||
|  |     List<String> list = Arrays.asList("B", "A", "C", "D", "F"); | ||||||
|  |     Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); | ||||||
|  | 
 | ||||||
|  |     Object[] listOutput = list.stream().toArray(); | ||||||
|  |     Object[] setOutput = set.stream().toArray(); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); | ||||||
|  |     assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenTwoCollections_whenStreamedInParallel_thenCheckOutputDifferent(){ | ||||||
|  | 
 | ||||||
|  |     List<String> list = Arrays.asList("B", "A", "C", "D", "F"); | ||||||
|  |     Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); | ||||||
|  | 
 | ||||||
|  |     Object[] listOutput = list.stream().parallel().toArray(); | ||||||
|  |     Object[] setOutput = set.stream().parallel().toArray(); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); | ||||||
|  |     assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenOrderedInput_whenUnorderedAndOrderedCompared_thenCheckUnorderedOutputChanges(){ | ||||||
|  |     Set<Integer> set = new TreeSet<>( | ||||||
|  |       Arrays.asList(-9, -5, -4, -2, 1, 2, 4, 5, 7, 9, 12, 13, 16, 29, 23, 34, 57, 68, 90, 102, 230)); | ||||||
|  | 
 | ||||||
|  |       Object[] orderedArray = set.stream() | ||||||
|  |         .parallel() | ||||||
|  |         .limit(5) | ||||||
|  |         .toArray(); | ||||||
|  |       Object[] unorderedArray = set.stream() | ||||||
|  |         .unordered() | ||||||
|  |         .parallel() | ||||||
|  |         .limit(5) | ||||||
|  |         .toArray(); | ||||||
|  | 
 | ||||||
|  |      logger.info(Arrays.toString(orderedArray)); | ||||||
|  |      logger.info(Arrays.toString(unorderedArray)); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenUnsortedStreamInput_whenStreamSorted_thenCheckOrderChanged(){ | ||||||
|  | 
 | ||||||
|  |     List<Integer> list = Arrays.asList(-3,10,-4,1,3); | ||||||
|  | 
 | ||||||
|  |     Object[] listOutput = list.stream().toArray(); | ||||||
|  |     Object[] listOutputSorted = list.stream().sorted().toArray(); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[-3, 10, -4, 1, 3]", Arrays.toString(listOutput)); | ||||||
|  |     assertEquals("[-4, -3, 1, 3, 10]", Arrays.toString(listOutputSorted)); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenUnsortedStreamInput_whenStreamDistinct_thenShowTimeTaken(){ | ||||||
|  |     long start, end; | ||||||
|  |     start = System.currentTimeMillis(); | ||||||
|  |     IntStream.range(1,1_000_000).unordered().parallel().distinct().toArray(); | ||||||
|  |     end = System.currentTimeMillis(); | ||||||
|  |     System.out.println(String.format("Time taken when unordered: %d ms", (end - start))); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenSameCollection_whenStreamTerminated_thenCheckEachVsEachOrdered(){ | ||||||
|  | 
 | ||||||
|  |     List<String> list = Arrays.asList("B", "A", "C", "D", "F"); | ||||||
|  | 
 | ||||||
|  |     list.stream().parallel().forEach(e -> logger.log(Level.INFO, e)); | ||||||
|  |     list.stream().parallel().forEachOrdered(e -> logger.log(Level.INFO, e)); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenSameCollection_whenStreamCollected_thenCheckOutput(){ | ||||||
|  | 
 | ||||||
|  |     List<String> list = Arrays.asList("B", "A", "C", "D", "F"); | ||||||
|  | 
 | ||||||
|  |     List<String> collectionList = list.stream().parallel().collect(Collectors.toList()); | ||||||
|  |     Set<String> collectionSet = list.stream().parallel().collect(Collectors.toCollection(TreeSet::new)); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[B, A, C, D, F]", collectionList.toString()); | ||||||
|  |     assertEquals("[A, B, C, D, F]", collectionSet.toString()); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenListIterationOrder_whenStreamCollectedToMap_thenCeckOrderChanged() { | ||||||
|  |     List<String> list = Arrays.asList("A", "BB", "CCC"); | ||||||
|  | 
 | ||||||
|  |     Map<String, Integer> hashMap = list.stream().collect(Collectors.toMap(Function.identity(), String::length)); | ||||||
|  | 
 | ||||||
|  |     Object[] keySet = hashMap.keySet().toArray(); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[BB, A, CCC]", Arrays.toString(keySet)); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Test | ||||||
|  |   public void givenListIteration_whenStreamCollectedtoHashMap_thenCheckOrderMaintained() { | ||||||
|  |     List<String> list = Arrays.asList("A", "BB", "CCC", "CCC"); | ||||||
|  | 
 | ||||||
|  |     Map<String, Integer> linkedHashMap = list.stream().collect(Collectors.toMap( | ||||||
|  |       Function.identity(), | ||||||
|  |       String::length, | ||||||
|  |       (u, v) -> u, | ||||||
|  |       LinkedHashMap::new | ||||||
|  |     )); | ||||||
|  | 
 | ||||||
|  |     Object[] keySet = linkedHashMap.keySet().toArray(); | ||||||
|  | 
 | ||||||
|  |     assertEquals("[A, BB, CCC]", Arrays.toString(keySet)); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -15,10 +15,7 @@ | |||||||
| - [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity) | - [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity) | ||||||
| - [Java 9 Optional API Additions](http://www.baeldung.com/java-9-optional) | - [Java 9 Optional API Additions](http://www.baeldung.com/java-9-optional) | ||||||
| - [Java 9 Reactive Streams](http://www.baeldung.com/java-9-reactive-streams) | - [Java 9 Reactive Streams](http://www.baeldung.com/java-9-reactive-streams) | ||||||
| - [How to Get All Dates Between Two Dates?](http://www.baeldung.com/java-between-dates) |  | ||||||
| - [Java 9 java.util.Objects Additions](http://www.baeldung.com/java-9-objects-new) | - [Java 9 java.util.Objects Additions](http://www.baeldung.com/java-9-objects-new) | ||||||
| - [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string) |  | ||||||
| - [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime) |  | ||||||
| - [Java 9 Variable Handles Demistyfied](http://www.baeldung.com/java-variable-handles) | - [Java 9 Variable Handles Demistyfied](http://www.baeldung.com/java-variable-handles) | ||||||
| - [Exploring the New HTTP Client in Java 9](http://www.baeldung.com/java-9-http-client) | - [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) | - [Method Handles in Java](http://www.baeldung.com/java-method-handles) | ||||||
| @ -26,3 +23,4 @@ | |||||||
| - [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity) | - [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity) | ||||||
| - [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional) | - [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional) | ||||||
| - [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api) | - [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api) | ||||||
|  | - [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range) | ||||||
|  | |||||||
| @ -0,0 +1,24 @@ | |||||||
|  | package com.baeldung.java9.rangedates; | ||||||
|  | 
 | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.Date; | ||||||
|  | 
 | ||||||
|  | public class DatesCollectionIteration { | ||||||
|  | 
 | ||||||
|  |     public void iteratingRangeOfDatesJava7(Collection<Date> dates) { | ||||||
|  | 
 | ||||||
|  |         for (Date date : dates) { | ||||||
|  |             processDate(date); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void iteratingRangeOfDatesJava8(Collection<Date> dates) { | ||||||
|  |         dates.stream() | ||||||
|  |             .forEach(this::processDate); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void processDate(Date date) { | ||||||
|  |         System.out.println(date); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,43 @@ | |||||||
|  | package com.baeldung.java9.rangedates; | ||||||
|  | 
 | ||||||
|  | import java.time.LocalDate; | ||||||
|  | import java.util.Calendar; | ||||||
|  | import java.util.Date; | ||||||
|  | 
 | ||||||
|  | public class RangeDatesIteration { | ||||||
|  | 
 | ||||||
|  |     public void iterateBetweenDatesJava9(LocalDate startDate, LocalDate endDate) { | ||||||
|  | 
 | ||||||
|  |         startDate.datesUntil(endDate) | ||||||
|  |             .forEach(this::processDate); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void iterateBetweenDatesJava8(LocalDate start, LocalDate end) { | ||||||
|  |         for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) { | ||||||
|  |             processDate(date); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void iterateBetweenDatesJava7(Date start, Date end) { | ||||||
|  |         Date current = start; | ||||||
|  | 
 | ||||||
|  |         while (current.before(end)) { | ||||||
|  |             processDate(current); | ||||||
|  | 
 | ||||||
|  |             Calendar calendar = Calendar.getInstance(); | ||||||
|  |             calendar.setTime(current); | ||||||
|  | 
 | ||||||
|  |             calendar.add(Calendar.DATE, 1); | ||||||
|  | 
 | ||||||
|  |             current = calendar.getTime(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void processDate(LocalDate date) { | ||||||
|  |         System.out.println(date); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void processDate(Date date) { | ||||||
|  |         System.out.println(date); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -2,7 +2,7 @@ | |||||||
| <configuration> | <configuration> | ||||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|         <encoder> |         <encoder> | ||||||
|             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|             </pattern> |             </pattern> | ||||||
|         </encoder> |         </encoder> | ||||||
|     </appender> |     </appender> | ||||||
|  | |||||||
| @ -0,0 +1,61 @@ | |||||||
|  | package com.baeldung.java9.rangedates; | ||||||
|  | 
 | ||||||
|  | import java.time.LocalDate; | ||||||
|  | import java.time.ZoneId; | ||||||
|  | import java.time.temporal.ChronoUnit; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Calendar; | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.Date; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class DatesCollectionIterationUnitTest { | ||||||
|  | 
 | ||||||
|  |     private Collection<LocalDate> localDates = LocalDate.now() | ||||||
|  |         .datesUntil(LocalDate.now() | ||||||
|  |             .plus(10L, ChronoUnit.DAYS)) | ||||||
|  |         .collect(Collectors.toList()); | ||||||
|  | 
 | ||||||
|  |     private Collection<Date> dates = localDates.stream() | ||||||
|  |         .map(localDate -> Date.from(localDate.atStartOfDay(ZoneId.systemDefault()) | ||||||
|  |             .toInstant())) | ||||||
|  |         .collect(Collectors.toList()); | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenIteratingListOfDatesJava7_WhenStartTodayAndEnding10DaysAhead() { | ||||||
|  |         DatesCollectionIteration iterateInColleciton = new DatesCollectionIteration(); | ||||||
|  |         Calendar today = Calendar.getInstance(); | ||||||
|  |         Calendar next10Ahead = (Calendar) today.clone(); | ||||||
|  |         next10Ahead.add(Calendar.DATE, 10); | ||||||
|  | 
 | ||||||
|  |         iterateInColleciton.iteratingRangeOfDatesJava7(createRangeDates(today.getTime(), next10Ahead.getTime())); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenIteratingListOfDatesJava8_WhenStartTodayAndEnd10DaysAhead() { | ||||||
|  |         DatesCollectionIteration iterateInColleciton = new DatesCollectionIteration(); | ||||||
|  | 
 | ||||||
|  |         iterateInColleciton.iteratingRangeOfDatesJava8(dates); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private List<Date> createRangeDates(Date start, Date end) { | ||||||
|  | 
 | ||||||
|  |         List<Date> dates = new ArrayList<>(); | ||||||
|  |         Date current = start; | ||||||
|  | 
 | ||||||
|  |         while (current.before(end)) { | ||||||
|  |             dates.add(current); | ||||||
|  |              | ||||||
|  |             Calendar calendar = Calendar.getInstance(); | ||||||
|  |             calendar.setTime(current); | ||||||
|  |             calendar.add(Calendar.DATE, 1); | ||||||
|  | 
 | ||||||
|  |             current = calendar.getTime(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return dates; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,48 @@ | |||||||
|  | package com.baeldung.java9.rangedates; | ||||||
|  | 
 | ||||||
|  | import java.time.LocalDate; | ||||||
|  | import java.time.temporal.ChronoUnit; | ||||||
|  | import java.util.Calendar; | ||||||
|  | import java.util.Date; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class RangeDatesIterationUnitTest { | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenIterateBetweenDatesJava9_WhenStartDateAsTodayAndEndDateAs10DaysAhead() { | ||||||
|  |         LocalDate start = LocalDate.now(); | ||||||
|  |         LocalDate end = start.plus(10L, ChronoUnit.DAYS); | ||||||
|  | 
 | ||||||
|  |         RangeDatesIteration iteration = new RangeDatesIteration(); | ||||||
|  | 
 | ||||||
|  |         iteration.iterateBetweenDatesJava9(start, end); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenIterateBetweenDatesJava8_WhenStartDateAsTodayAndEndDateAs10DaysAhead() { | ||||||
|  |         LocalDate start = LocalDate.now(); | ||||||
|  |         LocalDate end = start.plus(10L, ChronoUnit.DAYS); | ||||||
|  | 
 | ||||||
|  |         RangeDatesIteration iteration = new RangeDatesIteration(); | ||||||
|  | 
 | ||||||
|  |         iteration.iterateBetweenDatesJava8(start, end); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenIterateBetweenDatesJava7_WhenStartDateAsTodayAndEndDateAs10DaysAhead() { | ||||||
|  |         Calendar today = Calendar.getInstance(); | ||||||
|  |         Calendar calendar = Calendar.getInstance(); | ||||||
|  |         calendar.clear(); | ||||||
|  |         calendar.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE)); | ||||||
|  |         Date start = calendar.getTime(); | ||||||
|  | 
 | ||||||
|  |         calendar.add(Calendar.DATE, 10); | ||||||
|  |         Date end = calendar.getTime(); | ||||||
|  | 
 | ||||||
|  |         RangeDatesIteration iteration = new RangeDatesIteration(); | ||||||
|  | 
 | ||||||
|  |         iteration.iterateBetweenDatesJava7(start, end); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -36,3 +36,4 @@ | |||||||
| - [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list) | - [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list) | ||||||
| - [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map) | - [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map) | ||||||
| - [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset) | - [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset) | ||||||
|  | - [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element) | ||||||
|  | |||||||
| @ -53,15 +53,27 @@ | |||||||
|             <version>${junit.platform.version}</version> |             <version>${junit.platform.version}</version> | ||||||
|             <scope>test</scope> |             <scope>test</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.openjdk.jmh</groupId> | ||||||
|  |             <artifactId>jmh-core</artifactId> | ||||||
|  |             <version>${openjdk.jmh.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.openjdk.jmh</groupId> | ||||||
|  |             <artifactId>jmh-generator-annprocess</artifactId> | ||||||
|  |             <version>${openjdk.jmh.version}</version> | ||||||
|  |         </dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     <properties> |     <properties> | ||||||
|  |         <openjdk.jmh.version>1.19</openjdk.jmh.version> | ||||||
|         <junit.platform.version>1.2.0</junit.platform.version> |         <junit.platform.version>1.2.0</junit.platform.version> | ||||||
|         <commons-lang3.version>3.5</commons-lang3.version> |         <commons-lang3.version>3.5</commons-lang3.version> | ||||||
|         <commons-collections4.version>4.1</commons-collections4.version> |         <commons-collections4.version>4.1</commons-collections4.version> | ||||||
|         <collections-generic.version>4.01</collections-generic.version> |         <collections-generic.version>4.01</collections-generic.version> | ||||||
|         <avaitility.version>1.7.0</avaitility.version> |         <avaitility.version>1.7.0</avaitility.version> | ||||||
|         <assertj.version>3.6.1</assertj.version> |         <assertj.version>3.6.1</assertj.version> | ||||||
|         <eclipse.collections.version>9.2.0</eclipse.collections.version> |         <eclipse.collections.version>7.1.0</eclipse.collections.version> | ||||||
|     </properties> |     </properties> | ||||||
| </project> | </project> | ||||||
|  | |||||||
| @ -0,0 +1,59 @@ | |||||||
|  | package com.baeldung.performance; | ||||||
|  | 
 | ||||||
|  | import org.openjdk.jmh.annotations.*; | ||||||
|  | import org.openjdk.jmh.runner.Runner; | ||||||
|  | import org.openjdk.jmh.runner.options.Options; | ||||||
|  | import org.openjdk.jmh.runner.options.OptionsBuilder; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Set; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | 
 | ||||||
|  | @BenchmarkMode(Mode.AverageTime) | ||||||
|  | @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||||||
|  | @Warmup(iterations = 5) | ||||||
|  | public class CollectionsBenchmark { | ||||||
|  | 
 | ||||||
|  |     @State(Scope.Thread) | ||||||
|  |     public static class MyState { | ||||||
|  |         private Set<Employee> employeeSet = new HashSet<>(); | ||||||
|  |         private List<Employee> employeeList = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         private long iterations = 10000; | ||||||
|  | 
 | ||||||
|  |         private Employee employee = new Employee(100L, "Harry"); | ||||||
|  | 
 | ||||||
|  |         @Setup(Level.Trial) | ||||||
|  |         public void setUp() { | ||||||
|  | 
 | ||||||
|  |             for (long i = 0; i < iterations; i++) { | ||||||
|  |                 employeeSet.add(new Employee(i, "John")); | ||||||
|  |                 employeeList.add(new Employee(i, "John")); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             employeeList.add(employee); | ||||||
|  |             employeeSet.add(employee); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean testArrayList(MyState state) { | ||||||
|  |         return state.employeeList.contains(state.employee); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean testHashSet(MyState state) { | ||||||
|  |         return state.employeeSet.contains(state.employee); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) throws Exception { | ||||||
|  |         Options options = new OptionsBuilder() | ||||||
|  |                 .include(CollectionsBenchmark.class.getSimpleName()).threads(1) | ||||||
|  |                 .forks(1).shouldFailOnError(true) | ||||||
|  |                 .shouldDoGC(true) | ||||||
|  |                 .jvmArgs("-server").build(); | ||||||
|  |         new Runner(options).run(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package com.baeldung.performance; | ||||||
|  | 
 | ||||||
|  | public class Employee { | ||||||
|  | 
 | ||||||
|  |     private Long id; | ||||||
|  |     private String name; | ||||||
|  | 
 | ||||||
|  |     public Employee(Long id, String name) { | ||||||
|  |         this.name = name; | ||||||
|  |         this.id = id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public boolean equals(Object o) { | ||||||
|  |         if (this == o) return true; | ||||||
|  |         if (o == null || getClass() != o.getClass()) return false; | ||||||
|  | 
 | ||||||
|  |         Employee employee = (Employee) o; | ||||||
|  | 
 | ||||||
|  |         if (!id.equals(employee.id)) return false; | ||||||
|  |         return name.equals(employee.name); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int hashCode() { | ||||||
|  |         int result = id.hashCode(); | ||||||
|  |         result = 31 * result + name.hashCode(); | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,80 @@ | |||||||
|  | package com.baeldung.collection; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.function.Predicate; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  | 
 | ||||||
|  | import org.junit.Assert; | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  | 
 | ||||||
|  | public class StreamOperateAndRemoveUnitTest { | ||||||
|  | 
 | ||||||
|  |     private List<Item> itemList; | ||||||
|  | 
 | ||||||
|  |     @Before | ||||||
|  |     public void setup() { | ||||||
|  | 
 | ||||||
|  |         itemList = new ArrayList<>(); | ||||||
|  |         for (int i = 0; i < 10; i++) { | ||||||
|  |             itemList.add(new Item(i)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAListOf10Items_whenFilteredForQualifiedItems_thenFilteredListContains5Items() { | ||||||
|  | 
 | ||||||
|  |         final List<Item> filteredList = itemList.stream().filter(item -> item.isQualified()) | ||||||
|  |             .collect(Collectors.toList()); | ||||||
|  | 
 | ||||||
|  |         Assert.assertEquals(5, filteredList.size()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAListOf10Items_whenOperateAndRemoveQualifiedItemsUsingRemoveIf_thenListContains5Items() { | ||||||
|  | 
 | ||||||
|  |         final Predicate<Item> isQualified = item -> item.isQualified(); | ||||||
|  |         itemList.stream().filter(isQualified).forEach(item -> item.operate()); | ||||||
|  |         itemList.removeIf(isQualified); | ||||||
|  | 
 | ||||||
|  |         Assert.assertEquals(5, itemList.size()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAListOf10Items_whenOperateAndRemoveQualifiedItemsUsingRemoveAll_thenListContains5Items() { | ||||||
|  | 
 | ||||||
|  |         final List<Item> operatedList = new ArrayList<>(); | ||||||
|  |         itemList.stream().filter(item -> item.isQualified()).forEach(item -> { | ||||||
|  |             item.operate(); | ||||||
|  |             operatedList.add(item); | ||||||
|  |         }); | ||||||
|  |         itemList.removeAll(operatedList); | ||||||
|  | 
 | ||||||
|  |         Assert.assertEquals(5, itemList.size()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     class Item { | ||||||
|  | 
 | ||||||
|  |         private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); | ||||||
|  | 
 | ||||||
|  |         private final int value; | ||||||
|  | 
 | ||||||
|  |         public Item(final int value) { | ||||||
|  | 
 | ||||||
|  |             this.value = value; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public boolean isQualified() { | ||||||
|  | 
 | ||||||
|  |             return value % 2 == 0; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public void operate() { | ||||||
|  | 
 | ||||||
|  |             logger.info("Even Number: " + this.value); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat; | |||||||
| import static org.hamcrest.Matchers.*; | import static org.hamcrest.Matchers.*; | ||||||
| 
 | 
 | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.Arrays; | ||||||
| import java.util.LinkedList; | import java.util.LinkedList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| @ -48,4 +49,14 @@ public class RemoveFirstElementUnitTest { | |||||||
|         assertThat(linkedList, not(contains("cat"))); |         assertThat(linkedList, not(contains("cat"))); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() { | ||||||
|  |         String[] stringArray = {"foo", "bar", "baz"}; | ||||||
|  | 
 | ||||||
|  |         String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length); | ||||||
|  | 
 | ||||||
|  |         assertThat(modifiedArray.length, is(2)); | ||||||
|  |         assertThat(modifiedArray[0], is("bar")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,26 @@ | |||||||
|  | package org.baeldung.java.collections; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Collections; | ||||||
|  | import java.util.List; | ||||||
|  | import org.junit.Assert; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | class CollectionsEmpty { | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenArrayList_whenAddingElement_addsNewElement() { | ||||||
|  |         ArrayList<String> mutableList = new ArrayList<>(); | ||||||
|  |         mutableList.add("test"); | ||||||
|  | 
 | ||||||
|  |         Assert.assertEquals(mutableList.size(), 1); | ||||||
|  |         Assert.assertEquals(mutableList.get(0), "test"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test(expected = UnsupportedOperationException.class) | ||||||
|  |     public void givenCollectionsEmptyList_whenAddingElement_throwsUnsupportedOperationException() { | ||||||
|  |         List<String> immutableList = Collections.emptyList(); | ||||||
|  |         immutableList.add("test"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										26
									
								
								core-java-concurrency-collections/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								core-java-concurrency-collections/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | |||||||
|  | *.class | ||||||
|  | 
 | ||||||
|  | 0.* | ||||||
|  | 
 | ||||||
|  | #folders# | ||||||
|  | /target | ||||||
|  | /neoDb* | ||||||
|  | /data | ||||||
|  | /src/main/webapp/WEB-INF/classes | ||||||
|  | */META-INF/* | ||||||
|  | .resourceCache | ||||||
|  | 
 | ||||||
|  | # Packaged files # | ||||||
|  | *.jar | ||||||
|  | *.war | ||||||
|  | *.ear | ||||||
|  | 
 | ||||||
|  | # Files generated by integration tests | ||||||
|  | *.txt | ||||||
|  | backup-pom.xml | ||||||
|  | /bin/ | ||||||
|  | /temp | ||||||
|  | 
 | ||||||
|  | #IntelliJ specific | ||||||
|  | .idea/ | ||||||
|  | *.iml | ||||||
							
								
								
									
										15
									
								
								core-java-concurrency-collections/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								core-java-concurrency-collections/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | |||||||
|  | ========= | ||||||
|  | 
 | ||||||
|  | ## Core Java Concurrency Collections Examples | ||||||
|  | 
 | ||||||
|  | ### Relevant Articles:  | ||||||
|  | - [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) | ||||||
|  | - [A Guide to ConcurrentMap](http://www.baeldung.com/java-concurrent-map) | ||||||
|  | - [Guide to PriorityBlockingQueue in Java](http://www.baeldung.com/java-priority-blocking-queue) | ||||||
|  | - [Avoiding the ConcurrentModificationException in Java](http://www.baeldung.com/java-concurrentmodificationexception) | ||||||
|  | - [Custom Thread Pools In Java 8 Parallel Streams](http://www.baeldung.com/java-8-parallel-streams-custom-threadpool) | ||||||
|  | - [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue) | ||||||
|  | - [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue) | ||||||
|  | - [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue) | ||||||
|  | - [Guide to the ConcurrentSkipListMap](http://www.baeldung.com/java-concurrent-skip-list-map) | ||||||
|  | - [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist) | ||||||
							
								
								
									
										94
									
								
								core-java-concurrency-collections/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								core-java-concurrency-collections/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,94 @@ | |||||||
|  | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|  |     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||||
|  |     <modelVersion>4.0.0</modelVersion> | ||||||
|  |     <groupId>com.baeldung</groupId> | ||||||
|  |     <artifactId>core-java-concurrency-collections</artifactId> | ||||||
|  |     <version>0.1.0-SNAPSHOT</version> | ||||||
|  |     <packaging>jar</packaging> | ||||||
|  |     <name>core-java-concurrency-collections</name> | ||||||
|  | 
 | ||||||
|  |     <parent> | ||||||
|  |         <groupId>com.baeldung</groupId> | ||||||
|  |         <artifactId>parent-java</artifactId> | ||||||
|  |         <version>0.0.1-SNAPSHOT</version> | ||||||
|  |         <relativePath>../parent-java</relativePath> | ||||||
|  |     </parent> | ||||||
|  | 
 | ||||||
|  |     <dependencies> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.apache.commons</groupId> | ||||||
|  |             <artifactId>commons-collections4</artifactId> | ||||||
|  |             <version>${commons-collections4.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>commons-io</groupId> | ||||||
|  |             <artifactId>commons-io</artifactId> | ||||||
|  |             <version>${commons-io.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.apache.commons</groupId> | ||||||
|  |             <artifactId>commons-lang3</artifactId> | ||||||
|  |             <version>${commons-lang3.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.apache.commons</groupId> | ||||||
|  |             <artifactId>commons-math3</artifactId> | ||||||
|  |             <version>${commons-math3.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.assertj</groupId> | ||||||
|  |             <artifactId>assertj-core</artifactId> | ||||||
|  |             <version>${assertj.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>com.jayway.awaitility</groupId> | ||||||
|  |             <artifactId>awaitility</artifactId> | ||||||
|  |             <version>${avaitility.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |     </dependencies> | ||||||
|  | 
 | ||||||
|  |     <build> | ||||||
|  |         <finalName>core-java-concurrency-collections</finalName> | ||||||
|  |         <resources> | ||||||
|  |             <resource> | ||||||
|  |                 <directory>src/main/resources</directory> | ||||||
|  |                 <filtering>true</filtering> | ||||||
|  |             </resource> | ||||||
|  |         </resources> | ||||||
|  | 
 | ||||||
|  |         <plugins> | ||||||
|  |             <plugin> | ||||||
|  |                 <groupId>org.apache.maven.plugins</groupId> | ||||||
|  |                 <artifactId>maven-dependency-plugin</artifactId> | ||||||
|  |                 <executions> | ||||||
|  |                     <execution> | ||||||
|  |                         <id>copy-dependencies</id> | ||||||
|  |                         <phase>prepare-package</phase> | ||||||
|  |                         <goals> | ||||||
|  |                             <goal>copy-dependencies</goal> | ||||||
|  |                         </goals> | ||||||
|  |                         <configuration> | ||||||
|  |                             <outputDirectory>${project.build.directory}/libs</outputDirectory> | ||||||
|  |                         </configuration> | ||||||
|  |                     </execution> | ||||||
|  |                 </executions> | ||||||
|  |             </plugin> | ||||||
|  |              | ||||||
|  |         </plugins> | ||||||
|  |     </build> | ||||||
|  | 
 | ||||||
|  |     <properties> | ||||||
|  |         <!-- util --> | ||||||
|  |         <guava.version>21.0</guava.version> | ||||||
|  |         <commons-lang3.version>3.5</commons-lang3.version> | ||||||
|  |         <commons-math3.version>3.6.1</commons-math3.version> | ||||||
|  |         <commons-collections4.version>4.1</commons-collections4.version> | ||||||
|  |         <collections-generic.version>4.01</collections-generic.version> | ||||||
|  |         <!-- testing --> | ||||||
|  |         <assertj.version>3.6.1</assertj.version> | ||||||
|  |         <avaitility.version>1.7.0</avaitility.version> | ||||||
|  |     </properties> | ||||||
|  | 
 | ||||||
|  | </project> | ||||||
| @ -10,15 +10,18 @@ public class BlockingQueueUsage { | |||||||
|         int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); |         int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); | ||||||
|         int poisonPill = Integer.MAX_VALUE; |         int poisonPill = Integer.MAX_VALUE; | ||||||
|         int poisonPillPerProducer = N_CONSUMERS / N_PRODUCERS; |         int poisonPillPerProducer = N_CONSUMERS / N_PRODUCERS; | ||||||
| 
 |         int mod = N_CONSUMERS % N_PRODUCERS; | ||||||
|         BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(BOUND); |         BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(BOUND); | ||||||
| 
 | 
 | ||||||
|         for (int i = 0; i < N_PRODUCERS; i++) { |         for (int i = 1; i < N_PRODUCERS; i++) { | ||||||
|             new Thread(new NumbersProducer(queue, poisonPill, poisonPillPerProducer)).start(); |             new Thread(new NumbersProducer(queue, poisonPill, poisonPillPerProducer)).start(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         for (int j = 0; j < N_CONSUMERS; j++) { |         for (int j = 0; j < N_CONSUMERS; j++) { | ||||||
|             new Thread(new NumbersConsumer(queue, poisonPill)).start(); |             new Thread(new NumbersConsumer(queue, poisonPill)).start(); | ||||||
|         } |         } | ||||||
|  |          | ||||||
|  |         new Thread(new NumbersProducer(queue, poisonPill, poisonPillPerProducer+mod)).start(); | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -0,0 +1,19 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <configuration> | ||||||
|  |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|  |         <encoder> | ||||||
|  |             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n | ||||||
|  |             </pattern> | ||||||
|  |         </encoder> | ||||||
|  |     </appender> | ||||||
|  | 
 | ||||||
|  |     <logger name="org.springframework" level="WARN" /> | ||||||
|  |     <logger name="org.springframework.transaction" level="WARN" /> | ||||||
|  | 
 | ||||||
|  |     <!-- in order to debug some marshalling issues, this needs to be TRACE --> | ||||||
|  |     <logger name="org.springframework.web.servlet.mvc" level="WARN" /> | ||||||
|  | 
 | ||||||
|  |     <root level="INFO"> | ||||||
|  |         <appender-ref ref="STDOUT" /> | ||||||
|  |     </root> | ||||||
|  | </configuration> | ||||||
| @ -7,22 +7,12 @@ | |||||||
| - [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) | - [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) | ||||||
| - [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava) | - [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava) | ||||||
| - [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) | - [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) | ||||||
| - [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) |  | ||||||
| - [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) | - [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) | ||||||
| - [A Guide to ConcurrentMap](http://www.baeldung.com/java-concurrent-map) |  | ||||||
| - [Guide to PriorityBlockingQueue in Java](http://www.baeldung.com/java-priority-blocking-queue) |  | ||||||
| - [Avoiding the ConcurrentModificationException in Java](http://www.baeldung.com/java-concurrentmodificationexception) |  | ||||||
| - [Custom Thread Pools In Java 8 Parallel Streams](http://www.baeldung.com/java-8-parallel-streams-custom-threadpool) |  | ||||||
| - [Guide to java.util.concurrent.Locks](http://www.baeldung.com/java-concurrent-locks) | - [Guide to java.util.concurrent.Locks](http://www.baeldung.com/java-concurrent-locks) | ||||||
| - [An Introduction to ThreadLocal in Java](http://www.baeldung.com/java-threadlocal) | - [An Introduction to ThreadLocal in Java](http://www.baeldung.com/java-threadlocal) | ||||||
| - [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue) |  | ||||||
| - [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue) |  | ||||||
| - [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue) |  | ||||||
| - [Guide to the ConcurrentSkipListMap](http://www.baeldung.com/java-concurrent-skip-list-map) |  | ||||||
| - [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) | - [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) | ||||||
| - [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) | - [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) | ||||||
| - [The Dining Philosophers Problem in Java](http://www.baeldung.com/java-dining-philoshophers) | - [The Dining Philosophers Problem in Java](http://www.baeldung.com/java-dining-philoshophers) | ||||||
| - [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist) |  | ||||||
| - [Guide to the Java Phaser](http://www.baeldung.com/java-phaser) | - [Guide to the Java Phaser](http://www.baeldung.com/java-phaser) | ||||||
| - [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized) | - [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized) | ||||||
| - [An Introduction to Atomic Variables in Java](http://www.baeldung.com/java-atomic-variables) | - [An Introduction to Atomic Variables in Java](http://www.baeldung.com/java-atomic-variables) | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| <configuration> | <configuration> | ||||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|         <encoder> |         <encoder> | ||||||
|             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|             </pattern> |             </pattern> | ||||||
|         </encoder> |         </encoder> | ||||||
|     </appender> |     </appender> | ||||||
|  | |||||||
| @ -183,6 +183,7 @@ | |||||||
|             <plugin> |             <plugin> | ||||||
|                 <groupId>org.springframework.boot</groupId> |                 <groupId>org.springframework.boot</groupId> | ||||||
|                 <artifactId>spring-boot-maven-plugin</artifactId> |                 <artifactId>spring-boot-maven-plugin</artifactId> | ||||||
|  |                 <version>${spring-boot-maven-plugin.version}</version> | ||||||
|                 <executions> |                 <executions> | ||||||
|                     <execution> |                     <execution> | ||||||
|                         <goals> |                         <goals> | ||||||
| @ -316,6 +317,7 @@ | |||||||
|         <esapi.version>2.1.0.1</esapi.version> |         <esapi.version>2.1.0.1</esapi.version> | ||||||
|         <jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version> |         <jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version> | ||||||
|         <async-http-client.version>2.4.5</async-http-client.version> |         <async-http-client.version>2.4.5</async-http-client.version> | ||||||
|  |         <spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import java.io.BufferedReader; | ||||||
|  | import java.io.FileReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | 
 | ||||||
|  | public class BufferedReaderExample { | ||||||
|  | 
 | ||||||
|  |     protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException { | ||||||
|  | 
 | ||||||
|  |         ArrayList<String> result = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         try (BufferedReader br = new BufferedReader(new FileReader(filename))) { | ||||||
|  | 
 | ||||||
|  |             while (br.ready()) { | ||||||
|  |                 result.add(br.readLine()); | ||||||
|  |             } | ||||||
|  |             return result; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import java.io.FileReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | 
 | ||||||
|  | public class FileReaderExample { | ||||||
|  | 
 | ||||||
|  |     protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException { | ||||||
|  | 
 | ||||||
|  |         ArrayList<String> result = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         try (FileReader f = new FileReader(filename)) { | ||||||
|  |             StringBuffer sb = new StringBuffer(); | ||||||
|  |             while (f.ready()) { | ||||||
|  |                 char c = (char) f.read(); | ||||||
|  |                 if (c == '\n') { | ||||||
|  |                     result.add(sb.toString()); | ||||||
|  |                     sb = new StringBuffer(); | ||||||
|  |                 } else { | ||||||
|  |                     sb.append(c); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (sb.length() > 0) { | ||||||
|  |                 result.add(sb.toString()); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,18 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.nio.file.Files; | ||||||
|  | import java.nio.file.Paths; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | public class FilesReadLinesExample { | ||||||
|  | 
 | ||||||
|  |     protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException { | ||||||
|  |          | ||||||
|  |         List<String> result = Files.readAllLines(Paths.get(filename)); | ||||||
|  |          | ||||||
|  |         return (ArrayList<String>) result; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import java.io.FileReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Scanner; | ||||||
|  | 
 | ||||||
|  | public class ScannerIntExample { | ||||||
|  | 
 | ||||||
|  |     protected static ArrayList<Integer> generateArrayListFromFile(String filename) throws IOException { | ||||||
|  |          | ||||||
|  |         ArrayList<Integer> result = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         try (Scanner s = new Scanner(new FileReader(filename))) { | ||||||
|  | 
 | ||||||
|  |             while (s.hasNext()) { | ||||||
|  |                 result.add(s.nextInt()); | ||||||
|  |             } | ||||||
|  |             return result; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import java.io.FileReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Scanner; | ||||||
|  | 
 | ||||||
|  | public class ScannerStringExample { | ||||||
|  | 
 | ||||||
|  |     protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException { | ||||||
|  |          | ||||||
|  |         ArrayList<String> result = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         try (Scanner s = new Scanner(new FileReader(filename))) { | ||||||
|  | 
 | ||||||
|  |             while (s.hasNext()) { | ||||||
|  |                 result.add(s.nextLine()); | ||||||
|  |             } | ||||||
|  |             return result; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -2,7 +2,7 @@ | |||||||
| <configuration> | <configuration> | ||||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|         <encoder> |         <encoder> | ||||||
|             <pattern>web - %date [%thread] %-5level %logger{36} - %message%n |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|             </pattern> |             </pattern> | ||||||
|         </encoder> |         </encoder> | ||||||
|     </appender> |     </appender> | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ public class FilenameFilterManualTest { | |||||||
|     @BeforeClass |     @BeforeClass | ||||||
|     public static void setupClass() { |     public static void setupClass() { | ||||||
|         directory = new File(FilenameFilterManualTest.class.getClassLoader() |         directory = new File(FilenameFilterManualTest.class.getClassLoader() | ||||||
|             .getResource("testFolder") |             .getResource("fileNameFilterManualTestFolder") | ||||||
|             .getFile()); |             .getFile()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,19 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertTrue; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class BufferedReaderUnitTest { | ||||||
|  | 
 | ||||||
|  |     protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; | ||||||
|  |      | ||||||
|  |     @Test | ||||||
|  |     public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { | ||||||
|  |         List<String> lines = BufferedReaderExample.generateArrayListFromFile(TEXT_FILENAME); | ||||||
|  |         assertTrue("File does not has 2 lines", lines.size() == 2); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,19 @@ | |||||||
|  | package com.baeldung.fileparser; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertTrue; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class FileReaderUnitTest { | ||||||
|  | 
 | ||||||
|  |     protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; | ||||||
|  |      | ||||||
|  |     @Test | ||||||
|  |     public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { | ||||||
|  |         List<String> lines = FileReaderExample.generateArrayListFromFile(TEXT_FILENAME); | ||||||
|  |         assertTrue("File does not has 2 lines", lines.size() == 2); | ||||||
|  |     } | ||||||
|  | } | ||||||
Some files were not shown because too many files have changed in this diff Show More
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user