Unordered Data using HashMap in Java
May 1, 2017
Categorised in: Java Core
Understanding through Code
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
public static void main( String[] args ){
Map<String, String> map = new HashMap<>();
map.put("India", "First");
map.put("US", "Second");
map.put("Canada", "Third");
System.out.println(map);
map.put("Russia", "Fourth");
System.out.println(map);
String mapString = map.get("US");
System.out.println("The US stands at: " +mapString);
System.out.println(mapString);
map.remove("Russia");
}
}
Output
- {Canada=Third, US=Second, India=First}
- {Canada=Third, US=Second, India=First, Russia=Fourth}
- The US stands at: Second
- Second
Notes
- HashMap is unordered data collection.
- HashMap is implementation of Map. Just like ArrayList is an implementation of List.
- Maps are in Key, Value pairs.
- You can look up an item in HashMap with the help of Key.
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.