Java

[Java] HashMap 정렬하기

블로그 주인장 2024. 4. 14.

List 오름차순 (Key 정렬)


  
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void solution1(String[] sNation, String[] sCapital) {
Map<String , String> map = new HashMap<>();
for (int i = 0; i < sNation.length; i++) {
map.put(sNation[i], map.getOrDefault(sNation[i], sCapital[i]));
}
List<String> keyList = new ArrayList<>(map.keySet());
keyList.sort((s1,s2) -> s1.compareTo(s2));
for (String key : keyList) {
System.out.println("Key : " + key + ", Value : " + map.get(key));
}
}
public static void main(String[] args) {
String[] sNation = {"KOREA","JAPAN","CHINA","USA"};
String[] sCapital = {"SEOUL", "TOKYO", "BEIJING", "WASHINGTON"};
System.out.println("==== List 오름차순 (Key 정렬) ====");
solution1(sNation, sCapital);
// ==== List 오름차순 (Key 정렬) ====
// Key : CHINA, Value : BEIJING
// Key : JAPAN, Value : TOKYO
// Key : KOREA, Value : SEOUL
// Key : USA, Value : WASHINGTON
}
}

List 오름차순 (Value 정렬)


  
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void solution2(String[] sNation, String[] sCapital) {
Map<String , String> map = new HashMap<>();
for (int i = 0; i < sNation.length; i++) {
map.put(sNation[i], map.getOrDefault(sNation[i], sCapital[i]));
}
List<String> valueList = new ArrayList<>(map.values());
valueList.sort(String::compareTo);
for (String value : valueList) {
System.out.println("Value : " + value);
}
}
public static void main(String[] args) {
String[] sNation = {"KOREA","JAPAN","CHINA","USA"};
String[] sCapital = {"SEOUL", "TOKYO", "BEIJING", "WASHINGTON"};
System.out.println("==== List 오름차순 (value 정렬) ====");
solution2(sNation, sCapital);
// ==== List 오름차순 (value 정렬) ====
// Value : BEIJING
// Value : SEOUL
// Value : TOKYO
// Value : WASHINGTON
}
}

Stream 오름차순 (Key 정렬)


  
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void solution3(String[] sNation, String[] sCapital) {
Map<String , String> map = new HashMap<>();
for (int i = 0; i < sNation.length; i++) {
map.put(sNation[i], map.getOrDefault(sNation[i], sCapital[i]));
}
List<Map.Entry<String, String>> entries = map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());
for (Map.Entry<String,String> entry : entries) {
System.out.println("Key : " + entry.getKey() + ", Value : " + entry.getValue());
}
}
public static void main(String[] args) {
String[] sNation = {"KOREA","JAPAN","CHINA","USA"};
String[] sCapital = {"SEOUL", "TOKYO", "BEIJING", "WASHINGTON"};
System.out.println("==== Stream 오름차순 (Key 정렬) ====");
solution3(sNation, sCapital);
// ==== Stream 오름차순 (Key 정렬) ====
// Key : CHINA, Value : BEIJING
// Key : JAPAN, Value : TOKYO
// Key : KOREA, Value : SEOUL
// Key : USA, Value : WASHINGTON
}
}

Stream 오름차순 (Value 정렬)


  
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void solution4(String[] sNation, String[] sCapital) {
Map<String , String> map = new HashMap<>();
for (int i = 0; i < sNation.length; i++) {
map.put(sNation[i], map.getOrDefault(sNation[i], sCapital[i]));
}
// Map.Entry 리스트 생성
List<Map.Entry<String, String>> entries = new ArrayList<>(map.entrySet());
// 리스트를 value 기준으로 정렬
// 방법 1
entries.sort((entry1, entry2) ->
entry1.getValue().compareTo(entry2.getValue()));
// 방법 2
entries.sort(Comparator.comparing(Entry::getValue));
// 방법 3
entries.sort(Entry.comparingByValue());
for (Map.Entry<String, String> entry : entries) {
System.out.println("Key : " + entry.getKey() + ", Value : " + entry.getValue());
}
}
public static void main(String[] args) {
String[] sNation = {"KOREA","JAPAN","CHINA","USA"};
String[] sCapital = {"SEOUL", "TOKYO", "BEIJING", "WASHINGTON"};
System.out.println("==== Stream 오름차순 (Value 정렬) ====");
solution4(sNation, sCapital);
// ==== Stream 오름차순 (Value 정렬) ====
// Key : CHINA, Value : BEIJING
// Key : KOREA, Value : SEOUL
// Key : JAPAN, Value : TOKYO
// Key : USA, Value : WASHINGTON
}
}
반응형