Code example: Checking If a List Is Sorted in Java (#7139)

This commit is contained in:
Kamlesh Kumar 2019-06-25 08:42:34 +05:30 committed by KevinGilmore
parent ca560514a0
commit 49bafb0300
4 changed files with 239 additions and 1 deletions

View File

@ -18,6 +18,18 @@
<version>${org.assertj.core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
@ -34,6 +46,7 @@
<properties>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>28.0-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,34 @@
package com.baeldung.algorithms.checksortedlist;
public class Employee {
long id;
String name;
public Employee() {
}
public Employee(long id, String name) {
super();
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.algorithms.checksortedlist;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Comparators;
import com.google.common.collect.Ordering;;
public class SortedListChecker {
private SortedListChecker() {
throw new AssertionError();
}
public static boolean checkIfSortedUsingIterativeApproach(List<String> listOfStrings) {
if (isEmpty(listOfStrings) || listOfStrings.size() == 1) {
return true;
}
Iterator<String> iter = listOfStrings.iterator();
String current, previous = iter.next();
while (iter.hasNext()) {
current = iter.next();
if (previous.compareTo(current) > 0) {
return false;
}
}
return true;
}
public static boolean checkIfSortedUsingIterativeApproach(List<Employee> employees, Comparator<Employee> employeeComparator) {
if (isEmpty(employees) || employees.size() == 1) {
return true;
}
Iterator<Employee> iter = employees.iterator();
Employee current, previous = iter.next();
while (iter.hasNext()) {
current = iter.next();
if (employeeComparator.compare(previous, current) > 0) {
return false;
}
}
return true;
}
public static boolean checkIfSortedUsingRecursion(List<String> listOfStrings) {
return isSortedRecursive(listOfStrings, listOfStrings.size());
}
public static boolean isSortedRecursive(List<String> listOfStrings, int index) {
if (index < 2) {
return true;
} else if (listOfStrings.get(index - 2)
.compareTo(listOfStrings.get(index - 1)) > 0) {
return false;
} else {
return isSortedRecursive(listOfStrings, index - 1);
}
}
public static boolean checkIfSortedUsingOrderingClass(List<String> listOfStrings) {
return Ordering.<String> natural()
.isOrdered(listOfStrings);
}
public static boolean checkIfSortedUsingOrderingClass(List<Employee> employees, Comparator<Employee> employeeComparator) {
return Ordering.from(employeeComparator)
.isOrdered(employees);
}
public static boolean checkIfSortedUsingOrderingClassHandlingNull(List<String> listOfStrings) {
return Ordering.<String> natural()
.nullsLast()
.isOrdered(listOfStrings);
}
public static boolean checkIfSortedUsingComparators(List<String> listOfStrings) {
return Comparators.isInOrder(listOfStrings, Comparator.<String> naturalOrder());
}
}

View File

@ -0,0 +1,106 @@
package com.baeldung.algorithms.checksortedlist;
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingComparators;
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingIterativeApproach;
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingOrderingClass;
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingRecursion;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class SortedListCheckerUnitTest {
private List<String> sortedListOfString;
private List<String> unsortedListOfString;
private List<String> singletonList;
private List<Employee> employeesSortedByName;
private List<Employee> employeesNotSortedByName;
@Before
public void setUp() {
sortedListOfString = asList("Canada", "HK", "LA", "NJ", "NY");
unsortedListOfString = asList("LA", "HK", "NJ", "NY", "Canada");
singletonList = Collections.singletonList("NY");
employeesSortedByName = asList(new Employee(1L, "John"), new Employee(2L, "Kevin"), new Employee(3L, "Mike"));
employeesNotSortedByName = asList(new Employee(1L, "Kevin"), new Employee(2L, "John"), new Employee(3L, "Mike"));
}
@Test
public void givenSortedList_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(sortedListOfString)).isTrue();
}
@Test
public void givenSingleElementList_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(singletonList)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingIterativeApproach_thenReturnFalse() {
assertThat(checkIfSortedUsingIterativeApproach(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedListOfEmployees_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
}
@Test
public void givenUnsortedListOfEmployees_whenUsingIterativeApproach_thenReturnFalse() {
assertThat(checkIfSortedUsingIterativeApproach(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
}
@Test
public void givenSortedList_whenUsingRecursion_thenReturnTrue() {
assertThat(checkIfSortedUsingRecursion(sortedListOfString)).isTrue();
}
@Test
public void givenSingleElementList_whenUsingRecursion_thenReturnTrue() {
assertThat(checkIfSortedUsingRecursion(singletonList)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingRecursion_thenReturnFalse() {
assertThat(checkIfSortedUsingRecursion(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedList_whenUsingGuavaOrdering_thenReturnTrue() {
assertThat(checkIfSortedUsingOrderingClass(sortedListOfString)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingGuavaOrdering_thenReturnFalse() {
assertThat(checkIfSortedUsingOrderingClass(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedListOfEmployees_whenUsingGuavaOrdering_thenReturnTrue() {
assertThat(checkIfSortedUsingOrderingClass(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
}
@Test
public void givenUnsortedListOfEmployees_whenUsingGuavaOrdering_thenReturnFalse() {
assertThat(checkIfSortedUsingOrderingClass(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
}
@Test
public void givenSortedList_whenUsingGuavaComparators_thenReturnTrue() {
assertThat(checkIfSortedUsingComparators(sortedListOfString)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingGuavaComparators_thenReturnFalse() {
assertThat(checkIfSortedUsingComparators(unsortedListOfString)).isFalse();
}
}