diff --git a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ArrayListDemo.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ArrayListDemo.java new file mode 100644 index 0000000000..f425f19189 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ArrayListDemo.java @@ -0,0 +1,51 @@ +package com.baeldung.list.listvsarraylist; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ArrayListDemo { + + private ArrayList passengers = new ArrayList<>(20); +// private LinkedList passengers = new LinkedList<>(); // compile time error + + public ArrayList addPassenger(Passenger passenger) { + passengers.add(passenger); + return passengers; + } + + public ArrayList removePassenger(Passenger passenger) { + passengers.remove(passenger); + return passengers; + } + + public ArrayList getPassengersBySource(String source) { + return new ArrayList(passengers.stream() + .filter(it -> it.getSource().equals(source)) + .collect(Collectors.toList())); + } + + public ArrayList getPassengersByDestination(String destination) { + return new ArrayList (passengers.stream() + .filter(it -> it.getDestination().equals(destination)) + .collect(Collectors.toList())); + } + + public long getKidsCount(ArrayList passengerList) { + return passengerList.stream() + .filter(it -> (it.getAge() <= 10)) + .count(); + } + + public ArrayList getFinalPassengersList() { + return new ArrayList (Collections.unmodifiableList(passengers)); + } + + public ArrayList getServicedCountries() { + return new ArrayList (Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList())); + } + +} diff --git a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ListDemo.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ListDemo.java new file mode 100644 index 0000000000..a9a182d21e --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/ListDemo.java @@ -0,0 +1,53 @@ +package com.baeldung.list.listvsarraylist; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ListDemo { + + private List passengers = new ArrayList<>(20); +// private List passengers = new LinkedList<>(); // No compile time error + + public List addPassenger(Passenger passenger) { + passengers.add(passenger); + return passengers; + } + + public List removePassenger(Passenger passenger) { + passengers.remove(passenger); + return passengers; + } + + public List getPassengersBySource(String source) { + return passengers.stream() + .filter(it -> it.getSource().equals(source)) + .collect(Collectors.toList()); + } + + public List getPassengersByDestination(String destination) { + return passengers.stream() + .filter(it -> it.getDestination().equals(destination)) + .collect(Collectors.toList()); + } + + public long getKidsCount(List passengerList) { + return passengerList.stream() + .filter(it -> (it.getAge() <= 10)) + .count(); + } + + public List getFinalPassengersList() { + return Collections.unmodifiableList(passengers); + } + + public List getServicedCountries() { + return Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + } + +} diff --git a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/Passenger.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/Passenger.java new file mode 100644 index 0000000000..9c61b70aa6 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/listvsarraylist/Passenger.java @@ -0,0 +1,49 @@ +package com.baeldung.list.listvsarraylist; + +public class Passenger { + + private String name; + private int age; + private String source; + private String destination; + + public Passenger(String name, int age, String source, String destination) { + this.name = name; + this.age = age; + this.source = source; + this.destination = destination; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public String getDestination() { + return destination; + } + + public void setDestination(String destination) { + this.destination = destination; + } + +} diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ArrayListDemoUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ArrayListDemoUnitTest.java new file mode 100644 index 0000000000..59094ab562 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ArrayListDemoUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.list.listvsarraylist; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.Locale; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ArrayListDemoUnitTest { + + ArrayListDemo application = new ArrayListDemo(); + + Passenger passenger1; + Passenger passenger2; + Passenger passenger3; + + @BeforeEach + protected void setUp() { + passenger1 = new Passenger("Anna", 25, "London", "New York"); + passenger2 = new Passenger("Binny", 35, "New York", "London"); + passenger3 = new Passenger("Chandra", 8, "Paris", "New Delhi"); + application.addPassenger(passenger1); + application.addPassenger(passenger2); + application.addPassenger(passenger3); + } + + @Test + public void givenEmptyList_whenAddedPassenger_thenReturnCurrentPassengerList() { + ArrayList list = application.addPassenger(new Passenger("David", 54, "Milan", "Paris")); + + assertNotNull(list); + assertThat(list).hasSize(4); + } + + @Test + public void givenPassengerList_whenRemovedPassenger_thenReturnCurrentPassengerList() { + ArrayList list = application.removePassenger(passenger3); + + assertNotNull(list); + assertThat(list).hasSize(2); + } + + @Test + public void givenPassengerList_whenPassedWithSourceCity_thenReturnMatchingPassengerList() { + ArrayList list = application.getPassengersBySource("Singapore"); + ArrayList list2 = application.getPassengersBySource("London"); + + assertThat(list).isEmpty(); + assertThat(list2.get(0)).isEqualTo(passenger1); + } + + @Test + public void givenPassengerList_whenPassedWithDestinationCity_thenReturnMatchingPassengerList() { + ArrayList list = application.getPassengersByDestination("Singapore"); + ArrayList list2 = application.getPassengersByDestination("London"); + + assertThat(list).isEmpty(); + assertThat(list2.get(0)).isEqualTo(passenger2); + } + + @Test + public void givenPassengerList_whenFindKidsByAge_thenReturnKidsList() { + ArrayList list = new ArrayList<>(); + list.add(passenger1); + list.add(passenger2); + list.add(passenger3); + long count = application.getKidsCount(list); + + assertThat(count).isEqualTo(1); + } + + @Test + public void givenPassengerList_whenCalledWithCollectionsFunction_thenReturnsListType() { + ArrayList list = application.getFinalPassengersList(); + + assertNotNull(list); + assertThat(list).hasSize(3); + } + + @Test + public void givenCurrentLocale_whenUsingStreams_thenReturnsListType() { + ArrayList servicedCountries = application.getServicedCountries(); + + assertNotNull(servicedCountries); + assertThat(servicedCountries).hasSize(Locale.getISOCountries().length); + } + +} diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ListDemoUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ListDemoUnitTest.java new file mode 100644 index 0000000000..cf239a79ce --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listvsarraylist/ListDemoUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.list.listvsarraylist; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ListDemoUnitTest { + + ListDemo application = new ListDemo(); + + Passenger passenger1; + Passenger passenger2; + Passenger passenger3; + + @BeforeEach + protected void setUp() { + passenger1 = new Passenger("Anna", 25, "London", "New York"); + passenger2 = new Passenger("Binny", 35, "New York", "London"); + passenger3 = new Passenger("Chandra", 8, "Paris", "New Delhi"); + application.addPassenger(passenger1); + application.addPassenger(passenger2); + application.addPassenger(passenger3); + } + + @Test + public void givenEmptyList_whenAddedPassenger_thenReturnCurrentPassengerList() { + List list = application.addPassenger(new Passenger("David", 54, "Milan", "Paris")); + + assertNotNull(list); + assertThat(list).hasSize(4); + } + + @Test + public void givenPresentList_whenRemovedPassenger_thenReturnCurrentPassengerList() { + List list = application.removePassenger(passenger3); + + assertNotNull(list); + assertThat(list).hasSize(2); + } + + @Test + public void givenPresentList_whenPassedWithSourceCity_thenReturnMatchingPassengerList() { + List list = application.getPassengersBySource("Singapore"); + List list2 = application.getPassengersBySource("London"); + + assertThat(list).isEmpty(); + assertThat(list2.get(0)).isEqualTo(passenger1); + } + + @Test + public void givenPresentList_whenPassedWithDestinationCity_thenReturnMatchingPassengerList() { + List list = application.getPassengersByDestination("Singapore"); + List list2 = application.getPassengersByDestination("London"); + + assertThat(list).isEmpty(); + assertThat(list2.get(0)).isEqualTo(passenger2); + } + + @Test + public void givenPassengerList_whenFindKidsByAge_thenReturnKidsList() { + List list = new ArrayList<>(); + list.add(passenger1); + list.add(passenger2); + list.add(passenger3); + long count = application.getKidsCount(list); + + assertThat(count).isEqualTo(1); + } + + @Test + public void givenPresentList_whenCalledWithCollectionsFunction_thenReturnsListType() { + List list = application.getFinalPassengersList(); + + assertNotNull(list); + assertThat(list).hasSize(3); + } + + @Test + public void givenCurrentLocale_whenUsingStreams_thenReturnsListType() { + List servicedCountries = application.getServicedCountries(); + + assertNotNull(servicedCountries); + assertThat(servicedCountries).hasSize(Locale.getISOCountries().length); + } + +}