Home > Java > javaTutorial > body text

How to implement HashMap, LinkedHashMap and TreeMap in JShell in Java 9?

王林
Release: 2023-09-12 12:17:02
forward
1154 people have browsed it

如何在Java 9的JShell中实现HashMap、LinkedHashMap和TreeMap?

JShellJava 9中引入的命令行提示工具,也称为REPL 评估简单语句、执行它并立即打印输出的工具。

Map 接口指定一个契约,以键/值的形式实现元素集合对。实现Map接口的Java集合类有HashMap、LinkedHashMap和TreeMap

在下面的代码片段中,HashMap的元素不保证以插入顺序或键的排序顺序存储。

Snippet-1

<strong>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}</strong>
Copy after login

在下面的代码片段中,LinkedHashMap的元素已存储在插入中订单。

Snippet-2

<strong>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}</strong>
Copy after login

在下面的代码片段中,TreeMap 的元素已按键的自然排序顺序存储。

代码片段-3

<strong>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}</strong>
Copy after login

The above is the detailed content of How to implement HashMap, LinkedHashMap and TreeMap in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!