๐ซ ํค์ค์คํฌ ๊ณผ์ ๋ฅผ ๊ตฌํํ๋ฉฐ ๊ฒช์ ๋ฌธ์ ์ ๊ณผ ํด๊ฒฐ๋ฐฉ๋ฒ, ์๋ก ์๊ฒ๋ ์ ์ ๊ธฐ๋กํฉ๋๋ค.
์ฅ๋ฐ๊ตฌ๋ ๊ธฐ๋ฅ์ ๊ฒ์ฆํ๋ ์ค ์ฃผ๋ฌธ ์ ์ฅ๋ฐ๊ตฌ๋์ ๋ด๊ธด ๋ชจ๋ ํญ๋ชฉ์ ์ถ๋ ฅํ ๋ ์ฃผ๋ฌธ ํญ๋ชฉ์ด ๋ฉ๋ด๋ช ์ ๋ ฌ ์์๋ ์๋๊ณ ์ฅ๋ฐ๊ตฌ๋์ ๋ด์ ์์๋ ์๋ ์ ์ ์๋ ๊ธฐ์ค์ผ๋ก ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ 2๊ฐ์ง๋ฅผ ์ ๋ฆฌํ๋ค.
| ์ฃผ๋ฌธ์์ | ๋ฉ๋ด๋ช |
|---|---|
| 1 | ShackBurger |
| 2 | SmokeShack |
| 3 | Fresh Brewed Iced Tea |
| 4 | Fountain Soda |
| 5 | Cups & Cones |
ํ์ ๊ฐ์ ์์๋ก ์ฅ๋ฐ๊ตฌ๋์ ๋ฉ๋ด๋ฅผ ๋ด๊ณ ์ฃผ๋ฌธ

์ฃผ๋ฌธ ํ๋ชฉ์ ๋ด๋ cartItems๋ HashMap<> ์ผ๋ก ๊ตฌํ๋์๋ค.
public class Cart {
// menuItem(๋ฉ๋ด๋ช
, ๊ฐ๊ฒฉ์ ๋ณด)๊ณผ ์๋์ ์ ์ฅ
final Map<MenuItem, Integer> cartItems = new HashMap<>();
// ...
}
โถ๏ธ ๊ธฐ์กด์ฝ๋
printCart(): ์ฅ๋ฐ๊ตฌ๋์ ๋ชจ๋ ํญ๋ชฉ์ ์ถ๋ ฅํ๋ ๋ฉ์๋
// ์ฅ๋ฐ๊ตฌ๋ ์ถ๋ ฅ
private void printCart() {
// TODO: ์ฃผ๋ฌธ ๋ชฉ๋ก ์ถ๋ ฅ ์ ๋ฉ๋ด๋ช
์ค๋ฆ์ฐจ์ ์ ๋ ฌ
for (Map.Entry<MenuItem, Integer> entry : cartItems.entrySet()) {
MenuItem item = entry.getKey();
String name = item.getName();
double price = entry.getValue() * item.getPrice();
String description = item.getDescription();
System.out.printf("%-21s | W %.1f | %s%n", name, price, description);
}
}
๐ญ Stream์ sorted()๋ฅผ ์ฌ์ฉํด์ ์ ๋ ฌํ ๋ ๋งค๊ฐ๋ณ์๋ก Comparator๋ฅผ ๋๊ฒจ์ค๋ค?
Comparator<Map.Entry<K, V>>๋ฅผ ๋ฐํํ๋ค.
Integer ๋ฅผ ๋ด๊ณ ์๋ value๋ comparing ์ด ๊ฐ๋ฅํ์ง๋ง, MenuItem ์ ๋ด๊ณ ์๋ key๋ก๋ comparing ํ ์ ์๋ค.
๐จ no instance(s) of type variable(s) U exist so that MenuItem conforms to Comparable<? super U>
โ MenuItem์ด Comparable์ ์ค์ํ๋๋ก ์ ํ ๋ณ์ U์ ์ธ์คํด์ค๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.
MenuItem ์์๋ ์ฌ๋ฌ ํ๋๊ฐ๋ค์ด ์กด์ฌํ๊ณ , MenuItem ๋ ๊ฐ๋ฐ์๊ฐ ์๋กญ๊ฒ ์ ์ํ ์ฐธ์กฐ ์๋ฃํ์ด๊ธฐ ๋๋ฌธ์ ์๋ฐ ์ ์ฅ์์๋ ์ด๋ค ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ์ง ์ ์ ์๋ค.
โถ๏ธ ๊ธฐ์ค: Map<MenuItem, Integer>์ keySet()์ name ํ๋๋ก ์ ๋ ฌํ ํ ์ถ๋ ฅ
๐ ๊ธฐ์ค์ ์๋ฐ์๊ฒ ์๋ ค์ฃผ์!
Comparable ์ธํฐํ์ด์ค implementscompareTo() ์ค๋ฒ๋ผ์ด๋ฉ: ์ด๋ค ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ์ง ์ ์public class MenuItem implements Comparable<MenuItem> {
// ...
// Comaparable ์ธํฐํ์ด์ค ๋ฉ์๋
@Override
public int compareTo(MenuItem o) {
// ๊ธธ์ด ๋น๊ต
if (this.name.length() > o.name.length()) {
return 1;
} else if (this.name.length() < o.name.length()) {
return -1;
} else {
// ๊ธธ์ด๊ฐ ๊ฐ์ผ๋ฉด ์ฌ์ ์
return this.name.compareTo(o.name);
}
}
}
MenuItem ๊ฐ์ฒด๋ฅผ ๋น๊ตํ ๋ compareTo()์ ์ ์๋ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค.
โถ๏ธ ์์ ์ฝ๋
// ์ฅ๋ฐ๊ตฌ๋ ์ถ๋ ฅ
private void printCart() {
// ์ฃผ๋ฌธ ๋ชฉ๋ก ์ถ๋ ฅ ์ ๋ฉ๋ด๋ช
์ค๋ฆ์ฐจ์ ์ ๋ ฌ
List<Map.Entry<MenuItem, Integer>> entryList = cartItems.entrySet().stream()
.sorted(Map.Entry.<MenuItem, Integer>comparingByKey())
.toList();
for (Map.Entry<MenuItem, Integer> entry : entryList) {
MenuItem item = entry.getKey();
String name = item.getName();
double price = entry.getValue() * item.getPrice();
String description = item.getDescription();
System.out.printf("%-21s | W %.1f | %s%n", name, price, description);
}
}
stream().sorted().toList()๋ก ๋ฐํ๋ฐ์ entryList๋ฅผ ์ ์ธํ๊ณ ํด๋น ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ ์ถ๋ ฅํ๋ค.

cartItems์ HashMap<>์ด ์๋ LinkedHashMap<>์ผ๋ก ๊ตฌํํ๋ค.
โถ๏ธ ์์ ์ฝ๋
public class Cart {
// menuItem(๋ฉ๋ด๋ช
, ๊ฐ๊ฒฉ์ ๋ณด)๊ณผ ์๋์ ์ ์ฅ
final Map<MenuItem, Integer> cartItems = new LinkedHashMap<>();
// ...
}
โถ๏ธ ์์ ์ฝ๋

์ฃผ๋ฌธ ์ ์ฅ๋ฐ๊ตฌ๋์ ๋ด๊ธด ํญ๋ชฉ์ ์ถ๋ ฅํ๋ printCart()์๋ ์ฅ๋ฐ๊ตฌ๋์ ๋ด์ ์์๋๋ก ์ถ๋ ฅ์ด ๋ ์ ํฉํ์ฌ 2๋ฒ์ผ๋ก ์์
์ปฌ๋ ์ ํ๋ ์์ํฌ ๊ตฌํ์ฒด๋ค์ ์ ๋ ฌํ ๋ ์ด๋ค ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ ์ง์ ๋ฐ๋ผ ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์ ๋ ฌ์ ์ ์ฉํ ์ ์๋ค.