instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
Java program to remove key value from hashmap
Posted: Mar 14, 2016 4:22 PM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Java program to remove key value from hashmap
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
Advertisement
1.Basic java example program to remove value from hashmap.
using Object remove(Object key) method we can remove key value pair of hashmap. package com.javaremovevaluehashmap; import java.util.Hashmap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class HashmapExample{ public static void main(String[] args) { //create an Hashmap object HashMap<String, String> hashmap = new HashMap(); //Add key values pairs to hashmap hashmap.put("1","One"); hashmap.put("2","Two"); hashmap.put("3","Three"); hashmap.put("4","Four"); hashmap.put("5","Five"); hashmap.put("6","Six"); String key=null; String value="java programming basics Interview questions"; hashmap.put(key,value); /* To remove a key value pair from HashMap use Object remove(Object key) method of HashMap class. It returns either the value mapped with the key or null if no value was mapped. */ Object object = hashmap.remove("4"); System.out.println(object + " Removed from HashMap"); if(!hashmap.isEmpty()){ Iterator it=hashmap.entrySet().iterator(); while(it.hasNext()){ Map.Entry obj=(Entry) it.next(); System.out.print(obj.getKey()+" "); System.out.println(obj.getValue()); } } } }
Output: Four Removed from HashMap null java programming basics Interview questions 1 One 2 Two 3 Three 5 Five 6 Six
Read: Java program to remove key value from hashmap