Bifalgorithm post · Computer science one · Dynamic Data Structures

How does a Java map store and retrieve key-value pairs?

How does a Java map store and retrieve key-value pairs? (Computer science one · Dynamic Data Structures) — "Finally, Java also supports a Map data structure wh…

Ideas in this flowchart

  • Java Map data structure
  • A Map stores key-value pairs.
  • Keys and values can be any object type.
  • Two parameters specify the key and value types when the map is created.
  • HashMap is a common Map implementation.
  • The example defines a map with Integer keys and String values.
  • Key-value pairs 10–"ten" and 20–"twenty" are added to the map.
  • Retrieving the value for key 20 produces "twenty".
  • Retrieving an invalid key, such as 30, produces null.
  • The Collections library includes many other dynamic data structure implementations.
  • Examples include stacks, queues, hash tables, and balanced binary search trees.
Feed/

How does a Java map store and retrieve key-value pairs?

No cover

Channel

Computer science one

1 page

"Finally, Java also supports a Map data structure which allows you to store key-value pairs. The keys and values can be any object type and are specified by two parameters when you create the map. A common implementation is the HashMap.\n\n1 //define a map that maps integers (keys) to strings (values):\n\n2 Map<Integer, String> numbers = new HashMap<Integer, String>();\n\n3\n\n4 //add key-value pairs:\n\n5 numbers.add(10, \"ten\");\n\n6 numbers.add(20, \"twenty\");\n\n7\n\n8 //retrieve values given a key:\n\n9 String name = numbers.get(20); //name equals \"twenty\"\n\n10\n\n11 //invalid mappings result in null:\n\n12 name = numbers.get(30); //name is null\n\nThe Collections library is much more extensive than what we’ve presented here. It includes implementations of stacks, queues, hash tables, balanced binary search trees, and many other dynamic data structure implementations."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.