MoreCollections

- Showd that whilst the `ArrayList` type HAS a `srt(...)` methdo, the `List` interface doesn't
- The `Cllections` class DOES however have sorting methods that can opertae on `List`-based types
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-28 16:38:37 +02:00
parent e4bb4a1058
commit b47a39f7ac
1 changed files with 38 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package ocp.practice.collections.MoreCollections;
import java.util.ArrayList;
import java.util.Collections;
import com.sun.tools.javac.util.List;
/**
* More colelctions practicing
*
@ -8,6 +13,39 @@ public class App
{
public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(4);
numbers.add(3);
numbers.add(2);
numbers.add(1);
/**
* Holds a bunch of sorting routines that
* can be applied on collections, such as
* a {@link List}
*/
System.out.println("Before sort: "+numbers);
Collections.sort(numbers, (left, right) -> left-right);
System.out.println("After sort: "+numbers); // Sorts it IN-PLACE
// Collections.
ArrayList<Integer> numbers2 = new ArrayList<Integer>();
numbers2.add(5);
numbers2.add(4);
numbers2.add(3);
numbers2.add(2);
numbers2.add(1);
/**
* Worth noting that {@linkArrayList#sort}
* also exists
*/
System.out.println("Before sort: "+numbers2);
numbers2.sort((left, right) -> left-right);
System.out.println("After sort: "+numbers2); // Sorts it IN-PLACE
}
}