bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Data Structures
Java•Java Data Structures

Java Map

Native lesson simulator

Hash map lookup

Hash the key, jump to a bucket, then compare entries there.

Ada -> bucket 20Cy:21Bo:22Ada:3Dee:33

Ada hashes to bucket 2; lookup only scans entries in that bucket.

Java Map Interface

The Map interface is a part of the Java Collections Framework and is used to store key-value pairs . Each key must be unique, but values can be duplicated.

A Map is useful when you want to associate a key (like a name or ID) with a value (like an age or description).

Common classes that implement Map

  • HashMap - fast and unordered
  • TreeMap - sorted by key
  • LinkedHashMap - ordered by insertion

Tip

Use a Map when you want to associate values with unique keys, like storing user IDs with names.

Common Map Methods

MethodDescription
put()Adds or updates a key-value pair
get()Returns the value for a given key
remove()Removes the key and its value
containsKey()Checks if the map contains the key
keySet()Returns a set of all keys

Map vs. Set vs. List

FeatureListSetMap
Duplicates allowed?YesNoKeys: No Values: Yes
Stores key-value pairs?NoNoYes
Maintains order?YesNo (unless using TreeSet or LinkedHashSet)No (unless using TreeMap or LinkedHashMap)

Previous

Java LinkedHashSet

Next

Java HashMap

This chapter

Overview
16

Lessons

58m

Read time

1. Java Data Structures2. Java Collections Framework3. Java List4. Java ArrayList5. Java LinkedList6. Java List Sorting7. Java Set8. Java HashSet9. Java TreeSet10. Java LinkedHashSet11. Java Map12. Java HashMap13. Java TreeMap14. Java LinkedHashMap15. Java Iterator16. Java Algorithms

On this page

Java Map InterfaceCommon Map MethodsMap vs. Set vs. List