HashMap
- The underlying data structure is Hashtable.
- Hashing insertion order is not preserve, because it based on Hashcode of keys.
- Duplicate keys are not allowed. But values can be duplicated.
- Heterogeneous objects allowed for both keys& value.
- Null key is allowed.
- Null values are allowed, any null of times.
Difference between HashMap& Hashtable:-
HashMap |
Hashtable |
|
|
Collections class contain synchronized Map method:-
HashMap m = new HashMap ();
Map m2 = collections. synchronizedMap (m);
Constructors of HashMap:-
- HashMap h = new HashMap ();// creates an empty HashMap object, with default initial capacity& default fill ratio 0.7.
- HashMap h = new HashMap ( int initial capacity)
- HashMap h = new HashMap ( int initialcapacity, float fillratio);
- HashMap h = new HashMap ( Map m )
import java. util. *; Class HashMapDemo { Public static void main ( String[] args) { HashMap m = new HashMap(); m. put("narayana", 700); m. put("anitha", 800); m.put("sunny", 200); m. put("vighnesh", 500); System.out.println(m); System.out.println(m.put("venu",1000));//700 Set s = m.keySet(); System.out.println(s); collection c = m.values(); System.out.println(c);// 500, 800, 200, 1000 Set s1 = m.entrySet(); iterator itr = s1. iterator(); while ( its.hasNext()) { Map.Entry m1 =( Map.Entry) itr.next(); System.out.println(m1.getkey().equals("narayana")) { m1.setvalue(10000); } } System.out.println(m); } }
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.