java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
- All Implemented Interfaces:
- Serializable, Cloneable, Map<K,V>
- Direct Known Subclasses:
- LinkedHashMap, PrinterStateReasons
- See Also:
- Top Examples, Source Code,
Object.hashCode() ,
Collection ,
TreeMap ,
Hashtable
public void clear() - See Also:
- V, AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[192]Convert Map to ArrayList By Anonymous on 2005/09/19 07:18:23 Rate
Map aMap = new HashMap ( ) ; aList = new ArrayList ( aMap.values ( ) ) ; aMap.clear ( ) ; [1143]Use HashMap By mk_rauf { at } yahoo { dot } com on 2005/09/08 07:44:21 Rate
import java.util.*; public class UseHashMap { public static void main ( String [ ] args ) { String aa= "INDIA"; HashMap hashmap = new HashMap ( ) ; //To add elements in the HashMap hashmap.put ( "one",new Integer ( 1 ) ) ; hashmap.put ( "two",null ) ; hashmap.put ( "three","THREE" ) ; hashmap.put ( "four",aa ) ; //To see the size and to check whether HashMap is empty or not System.out.println ( "The size of HashMap = "+hashmap.size ( ) ) ; System.out.println ( "If hashmap empty = "+hashmap.isEmpty ( ) ) ; System.out.println ( "The elements of HashMap are" ) ; Set set= hashmap.keySet ( ) ; Iterator iter = set.iterator ( ) ; int i=1; while ( iter.hasNext ( ) ) { System.out.println ( " "+i+" ) "+hashmap.get ( iter.next ( ) ) ) ; i++; } // Checks whether this key/value is present in the HashMap or not System.out.println ( "Contains this \"one\" Key ="+hashmap.containsKey ( "one" ) ) ; System.out.println ( "Contains this \"five\" Key ="+hashmap.containsKey ( "five" ) ) ; System.out.println ( "Contains this \"aa\" Value ="+hashmap.containsValue ( aa ) ) ; System.out.println ( "Contains this \"new Integer ( 6 ) \" Value ="+hashmap.containsValue ( new Integer ( 6 ) ) ) ; //The value for the key specified System.out.println ( "The value for the \"three\" key ="+hashmap.get ( "three" ) ) ; //Remove the value for the sepcified key hashmap.remove ( "two" ) ; System.out.println ( "The size of HashMap has reduced by one" ) ; System.out.println ( "The size of HashMap = "+hashmap.size ( ) ) ; System.out.println ( "The values of HashMap are ="+hashmap.values ( ) ) ; //Remove all the elements from the HashMap hashmap.clear ( ) ; System.out.println ( "If hashmap empty = "+hashmap.isEmpty ( ) ) ; } } [2009]thanx By vana on 2010/07/02 01:10:31 Rate
HashMap is simple and useful thanx for sharing. I m working with SHINE pattern and find out it has something close to hashmap you can find doc and pattern here http://sourceforge.net/projects/shine-enterpris/
public Object clone() - See Also:
Cloneable , V, AbstractMap
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[622]Shallow copy of a HashMap By Sreeni Alakappan on 2005/06/02 08:01:37 Rate
// Create a new HashMap HashMap hash = new HashMap ( ) ; // Add values to the created HashMap hash.put ( "one", new Integer ( 1 ) ) ; hash.put ( "two", new Integer ( 2 ) ) ; hash.put ( "three", new Integer ( 3 ) ) ; // Use the clone method to create a shallow copy of the HashMap hash HashMap newHash = ( HashMap ) hash.clone ( ) ; // Print the clones hashmap System.out.println ( "MAP = "+newHash ) ;
public boolean containsKey(Object key) - See Also:
- V, AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[623]HashMap containsKey By Sreeni Alakappan on 2004/01/22 17:50:40 Rate
HashMap hash = new HashMap ( ) ; hash.put ( "one", new Integer ( 1 ) ) ; hash.put ( "two", new Integer ( 2 ) ) ; hash.put ( "three", new Integer ( 3 ) ) ; System.out.println ( "Contains Key "+hash.containsKey ( "one" ) ) ;
public boolean containsValue(Object value) - See Also:
- V, AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[624]HashMap containsValue By Sreeni Alakappan on 2005/03/26 09:45:46 Rate
HashMap hash = new HashMap ( ) ; hash.put ( "one", new Integer ( 1 ) ) ; hash.put ( "two", new Integer ( 2 ) ) ; hash.put ( "three", new Integer ( 3 ) ) ; System.out.println ( "Contains Value "+hash.containsValue ( new Integer ( 3 ) ) ) ;
public Set<Map.Entry<K,V>> entrySet() - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[387]Iterate through Map entry set By nitinparashar { at } opermail { dot } com on 2004/12/09 09:45:31 Rate
for ( Iterator it=pairList.entrySet ( ) .iterator ( ) ; it.hasNext ( ) ; ) { Map.Entry entry = ( Map.Entry ) it.next ( ) ; Object key = entry.getKey ( ) ; Object value = entry.getValue ( ) ; System.out.println ( ( String ) key+" " + ( String ) value ) ; } [1248]Uses Generics in Map By Kai Middleton on 2005/01/07 21:22:25 Rate
// look Ma, no casts! ( uses generics ) public class MyClass { // the data structure to iterate over: private TreeMap < String, Boolean > logs; // An inner class to iterate over 'logs'. // It's a custom iterator // that does a bit more than a normal iterator private class LogsIteratorImpl implements LogsIterator { private Iterator < Map.Entry < String, Boolean > > iterator; private Map.Entry < String, Boolean > entry; private int index; LogsIteratorImpl ( ) { iterator = MyClass.logs.entrySet ( ) .iterator ( ) ; index = -1; } public boolean hasNext ( ) { return iterator.hasNext ( ) ; } public void goNext ( ) throws NoSuchElementException { entry = iterator.next ( ) ; index++; } public int index ( ) { return index; } public String getLog ( ) { return entry.getKey ( ) ; } public boolean isSelected ( ) { return ( entry.getValue ( ) ) .booleanValue ( ) ; } public void setSelected ( boolean selected ) { entry.setValue ( new Boolean ( selected ) ) ; } } } [1472]Use for-each loop through Map entry set By Sparky on 2005/07/06 11:44:13 Rate
// Java 5.0 Map pairList = new HashMap ( ) ; // ... populate pairList ... pairList.put ( 1, "one" ) ; pairList.put ( 2, "two" ) ; for ( Map.Entry pair : pairList ) { System.out.println ( pair.getKey ( ) + " :: " + pair.getValue ( ) ) ; } [1974]simple By HRS on 2008/07/24 10:29:33 Rate
public static void main ( String [ ] args ) { // TODO Auto-generated method stub Map < String,String > hm = new HashMap < String,String > ( ) ; hm.put ( "1","Hugo" ) ; hm.put ( "2","Constanza" ) ; hm.put ( "3","Tamara" ) ; Iterator it = hm.entrySet ( ) .iterator ( ) ; while ( it.hasNext ( ) ) { Map.Entry e = ( Map.Entry ) it.next ( ) ; System.out.println ( e.getKey ( ) + " " + e.getValue ( ) ) ; } } }
public V get(Object key) - See Also:
put(Object, Object) , AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[732]Check map key set By Anonymous on 2005/07/15 06:29:36 Rate
public void search ( ) { String searchName= "la" try { Set keys = fullName.keySet ( ) ; Iterator iter = keys.iterator ( ) ; while ( iter.hasNext ( ) ) { keys.get ( searchName ) ; } } catch ( NoSuchElementException nsee ) { System.out.println ( "No entries by that name in the plant base" ) ; } }
public HashMap() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public HashMap(int initialCapacity) - See Also:
- IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public HashMap(int initialCapacity,
float loadFactor) - See Also:
- IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public HashMap(Map<? extends K,? extends V> m) - See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isEmpty() - See Also:
- V, AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[341]Manipulate map entry By Anonymous on 2005/04/13 10:36:01 Rate
Map myMap = new HashMap ( ) ; myMap.put ( "myKey", "myVal" ) ; myMap.remove ( "myKey", "myVal" ) ; if ( myMap.isEmpty ( ) ) System.out.println ( "HashMap is empty ..." ) ; else System.out.println ( "HashMap isn`t empty ..." ) ;
public Set<K> keySet() - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1034]Get a list of Keys in a HashMap and iterate through them By Chetan Arora, chetan_arora { at } yahoo { dot } com on 2005/07/28 01:24:10 Rate
//Sample code shows how to get a list of Keys in a HashMap and //iterate through them import java.util.*; public class testKeySet { public static void main ( String args [ ] ) { HashMap map = new HashMap ( ) ; map.put ( "1","101" ) ; map.put ( "2","202" ) ; Set s = map.keySet ( ) ; Iterator i = s.iterator ( ) ; while ( i.hasNext ( ) ) { System.out.println ( "Key: " + i.next ( ) ) ; } } } [1550]Print the elements of HashMap By Khurram Ilyas Chaudhry on 2005/09/29 12:27:43 Rate
System.out.println ( "The elements of HashMap are" ) ; Set set= hashmap.keySet ( ) ; Iterator iter = set.iterator ( ) ; int i=1; while ( iter.hasNext ( ) ) { System.out.println ( " "+i+" ) "+hashmap.get ( iter.next ( ) ) ) ; i++; }
public Object put(Object key,
Object value) - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[675]Test hash By Karthik Ramachandran on 2004/03/01 04:31:13 Rate
import java.util.*; public class TestHash { public static void main ( String args [ ] ) { HashMap map = new HashMap ( ) ; map.put ( "1", "one" ) ; map.put ( "2", "two" ) ; map.put ( "3", "three" ) ; map.put ( "4", "four" ) ; System.out.println ( map ) ; } } [1478]Store a Value object and tie it with the key By bv on 2005/07/12 02:33:20 Rate
normally, you can store a Value object and tie it with the key.. Here Person is the value object. import java.util.*; import myPackage.Person; public class TestHash { public static void main ( String args [ ] ) { Person p1 = new Person ( ) ; p1.setName ( "Chandramuki" ) ; p1.setAge ( 30 ) ; Person p2 = new Person ( ) ; p2.setName ( "Rajini" ) ; p2.setAge ( 50 ) ; HashMap map = new HashMap ( ) ; map.put ( "1", p1 ) ; map.put ( "2", p2 ) ; System.out.println ( map.size ( ) ) ; System.out.println ( map.containsKey ( "1" ) ) ; System.out.println ( map.toString ( ) ) ; System.out.println ( map ) ; } } ------------------------- */ package myPackage; public class Person { private String name; private int age = 0; public int getAge ( ) { return age; } public String getName ( ) { return name; } public void setAge ( int i ) { age = i; } public void setName ( String string ) { name = string; } }
public V put(K key,
V value) - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void putAll(Map<? extends K,? extends V> m) - See Also:
- NullPointerException, AbstractMap
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public V remove(Object key) - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[775]Remove map entry By rajtech { at } hotmail { dot } com on 2004/07/20 08:01:24 Rate
HashMap removeMe = new HashMap ( ) ; removeMe.put ( "one", new Long ( 1 ) ) ; removeMe.put ( "one", new Long ( 2 ) ) ; removeMe.remove ( removeMe ) ; [1088]_ By me { at } Mphasis on 2004/11/02 04:20:05 Rate
HashMap hMap = new HashMap ( ) ; hMap.put ( "One",new Integer ( 1 ) ) ; hMap.put ( "Two",new Integer ( 2 ) ) ; hMap.put ( "Three",new Integer ( 3 ) ) ; System.out.println ( "HashMap before removing :"+hMap.values ( ) ) ; hMap.remove ( "One" ) ; System.out.println ( "HashMap after removing :"+hMap.values ( ) ) ;
public int size() - See Also:
- V, AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[776]Add map entry By rajtech { at } hotmail { dot } com on 2004/10/22 09:07:46 Rate
HashMap addMe = new HashMap ( ) ; addMe.put ( "new",new String ( "Hello" ) ) ; addMe.put ( "old",new String ( "Bye!!!" ) ) ; System.out.println ( addMe.size ( ) ) ; [1323]Add, remove vector elements By Rakesh on 2005/03/03 02:03:29 Rate
import java.util.*; public class vector1 { public static void main ( String [ ] args ) throws Exception { Vector v1=new Vector ( ) ; v1.trimToSize ( ) ; Collection c=new Vector ( ) ; System.out.println ( "The Vector is "+v1 + " Capacity : "+ v1.capacity ( ) +" Size : "+v1.size ( ) ) ; v1.add ( new Integer ( 10 ) ) ; v1.add ( new Integer ( 20 ) ) ; v1.add ( new Integer ( 30 ) ) ; v1.addElement ( new Integer ( 40 ) ) ; System.out.println ( "Vector : "+v1 + " Capacity : "+ v1.capacity ( ) +" Size : "+v1.size ( ) ) ; Vector v2= ( Vector ) v1.clone ( ) ; System.out.println ( "The Vector v2 is "+v2 ) ; System.out.println ( "The Vector Contains integer 30 : " +v1.contains ( new Integer ( 30 ) ) ) ; v1.remove ( new Integer ( 30 ) ) ; System.out.println ( "Vector : "+v1 + " Capacity : "+ v1.capacity ( ) +" Size : "+v1.size ( ) ) ; System.out.println ( "The Vector Contains integer 30 : " +v1.contains ( new Integer ( 30 ) ) ) ; System.out.println ( "Vector : "+v1 + " Capacity : "+ v1.capacity ( ) +" Size : "+v1.size ( ) ) ; } }
public Collection<V> values() - See Also:
- AbstractMap, Map
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1087]Print map value sets By me { at } Mphasis on 2005/10/07 12:01:35 Rate
HashMap hMap = new HashMap ( ) ; hMap.add ( "One",new Integer ( 1 ) ) ; hMap.add ( "Two",new Integer ( 2 ) ) ; hMap.add ( "Three",new Integer ( 3 ) ) ; System.out.println ( "Values ( ) of HashMap results in :"+hMap.values ( ) ) ; | Popular Tags |