BAEL-5419: List vs ArrayList in Java + tests (#11936)
* BAEL-5419: List vs ArrayList in Java + tests * BAEL-5419: Updated code +tests as per review comments * BAEL-5419: Fixed failing tests * BAEL-5419: Fixed JUnit test as per review comments
This commit is contained in:
parent
b67525c5c1
commit
3045fea670
|
@ -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<Passenger> passengers = new ArrayList<>(20);
|
||||
// private LinkedList<Passenger> passengers = new LinkedList<>(); // compile time error
|
||||
|
||||
public ArrayList<Passenger> addPassenger(Passenger passenger) {
|
||||
passengers.add(passenger);
|
||||
return passengers;
|
||||
}
|
||||
|
||||
public ArrayList<Passenger> removePassenger(Passenger passenger) {
|
||||
passengers.remove(passenger);
|
||||
return passengers;
|
||||
}
|
||||
|
||||
public ArrayList<Passenger> getPassengersBySource(String source) {
|
||||
return new ArrayList<Passenger>(passengers.stream()
|
||||
.filter(it -> it.getSource().equals(source))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public ArrayList<Passenger> getPassengersByDestination(String destination) {
|
||||
return new ArrayList<Passenger> (passengers.stream()
|
||||
.filter(it -> it.getDestination().equals(destination))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public long getKidsCount(ArrayList<Passenger> passengerList) {
|
||||
return passengerList.stream()
|
||||
.filter(it -> (it.getAge() <= 10))
|
||||
.count();
|
||||
}
|
||||
|
||||
public ArrayList<Passenger> getFinalPassengersList() {
|
||||
return new ArrayList<Passenger> (Collections.unmodifiableList(passengers));
|
||||
}
|
||||
|
||||
public ArrayList<String> getServicedCountries() {
|
||||
return new ArrayList<String> (Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Passenger> passengers = new ArrayList<>(20);
|
||||
// private List<Passenger> passengers = new LinkedList<>(); // No compile time error
|
||||
|
||||
public List<Passenger> addPassenger(Passenger passenger) {
|
||||
passengers.add(passenger);
|
||||
return passengers;
|
||||
}
|
||||
|
||||
public List<Passenger> removePassenger(Passenger passenger) {
|
||||
passengers.remove(passenger);
|
||||
return passengers;
|
||||
}
|
||||
|
||||
public List<Passenger> getPassengersBySource(String source) {
|
||||
return passengers.stream()
|
||||
.filter(it -> it.getSource().equals(source))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Passenger> getPassengersByDestination(String destination) {
|
||||
return passengers.stream()
|
||||
.filter(it -> it.getDestination().equals(destination))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public long getKidsCount(List<Passenger> passengerList) {
|
||||
return passengerList.stream()
|
||||
.filter(it -> (it.getAge() <= 10))
|
||||
.count();
|
||||
}
|
||||
|
||||
public List<Passenger> getFinalPassengersList() {
|
||||
return Collections.unmodifiableList(passengers);
|
||||
}
|
||||
|
||||
public List<String> getServicedCountries() {
|
||||
return Stream.of(Locale.getISOCountries())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Passenger> list = application.addPassenger(new Passenger("David", 54, "Milan", "Paris"));
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengerList_whenRemovedPassenger_thenReturnCurrentPassengerList() {
|
||||
ArrayList<Passenger> list = application.removePassenger(passenger3);
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengerList_whenPassedWithSourceCity_thenReturnMatchingPassengerList() {
|
||||
ArrayList<Passenger> list = application.getPassengersBySource("Singapore");
|
||||
ArrayList<Passenger> list2 = application.getPassengersBySource("London");
|
||||
|
||||
assertThat(list).isEmpty();
|
||||
assertThat(list2.get(0)).isEqualTo(passenger1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengerList_whenPassedWithDestinationCity_thenReturnMatchingPassengerList() {
|
||||
ArrayList<Passenger> list = application.getPassengersByDestination("Singapore");
|
||||
ArrayList<Passenger> list2 = application.getPassengersByDestination("London");
|
||||
|
||||
assertThat(list).isEmpty();
|
||||
assertThat(list2.get(0)).isEqualTo(passenger2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengerList_whenFindKidsByAge_thenReturnKidsList() {
|
||||
ArrayList<Passenger> 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<Passenger> list = application.getFinalPassengersList();
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrentLocale_whenUsingStreams_thenReturnsListType() {
|
||||
ArrayList<String> servicedCountries = application.getServicedCountries();
|
||||
|
||||
assertNotNull(servicedCountries);
|
||||
assertThat(servicedCountries).hasSize(Locale.getISOCountries().length);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Passenger> list = application.addPassenger(new Passenger("David", 54, "Milan", "Paris"));
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPresentList_whenRemovedPassenger_thenReturnCurrentPassengerList() {
|
||||
List<Passenger> list = application.removePassenger(passenger3);
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPresentList_whenPassedWithSourceCity_thenReturnMatchingPassengerList() {
|
||||
List<Passenger> list = application.getPassengersBySource("Singapore");
|
||||
List<Passenger> list2 = application.getPassengersBySource("London");
|
||||
|
||||
assertThat(list).isEmpty();
|
||||
assertThat(list2.get(0)).isEqualTo(passenger1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPresentList_whenPassedWithDestinationCity_thenReturnMatchingPassengerList() {
|
||||
List<Passenger> list = application.getPassengersByDestination("Singapore");
|
||||
List<Passenger> list2 = application.getPassengersByDestination("London");
|
||||
|
||||
assertThat(list).isEmpty();
|
||||
assertThat(list2.get(0)).isEqualTo(passenger2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengerList_whenFindKidsByAge_thenReturnKidsList() {
|
||||
List<Passenger> 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<Passenger> list = application.getFinalPassengersList();
|
||||
|
||||
assertNotNull(list);
|
||||
assertThat(list).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrentLocale_whenUsingStreams_thenReturnsListType() {
|
||||
List<String> servicedCountries = application.getServicedCountries();
|
||||
|
||||
assertNotNull(servicedCountries);
|
||||
assertThat(servicedCountries).hasSize(Locale.getISOCountries().length);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue