-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList08.java
54 lines (42 loc) · 1.51 KB
/
ArrayList08.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 8. Write a Java program to sort a given array list.
Using .sort(comparator)
Collections.sort(arrayList,comparator) // returns void
Collections.shuffle(arrayList) // shuffles and returns void
Collections.reverse(arrayList) // reverse the arrayList and returns void
Collections is a class and its methods are static
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayList08
{
public static void main(String[] args)
{
List<String> colors = getColors();
System.out.println("Colors are: "+colors);
System.out.println("Sorting through comparator");
colors.sort((c1,c2)->c1.compareTo(c2));
System.out.println("Sorted list: "+colors);
System.out.println("Shuffling array list");
Collections.shuffle(colors);
System.out.println("After shuffling: "+colors);
Collections.sort(colors,(c1,c2)->c1.compareTo(c2));
System.out.println("Sorting through collections: "+colors);
Collections.reverse(colors);
System.out.println("Reverse of the sorted list: "+colors);
}
public static List<String> getColors()
{
List<String> colorsList = new ArrayList<>();
System.out.println("Adding colors...");
colorsList.add(new String("Red"));
colorsList.add(new String("Blue"));
colorsList.add(new String("Green"));
colorsList.add(new String("Orange"));
colorsList.add(new String("Violet"));
colorsList.add(new String("Yellow"));
colorsList.add(new String("Indigo"));
colorsList.add(new String("White"));
return colorsList;
}
}