Get Set view of Keys from TreeMap in Java
Description
The following code shows how to get Set view of Keys from TreeMap.
Example
/*from ww w . j av a 2s.c o m*/
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Set;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> treeMap = new TreeMap<String, String>();
treeMap.put("1", "One");
treeMap.put("3", "Three");
treeMap.put("2", "Two");
Set st = treeMap.keySet();
Iterator itr = st.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
st.remove("1");
System.out.println(treeMap.containsKey("1"));
}
}
The code above generates the following result.