Compare commits

...

3 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire cf7ba35e73 MoreCollections
- Added comment
2023-09-28 16:49:11 +02:00
Tristan B. Velloza Kildaire d6205d2047 MoreCollections
- Added information about reversing
2023-09-28 16:48:04 +02:00
Tristan B. Velloza Kildaire b47a39f7ac 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
2023-09-28 16:38:37 +02:00
1 changed files with 43 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,44 @@ 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.reverse(numbers);
System.out.println("After reverse: "+numbers); // Revererses it IN-PLACE
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
/**
* For reversal, the {@link Collections#reverse} class
* MUST be used as it does not exist in {@link List}
* NOR {@link ArrayList}
*/
}
}