1import java.util.ArrayList;
2import java.util.Collections;
3import java.util.List;
4import java.util.ListIterator;
5public class Main {
6 public static void main(String[] args) {
7 List<String> list = new ArrayList<String>();
8 list.add("a");
9 list.add("b");
10 list.add("c");
11 list.add("d");
12 list.add("e");
13 list.add("f");
14
15 //On met la liste dans le désordre
16 Collections.shuffle(list);
17 System.out.println(list);
18
19 //On la remet dans l'ordre
20 Collections.sort(list);
21 System.out.println(list);
22
23 Collections.rotate(list, -1);
24 System.out.println(list);
25
26 //On récupère une sous-liste
27 List<String> sub = list.subList(2, 5);
28 System.out.println(sub);
29 Collections.reverse(sub);
30 System.out.println(sub);
31
32 //On récupère un ListIterator
33 ListIterator<String> it = list.listIterator();
34 while(it.hasNext()){
35 String str = it.next();
36 if(str.equals("d"))
37 it.set("z");
38 }
39 while(it.hasPrevious())
40 System.out.print(it.previous());
41
42 }
43}
44
1import java.util.Map;
2import java.util.TreeMap;
3public class TreeMapExample
4{
5 public static void main(String[] args)
6 {
7 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
8 tm.put(1000, "Apple");
9 tm.put(1002, "Raspberry");
10 tm.put(1001, "Velvet apple");
11 tm.put(1003, "Banana");
12 for(Map.Entry obj : tm.entrySet())
13 {
14 System.out.println(obj.getKey() + " " + obj.getValue());
15 }
16 }
17}
1import java.util.*;
2
3// Declare the variable using the interface of the object for flexibility.
4// Non-primative data types only.
5Map<Integer, String> mambo = new TreeMap<Integer, String>();
6
7mambo.put(key, value);
8// TreeMap will be sorted by key.
9// Work with any comparable object as a key.
10
11mambo.put(1, "Monica");
12mambo.put(2, "Erica");
13mambo.put(3, "Rita");
1- TreeMap doesn't have null key and keys are sorted
2 Can contain only unique keys and keys are sorted in ascending order.
3
1TreeSet: Can contain only unique values and it is sorted in ascending order
2TreeMap: Can contain only unique keys and keys are sorted in ascending order.
3