---
title: "Java: Treemaps"
date: 2015-01-01
description: "## Quick summary * Treemaps stores key-value pairs * Treemaps guarantees their order * It"
canonical_url: https://idratherbewriting.com/java-treemaps/
---
# Java: Treemaps
## Quick summary

 - Treemaps stores key-value pairs

 - Treemaps guarantees their order

 - It’s common to iterate through a map and see if it has a particular value

 - Treemaps implements a Map interface

 - In Treemaps, keys are sorted

Eclipse example: treemaps

## Sample

```java
public class java.util.TreeMap 
implements SortedMap, Cloneable, java.io.Serializable {
public TreeMap(); // constructors
public TreeMap(Comparator c);
public TreeMap(Map m);
public TreeMap(SortedMap m);

public Comparator comparator();
public K firstKey();
public K lastKey();
public Object clone();

public SortedMap headMap(K toKey);
public SortedMap tailMap(K fromKey);
public SortedMap subMap(K fromKey, K toKey);
}
```