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
This commit is contained in:
Syed Ali Raza 2017-05-28 15:07:15 +05:00 committed by Grzegorz Piwowarek
parent 99c7a91587
commit 8c8c01ebbb
13 changed files with 306 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.stream;
import java.util.List;
import java.util.stream.Stream;
public class StreamApi {
public String getLastElementUsingReduce(List<String> valueList) {
Stream<String> stream = valueList.stream();
return stream.reduce((first, second) -> second).orElse(null);
}
public Integer getInfiniteStreamLastElementUsingReduce() {
Stream<Integer> stream = Stream.iterate(0, i -> i + 1);
return stream.limit(20).reduce((first, second) -> second).orElse(null);
}
public String getLastElementUsingSkip(List<String> valueList) {
long count = valueList.stream().count();
Stream<String> stream = valueList.stream();
return stream.skip(count - 1).findFirst().orElse(null);
}
}

View File

@ -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<String> valueList = new ArrayList<String>();
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<String> valueList = new ArrayList<String>();
valueList.add("Joe");
valueList.add("John");
valueList.add("Sean");
String last = streamApi.getLastElementUsingSkip(valueList);
assertEquals("Sean", last);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="ftpReader" class="com.baeldung.beaninjection.FtpReader">
<constructor-arg value="localhost" />
<constructor-arg value="21" />
</bean>
<bean name="readerService" class="com.baeldung.beaninjection.ReaderService">
<property name="ftpReader" ref="ftpReader" />
</bean>
</beans>

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}