
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
JShell is a command-line prompt tool introduced in Java 9, and it is also called a REPL tool to evaluate simple statements, executes it, and print the output immediately.
A Map interface specifies a contract to implement collections of elements in the form of key/value pairs. Java collection classes that implement the Map interface are HashMap, LinkedHashMap, and TreeMap.
In the below code snippet, the elements of HashMap are not guaranteed to store either in an insertion order or in the sorted order of keys.
Snippet-1
jshell> HashMap<String, Integer> hashMap = new HashMap<>(); hashMap ==> {} jshell> hashMap.put("Adithya", 101); $2 ==> null jshell> hashMap.put("Jai", 102); $3 ==> null jshell> hashMap.put("Chaitanya", 103); $4 ==> null jshell> hashMap.put("Ravi", 104); $5 ==> null jshell> hashMap hashMap ==> {Chaitanya=103, Jai=102, Ravi=104, Adithya=101}
In the below code snippet, the elements of LinkedHashMap have stored in the insertion order.
Snippet-2
jshell> LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>(); linkedHashMap ==> {} jshell> linkedHashMap.put("Raja", 101); $8 ==> null jshell> linkedHashMap.put("Adithya", 102); $9 ==> null jshell> linkedHashMap.put("Surya", 103); $10 ==> null jshell> linkedHashMap.put("Vamsi", 104); $11 ==> null jshell> linkedHashMap linkedHashMap ==> {Raja=101, Adithya=102, Surya=103, Vamsi=104}
In the below code snippet, the elements of TreeMap have stored in the natural sorted order of keys.
Snippet-3
jshell> TreeMap<String, Integer> treeMap = new TreeMap<>(); treeMap ==> {} jshell> treeMap.put("Raj", 101); $14 ==> null jshell> treeMap.put("Pavan", 102); $15 ==> null jshell> treeMap.put("Arjun", 103); $16 ==> null jshell> treeMap.put("Manoj", 104); $17 ==> null jshell> treeMap treeMap ==> {Arjun=103, Manoj=104, Pavan=102, Raj=101}
- Related Articles
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- What is the differences between TreeMap, HashMap and LinkedHashMap in Java?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- Difference Between HashMap and LinkedHashMap in Java
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How to implement a lambda expression in JShell in Java 9?
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?

Advertisements