From 8c8c01ebbb121ee67ec0cfcf9921655caf8b30e3 Mon Sep 17 00:00:00 2001 From: Syed Ali Raza Date: Sun, 28 May 2017 15:07:15 +0500 Subject: [PATCH] BAEL-833: How to get last element of a Stream in Java? (#1930) * Different Types of Bean Injection in Spring * Fixed code formatting and test names for "Different Types of Bean Injection in Spring" * BAEL-833: How to get last element of a Stream in Java? * BAEL-833: Updated based on review from editor --- .../java/com/baeldung/stream/StreamApi.java | 24 +++++++++++ .../com/baeldung/stream/StreamApiTest.java | 43 +++++++++++++++++++ .../baeldung/beaninjection/FileReader.java | 19 ++++++++ .../com/baeldung/beaninjection/FtpReader.java | 28 ++++++++++++ .../com/baeldung/beaninjection/Location.java | 17 ++++++++ .../ReaderApplicationConfig.java | 15 +++++++ .../baeldung/beaninjection/ReaderManager.java | 33 ++++++++++++++ .../baeldung/beaninjection/ReaderService.java | 14 ++++++ .../src/main/resources/injectiontypes.xml | 15 +++++++ .../beaninjection/FileReaderTest.java | 24 +++++++++++ .../baeldung/beaninjection/FtpReaderTest.java | 25 +++++++++++ .../beaninjection/ReaderManagerTest.java | 25 +++++++++++ .../beaninjection/ReaderServiceTest.java | 24 +++++++++++ 13 files changed, 306 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/stream/StreamApi.java create mode 100644 core-java/src/test/java/com/baeldung/stream/StreamApiTest.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/FileReader.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/FtpReader.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/Location.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/ReaderApplicationConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/ReaderManager.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjection/ReaderService.java create mode 100644 spring-core/src/main/resources/injectiontypes.xml create mode 100644 spring-core/src/test/java/com/baeldung/beaninjection/FileReaderTest.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjection/FtpReaderTest.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjection/ReaderManagerTest.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjection/ReaderServiceTest.java diff --git a/core-java/src/main/java/com/baeldung/stream/StreamApi.java b/core-java/src/main/java/com/baeldung/stream/StreamApi.java new file mode 100644 index 0000000000..ec792314d2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/stream/StreamApi.java @@ -0,0 +1,24 @@ +package com.baeldung.stream; + +import java.util.List; +import java.util.stream.Stream; + +public class StreamApi { + + public String getLastElementUsingReduce(List valueList) { + Stream stream = valueList.stream(); + return stream.reduce((first, second) -> second).orElse(null); + } + + public Integer getInfiniteStreamLastElementUsingReduce() { + Stream stream = Stream.iterate(0, i -> i + 1); + return stream.limit(20).reduce((first, second) -> second).orElse(null); + } + + public String getLastElementUsingSkip(List valueList) { + long count = valueList.stream().count(); + Stream stream = valueList.stream(); + return stream.skip(count - 1).findFirst().orElse(null); + } + +} diff --git a/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java b/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java new file mode 100644 index 0000000000..af52b3ee69 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java @@ -0,0 +1,43 @@ +package com.baeldung.stream; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class StreamApiTest { + private StreamApi streamApi = new StreamApi(); + + @Test + public void givenList_whenGetLastElementUsingReduce_thenReturnLastElement() { + List valueList = new ArrayList(); + valueList.add("Joe"); + valueList.add("John"); + valueList.add("Sean"); + + String last = streamApi.getLastElementUsingReduce(valueList); + + assertEquals("Sean", last); + } + + @Test + public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() { + Integer last = streamApi.getInfiniteStreamLastElementUsingReduce(); + assertEquals(new Integer(19), last); + } + + @Test + public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() { + List valueList = new ArrayList(); + valueList.add("Joe"); + valueList.add("John"); + valueList.add("Sean"); + + String last = streamApi.getLastElementUsingSkip(valueList); + + assertEquals("Sean", last); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/FileReader.java b/spring-core/src/main/java/com/baeldung/beaninjection/FileReader.java new file mode 100644 index 0000000000..7ea64e2ce6 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/FileReader.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class FileReader { + + @Autowired + private Location location; + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/FtpReader.java b/spring-core/src/main/java/com/baeldung/beaninjection/FtpReader.java new file mode 100644 index 0000000000..259710ce94 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/FtpReader.java @@ -0,0 +1,28 @@ +package com.baeldung.beaninjection; + +public class FtpReader { + private String ftpHost = null; + private Integer ftpPort = null; + + // Constructor with arguments + public FtpReader(String host, Integer port) { + this.ftpHost = host; + this.ftpPort = port; + } + + public String getFtpHost() { + return ftpHost; + } + + public void setFtpHost(String ftpHost) { + this.ftpHost = ftpHost; + } + + public Integer getFtpPort() { + return ftpPort; + } + + public void setFtpPort(Integer ftpPort) { + this.ftpPort = ftpPort; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/Location.java b/spring-core/src/main/java/com/baeldung/beaninjection/Location.java new file mode 100644 index 0000000000..9ecab83ef6 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/Location.java @@ -0,0 +1,17 @@ +package com.baeldung.beaninjection; + +import org.springframework.stereotype.Component; + +@Component +public class Location { + private String filePath; + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/ReaderApplicationConfig.java b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderApplicationConfig.java new file mode 100644 index 0000000000..cf4d91b1f5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderApplicationConfig.java @@ -0,0 +1,15 @@ +package com.baeldung.beaninjection; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = { "com.baeldung.beaninjection" }) +public class ReaderApplicationConfig { + + @Bean + public FtpReader exampleDAO() { + return new FtpReader("localhost", 21); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/ReaderManager.java b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderManager.java new file mode 100644 index 0000000000..155f2d6eea --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderManager.java @@ -0,0 +1,33 @@ +package com.baeldung.beaninjection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ReaderManager { + private FileReader fileReader; + private FtpReader ftpReader; + + @Autowired + public ReaderManager(FtpReader ftpReader) { + this.ftpReader = ftpReader; + } + + @Autowired + public void setFileReader(FileReader fileReader) { + this.fileReader = fileReader; + } + + public FileReader getFileReader() { + return fileReader; + } + + public void setFtpReader(FtpReader ftpReader) { + this.ftpReader = ftpReader; + } + + public FtpReader getFtpReader() { + return ftpReader; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjection/ReaderService.java b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderService.java new file mode 100644 index 0000000000..60bdac61e5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjection/ReaderService.java @@ -0,0 +1,14 @@ +package com.baeldung.beaninjection; + +public class ReaderService { + private FtpReader ftpReader; + + public void setFtpReader(FtpReader reader) { + this.ftpReader = reader; + } + + public FtpReader getFtpReader() { + return ftpReader; + } + +} diff --git a/spring-core/src/main/resources/injectiontypes.xml b/spring-core/src/main/resources/injectiontypes.xml new file mode 100644 index 0000000000..9dad1e300a --- /dev/null +++ b/spring-core/src/main/resources/injectiontypes.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/beaninjection/FileReaderTest.java b/spring-core/src/test/java/com/baeldung/beaninjection/FileReaderTest.java new file mode 100644 index 0000000000..f843fddd4d --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjection/FileReaderTest.java @@ -0,0 +1,24 @@ +package com.baeldung.beaninjection; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ReaderApplicationConfig.class) +public class FileReaderTest { + + @Autowired + private ApplicationContext context; + + @Test + public void testAutowiredAnnotation_WhenField_ThenInjected() { + FileReader service = context.getBean(FileReader.class); + assertNotNull(service.getLocation()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjection/FtpReaderTest.java b/spring-core/src/test/java/com/baeldung/beaninjection/FtpReaderTest.java new file mode 100644 index 0000000000..a11afff9ea --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjection/FtpReaderTest.java @@ -0,0 +1,25 @@ +package com.baeldung.beaninjection; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:injectiontypes.xml") +public class FtpReaderTest { + + @Autowired + private ApplicationContext context; + + @Test + public void testXMLBeanConfig_WhenConstructorArguments_ThenInjected() { + FtpReader service = context.getBean(FtpReader.class); + assertNotNull(service.getFtpHost()); + assertNotNull(service.getFtpPort()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjection/ReaderManagerTest.java b/spring-core/src/test/java/com/baeldung/beaninjection/ReaderManagerTest.java new file mode 100644 index 0000000000..7d85c0bf07 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjection/ReaderManagerTest.java @@ -0,0 +1,25 @@ +package com.baeldung.beaninjection; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ReaderApplicationConfig.class) +public class ReaderManagerTest { + + @Autowired + private ApplicationContext context; + + @Test + public void testAutowiredAnnotation_WhenConstructorAndSetter_ThenInjected() { + ReaderManager service = context.getBean(ReaderManager.class); + assertNotNull(service.getFtpReader()); + assertNotNull(service.getFileReader()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjection/ReaderServiceTest.java b/spring-core/src/test/java/com/baeldung/beaninjection/ReaderServiceTest.java new file mode 100644 index 0000000000..da4684ad4e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjection/ReaderServiceTest.java @@ -0,0 +1,24 @@ +package com.baeldung.beaninjection; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:injectiontypes.xml") +public class ReaderServiceTest { + + @Autowired + private ApplicationContext context; + + @Test + public void testXMLBeanConfig_WhenSetter_ThenInjected() { + ReaderService service = context.getBean(ReaderService.class); + assertNotNull(service.getFtpReader()); + } +}