Collections – Sorted Map Interface

Hi all! Today i’m going to tell about Sorted Map Interface. We talked about Map Interface before this article. This interface extends Map interface. For example class that implements this interface is TreeMap.
Sorted Map doesn’t allow null keys and null values and it stores elements by sorting with keys.

Let’s take a look Sorted Map methods in shortly

subMap(K fromKey, K toKey) : It returns part of Map with keys range.
headMap(K toKey) : It returns part of Map whose keys are strictly less than toKey
tailMap(K fromKey) : It returns part of Map whose keys are greater than or equal fromKey
firstKey() : It returns fırst key currently in this Map.
lasKey() : It returns last key currently in this Map.
comparator() : It returns the Comparator used to order the keys in this Map.

Let’s check the example code.

import java.util.*;

public class Main {

    public static void main(String... args) {
        SortedMap<Integer, String> ageNameMap = new TreeMap<>();
        ageNameMap.put(26, "Ufuk");
        ageNameMap.put(24, "Halis");
        ageNameMap.put(28, "Alice");

        write(ageNameMap);
        System.out.println("---------------------------------------");
        System.out.println(ageNameMap.subMap(24,26));
        System.out.println(ageNameMap.headMap(27));
        System.out.println(ageNameMap.tailMap(25));
        System.out.println(ageNameMap.firstKey());
        System.out.println(ageNameMap.lastKey());
    }

    private static void write(Map map) {
        Set<Map.Entry> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println("Key : " + entry.getKey());
            System.out.println("Value : " + entry.getValue());
        }
    }
}
Advertisements

Collections – Map Interface

Hi everyone! Today i’m going to explain about Map Interface in Java. We started the these article series with Collections and Map Interface is a valuable part of that Collection Framework. But Map interface is not a subtype Collection Interface. It behaves little bit different.
Map stores elements key-value pairs. You can access to values with key. It can not contains duplicate keys. Each key has only one value. Some of Map implementations can allow null value(key or value) but some of not. Example, HashMap and LinkedHashMap allow that but TreeMap doesn’t allow.

Lets take a look Map interface methods in shortly.

public Object put(Object key, Object value) : This method is used to insert an entry in this map
public void putAll(Map map) : This method is used to insert a map in this map.
public Object remove(Object key) : This method is used to remove an entry for key
public Object get(Object key)  : This method is used to get value for key
public boolean containsKey(Object key) : This method is used to check key is exists.
public Set keySet() : This method returns containing all keys
public Set entrySet() : This method returns containing all keys and values.

Here is the example of code about Map Interface.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Main {

    public static void main(String... args) {
        Map<String, Integer> nameAgeMap = new HashMap<>();
        // Adding elements to map
        nameAgeMap.put("Ufuk", 26);
        nameAgeMap.put("Halis", 26);
        nameAgeMap.put("Alice", 30);

        write(nameAgeMap);
        System.out.println("------------------------------------");
        //Removing element from map
        nameAgeMap.remove("Ufuk");
        write(nameAgeMap);
        System.out.println("Map contains key Ufuk : " + nameAgeMap.containsKey("Ufuk"));
    }

    private static void write(Map map) {
        Set<Map.Entry> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println("Key : " + entry.getKey());
            System.out.println("Value : " + entry.getValue());
        }
    }
}