Skip to content

Commit

Permalink
sort lists with reverse and joining them
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianLopez3 committed Jun 11, 2024
1 parent 0884539 commit ad1cb65
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/test/java/com/finance/programming/ListExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Working with lists")
public class ListExercises {
Expand All @@ -29,8 +30,40 @@ void testListOfEvenNumbers(){

assertEquals(evenNumbers, actualEvenNumbers);

}

@Test
@DisplayName("test Join a list of strings")
void testJoinAListOfStrings(){
// Convert a list of strings into a single comma-separated string using Collectors.joining.
List<String> listOfString = List.of("Spring", "Microservices", "Boot", "Test");
String expectedString = "Spring,Microservices,Boot,Test";

// String actualString = String.join(",", listOfString);
String actualString = listOfString.stream()
.collect(Collectors.joining(","));

assertEquals(expectedString, actualString);
}


@Test
@DisplayName(": Sort a list of strings in reverse alphabetical order using streams.")
void testSortAListOfStrings(){
List<String> listOfString = List.of("spa", "zar", "tool", "alp", "toy");
List<String> expectedList = List.of("zar", "toy", "tool", "spa", "alp");
List<String> actualList = listOfString.stream()
.sorted(Comparator.reverseOrder())
.toList();

assertIterableEquals(expectedList, actualList, "The list doesn't match");

assertNotEquals(
expectedList,
listOfString.stream().sorted(Comparator.naturalOrder()).toList(),
"The list must not be equals"

);
}

}
4 changes: 2 additions & 2 deletions tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Description: Use reduce to sum all the elements of a list of integers.
✔️ **Exercise 9: Using flatMap**
Description: Take a list of lists of integers and use flatMap to convert it into a flat list of integers.

**Exercise 10: Sorting a list**
✔️ **Exercise 10: Sorting a list**
Description: Sort a list of strings in reverse alphabetical order using streams.

**Exercise 11: Using collect**
✔️ **Exercise 11: Using collect**
Description: Convert a list of strings into a single comma-separated string using Collectors.joining.

**Exercise 12: Creating an infinite Stream**
Expand Down

0 comments on commit ad1cb65

Please sign in to comment.