# 用Java對HashMap排序

## 1.簡介

• `TreeMap`
• `ArrayList``Collections.sort()`
• `TreeSet`
• 使用`Stream` API `,`最後，
• 使用`Guava`

## 2.使用`TreeMap`

``````Map<String, Employee> map = new HashMap<>();

Employee employee1 = new Employee(1L, "Mher");

map.put(employee1.getName(), employee1);

Employee employee2 = new Employee(22L, "Annie");

map.put(employee2.getName(), employee2);

Employee employee3 = new Employee(8L, "John");

map.put(employee3.getName(), employee3);

Employee employee4 = new Employee(2L, "George");

map.put(employee4.getName(), employee4);``````

``````public class Employee implements Comparable<Employee> {

private Long id;

private String name;

// constructor, getters, setters

// override equals and hashCode

@Override

public int compareTo(Employee employee) {

return (int)(this.id - employee.getId());

}

}``````

``TreeMap<String, Employee> sorted = new TreeMap<>(map);``

``````TreeMap<String, Employee> sorted = new TreeMap<>();

sorted.putAll(map);``````

``````Annie=Employee{id=22, name='Annie'}

George=Employee{id=2, name='George'}

John=Employee{id=8, name='John'}

Mher=Employee{id=1, name='Mher'}``````

## 3.使用**`ArrayList`**

### 3.1。按鍵排序

``````List<String> employeeByKey = new ArrayList<>(map.keySet());

Collections.sort(employeeByKey);``````

``[Annie, George, John, Mher]``

### 3.2。按值排序

``List<Employee> employeeById = new ArrayList<>(map.values());``

``Collections.sort(employeeById);``

``````[Employee{id=1, name='Mher'},

Employee{id=2, name='George'},

Employee{id=8, name='John'},

Employee{id=22, name='Annie'}]``````

## 4.使用`TreeSet`

``````Employee employee5 = new Employee(1L, "Mher");

map.put(employee5.getName(), employee5);

Employee employee6 = new Employee(22L, "Annie");

map.put(employee6.getName(), employee6);``````

### 4.1。按鍵排序

``SortedSet<String> keySet = new TreeSet<>(map.keySet());``

``[Annie, George, John, Mher]``

### 4.2。按值排序

``SortedSet<Employee> values = new TreeSet<>(map.values());``

``````[Employee{id=1, name='Mher'},

Employee{id=2, name='George'},

Employee{id=8, name='John'},

Employee{id=22, name='Annie'}]``````

## 5.使用Lambda和流

### 5.1。按鍵排序

``````map.entrySet()

.stream()

.sorted(Map.Entry.<String, Employee>comparingByKey())

.forEach(System.out::println);``````

``````Annie=Employee{id=22, name='Annie'}

George=Employee{id=2, name='George'}

John=Employee{id=8, name='John'}

Mher=Employee{id=1, name='Mher'}``````

### 5.2。按值排序

``````map.entrySet()

.stream()

.sorted(Map.Entry.comparingByValue())

.forEach(System.out::println);``````

``````Mher=Employee{id=1, name='Mher'}

George=Employee{id=2, name='George'}

John=Employee{id=8, name='John'}

Annie=Employee{id=22, name='Annie'}``````

``````Map<String, Employee> result = map.entrySet()

.stream()

.sorted(Map.Entry.comparingByValue())

.collect(Collectors.toMap(

Map.Entry::getKey,

Map.Entry::getValue,

(oldValue, newValue) -> oldValue, LinkedHashMap::new));``````

## 6.使用番石榴

``````Ordering naturalOrdering = Ordering.natural()

.onResultOf(Functions.forMap(map, null));``````

``ImmutableSortedMap.copyOf(map, naturalOrdering);``

``````Mher=Employee{id=1, name='Mher'}

George=Employee{id=2, name='George'}

John=Employee{id=8, name='John'}

Annie=Employee{id=22, name='Annie'}``````