Tutorialspoint

Java Program to get Sub Map from TreeMap

import java.util.SortedMap;
import java.util.TreeMap;
public class Demo {
	public static void main(String[] args) {
		TreeMap<String, String>tMap = new TreeMap<String, String>();
		tMap.put("1", "A");
		tMap.put("2", "B");
		tMap.put("3", "C");
		tMap.put("4", "D");
		tMap.put("5", "E");
		tMap.put("6", "F");
		tMap.put("7", "G");
		tMap.put("8", "H");
		tMap.put("9", "I");
		tMap.put("10", "J");
		SortedMap<String, String>map = tMap.subMap("3", "7");
		System.out.println("SortedMap  = " + map);
		map = tMap.subMap("5", "7");
		System.out.println("SortedMap  = " + map);
		map = tMap.subMap("8", "8");
		System.out.println("SortedMap  = " + map);
	}
}

Sort Map values by key in Java

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Demo {
	public static void main(String[] args) {
		HashMap<String, String>map = new HashMap<String, String>();
		map.put("1", "A");
		map.put("6", "B");
		map.put("3", "C");
		map.put("7", "D");
		map.put("5", "E");
		map.put("2", "F");
		map.put("4", "G");
		map.put("8", "H");
		Map<String, String>tMap = new TreeMap<String, String>(map);
		System.out.println("Sorted Map values: "+tMap);
	}
}

Java Program to convert HashMap to TreeMap

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Demo {
	public static void main(String[] a) {
		Map<String, String>map = new HashMap<String, String>();
		map.put("1", "A");
		map.put("2", "B");
		map.put("3", "C");
		map.put("4", "D");
		map.put("5", "E");
		map.put("6", "F");
		map.put("7", "G");
		map.put("8", "H");
		map.put("9", "I");
		Map<String, String>sorted = new TreeMap<String, String>(map);
		System.out.println("Sorted Map = "+sorted);
	}
}

Java Program to convert a Map to a read only map

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Demo {
	public static void main(String[] argv) throws Exception {
		Map<String, String>map = new HashMap<String,String>();
		map.put("1","A");
		map.put("2","B");
		map.put("3","C");
		// making it read-only
		map = Collections.unmodifiableMap(map);
		try {
		map.put("4","D");
		map.put("5","E");
		map.put("6","F");
		} catch (UnsupportedOperationException e) {
		System.out.println(e.getMessage());
		}
	}
}

Null value as key in Java HashMap

import java.util.HashMap;
import java.util.Map;
public class Demo {
	public static final void main(String[] args) {
		Map<String,String>map = new HashMap<>();
		map.put("Football", "A");
		map.put("Squash", "B");
		map.put("Cricket", "C");
		map.put("Hockey", "D");
		map.put("Rugby", "E");
		map.put("Golf", "F");
		map.put("Archery", "G");
		System.out.println("Size of HashMap = " + map.size());
		map.put(null, "H");
		System.out.println("Updated Size of HashMap = " + map.size());
		System.out.println("For null = " + map.get(null));
	}
}

Iterator to loop through the Map key set in Java

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Demo {
	public static void main(String[] args) {
		Map<String, String>map = new LinkedHashMap<>();
		map.put("Jack","0");
		map.put("Tim", "1");
		map.put("David","2");
		map.put("Tom", "3");
		map.put("Kevin", "4");
		map.put("Jeff", "5");
		map.put("Katie","6");
		map.put("Steve", "7");
		map.put("Andrew", "8");
		map.put("Angelina", "9");
		String str = (String) map.get("David");
		System.out.println("Rank of David = " + str);
		Iterator iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			String resKey = (String) iterator.next();
			System.out.println("Rank of " + resKey + " is " + map.get(resKey));
		}
	}
}

Insertion using Java collections

import java.util.LinkedHashMap;
import java.util.Map;
public class Demo {
	public static void main(String[] args) {
		Map<String, Integer>map = new LinkedHashMap<>();
		map.put("Jack", 0);
		map.put("Tim", 1);
		map.put("David", 2);
		map.put("Tom", 3);
		map.put("Kevin", 4);
		map.put("Jeff", 5);
		map.put("Katies", 6);
		map.put("Steve", 7);
		map.put("Andrew", 8);
		map.put("Angelina", 9);
		for (Map.Entry<String, Integer>entry: map.entrySet()) {
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
	}
}

Retrieve all the keys from HashMap in Java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Demo {
	public static void main(String[] args) {
		HashMap<Integer, String>map = new HashMap<Integer, String>();
		map.put(10, "A");
		map.put(20, "B");
		map.put(30, "C");
		map.put(40, "D");
		map.put(50, "E");
		map.put(60, "F");
		map.put(70, "G");
		map.put(80, "H");
		Set<Integer>set = map.keySet();
		Iterator<Integer>i = set.iterator();
		while (i.hasNext()) {
			Integer res = i.next();
			System.out.println(res + ": " + map.get(res));
		}
	}
}

Java Program to replace key and value in HashMap

import java.util.HashMap;
import java.util.Map;
public class Demo {
	public static void main(String args[]) {
		Map<Integer, String>map = new HashMap<>();
		map.put(10, "A");
		map.put(20, "B");
		map.put(30, "C");
		map.put(40, "D");
		map.put(50, "E");
		map.put(60, "F");
		map.put(70, "G");
		map.put(80, "H");
		System.out.println("Map = \t" + map);
		map.put(30, "T");
		System.out.println("Updated Map = \t" + map);
	}
}

Java Program to remove key value pair from HashMap

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class Demo {
	public static void main(String[] args) {
		HashMap<String,String>map = new HashMap<String,String>();
		map.put("1", "A");
		map.put("2", "B");
		map.put("3", "C");
		map.put("4", "D");
		map.put("5", "E");
		map.put("6", "F");
		map.put("7", "G");
		map.put("8", "H");
		map.put("9", "I");
		System.out.println("HashMap...");
		for (String res: map.keySet()) {
			System.out.println(res);
		}
		map.remove("5");
		System.out.println("Updated HashMap...");
		for (String res: map.keySet()) {
			System.out.println(res);
		}
		map.remove("8");
		System.out.println("Updated HashMap...");
		for (String res: map.keySet()) {
			System.out.println(res);
		}
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.