Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

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);
		}
	}
}

Java Program to display a prime number less than the given number

public class Demo {
	public static void main(String[] args) {
		int val = 20;
		boolean[] isprime = new boolean[val + 1];
		for (int i = 0; i<= val; i++)
			isprime[i] = true;
			// 0 and 1 is not prime
			isprime[0] =  false;
			isprime[1] =  false;
			int n = (int) Math.ceil(Math.sqrt(val));
			for (int i = 0; i<= n; i++) {
				if (isprime[i])
					for (int j = 2 * i; j<= val; j = j + i)
						// not prime
						isprime[j] = false;
			}
			int myPrime;
			for (myPrime = val; !isprime[myPrime]; myPrime--)
			; // empty loop body
			System.out.println("Largest prime less than or equal to " + val + " = " + myPrime);
	}
}

Java Program to convert positive int to negative and negative to positive

public class Demo {
	public static void main(String[] args) throws java.lang.Exception {
		int positiveVal = 100;
		int negativeVal = (~(positiveVal - 1));
		System.out.println("Result: Positive value converted to Negative = "+negativeVal);
		positiveVal = ~(negativeVal - 1);
		System.out.println("Actual Positive Value = "+positiveVal);
		negativeVal = -200;
		System.out.println("Actual Negative Value = "+negativeVal);
		positiveVal = ~(negativeVal - 1);
		System.out.println("Result: Negative value converted to Positive = "+positiveVal);
	}
}

Compare BigDecimal movePointRight and scaleByPowerOfTen in Java

import java.math.BigDecimal;
public class Demo {
	public static void main(String... args) {
		long base = 3676;
		int scale = 5;
		BigDecimal d = BigDecimal.valueOf(base, scale);
		System.out.println("Value = "+d);
		System.out.println("\nDemonstrating moveRight()...");
		BigDecimal moveRight = d.movePointRight(12);
		System.out.println("Result = "+moveRight);
		System.out.println("Scale = " + moveRight.scale());
		System.out.println("\nDemonstrating scaleByPowerOfTen()...");
		BigDecimal scaleRes = d.scaleByPowerOfTen(12);
		System.out.println("Result = "+scaleRes);
		System.out.println("Scale = " + scaleRes.scale());
	}
}

Java Program to read map by Map.Entry

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class Demo {
	public static void main(String[] a) {
		Properties prop = System.getProperties();
		Iterator i = prop.entrySet().iterator();
		while (i.hasNext()) {
			Map.Entry entry = (Map.Entry) i.next();
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
	}
}

Java Program to loop through Map by Map.Entry

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");
		Set<Map.Entry<String, String>>s = map.entrySet();
		Iterator<Map.Entry<String, String>>i = s.iterator();
		while (i.hasNext()) {
			Map.Entry<String, String>e = (Map.Entry<String, String>) i.next();
			String key = (String) e.getKey();
			String value = (String) e.getValue();
			System.out.println("Key = "+key + " => Value = "+ value);
		}
	}
}

Insertion order with Java LinkedHashMap

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
public class Demo {
	public static void main(String[] args) {
		LinkedHashMap<String,String>lHashMap = new LinkedHashMap<String,String>();
		lHashMap.put("1", "A");
		lHashMap.put("2", "B");
		lHashMap.put("3", "C");
		lHashMap.put("4", "D");
		lHashMap.put("5", "E");
		lHashMap.put("6", "F");
		lHashMap.put("7", "G");
		lHashMap.put("8", "H");
		lHashMap.put("9", "I");
		Collection collection = lHashMap.values();
		Iterator i = collection.iterator();
		while (i.hasNext()){
			System.out.println(i.next());
		}
	}
}

Extract values from HashMap in Java

import java.util.HashMap;
public class Demo {
	public static void main(String args[]) {
		HashMap<Integer, Integer>m = new HashMap<Integer, Integer>();
		m.put(10, 20);
		m.put(30, 40);
		m.put(50, 60);
		m.put(70, 80);
		m.put(90, 100);
		m.put(110, 120);
		m.put(130, 140);
		m.put(150, 160);
		System.out.println("Displaying values from HashMap...");
		for (Integer i: m.keySet()) {
			System.out.println(m.get(i));
		}
	}
}

Advertisements
Loading...

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