--- id: java-treemap title: "Java TreeMap" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["μžλ°” TreeMap"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["java", "programming", "w3schools", "collections-framework", "treemap"] raw_sources: ["https://www.w3schools.com/java/java_treemap.asp"] applied_in: [] github_commit: "" --- # [[Java TreeMap]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) TreeMap's comparison table reveals a subtle constraint beyond sorting/speed β€” HashMap allows exactly ONE null key, while TreeMap disallows null keys entirely, since sorting requires comparing keys and `null` has no natural ordering position. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`TreeMap`** β€” stores key-value pairs sorted automatically by key. [S1] - **Same API as HashMap** β€” `put()`, `get()`, `remove()`, `clear()`, `size()`, `keySet()`, `values()`. [S1] - **No null keys** β€” unlike HashMap (which allows one null key), TreeMap disallows null keys entirely. [S1] - **Performance trade-off** β€” slower than HashMap due to maintaining sort order. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Sorted-by-key output: `TreeMap capitalCities = new TreeMap<>(); capitalCities.put("England", "London"); capitalCities.put("Austria", "Wien"); ... // {Austria=Wien, England=London, India=New Dehli, Norway=Oslo, USA=Washington DC} β€” alphabetically sorted by key`. [S1] - Comparison table: HashMap (no order, allows one null key, faster) vs. TreeMap (sorted by keys, no null keys, slower). [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **null ν‚€ μ œμ•½**: HashMap은 null ν‚€λ₯Ό ν•˜λ‚˜ ν—ˆμš©ν•˜μ§€λ§Œ TreeMap은 μ „ν˜€ ν—ˆμš©ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 점이 λΉ„κ΅ν‘œμ—μ„œ λͺ…ν™•νžˆ ꡬ뢄됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” μ•ŒνŒŒλ²³μˆœμœΌλ‘œ μ •λ ¬λœ κ΅­κ°€-μˆ˜λ„ λͺ©λ‘μ΄ ν‚€ 기반 μ •λ ¬ 쑰회의 λŒ€ν‘œ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) TreeMap sorts entries alphabetically by key (Java): ```java TreeMap capitalCities = new TreeMap<>(); capitalCities.put("England", "London"); capitalCities.put("Austria", "Wien"); capitalCities.put("India", "New Dehli"); System.out.println(capitalCities); // {Austria=Wien, England=London, India=New Dehli} ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[Java Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[Java HashMap]], [[Java LinkedHashMap]], [[Java TreeSet]] - **μ°Έμ‘° λ§₯락:** 정렬이 ν•„μš”ν•œ Map κ΅¬ν˜„μ²΄ β€” LinkedHashMap(μ‚½μž… μˆœμ„œ 보쑴) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” Java TreeMap β€” https://www.w3schools.com/java/java_treemap.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Java TreeMap" page (Astra wiki-curation, P-Reinforce v3.1 format).