当前位置:网站首页 > 技术博客 > 正文

java hashmap hashtable



import java.util.HashMap ;

class

Main

{

public static void

main

( String [ ]

args

) {

// 创建一个 HashMap

HashMap

< Integer

, String

>

sites

= new

HashMap

<> ( ) ;

// 往 HashMap 添加一些元素

sites.

put ( 1

,

"Google" ) ;

sites.

put ( 2

,

"Runoob" ) ;

sites.

put ( 3

,

"Taobao" ) ;

System

.

out

.

println ( "sites HashMap: " +

sites

) ;

// key 的映射存在于 HashMap 中

// Not Found - 如果 HashMap 中没有该 key,则返回默认值

String

value1

=

sites.

getOrDefault ( 1

,

"Not Found" ) ;

System

.

out

.

println ( "Value for key 1: " +

value1

) ;

// key 的映射不存在于 HashMap 中

// Not Found - 如果 HashMap 中没有该 key,则返回默认值

String

value2

=

sites.

getOrDefault ( 4

,

"Not Found" ) ;

System

.

out

.

println ( "Value for key 4: " +

value2

) ;

}
}

版权声明


相关文章:

  • fetch发送post请求2025-01-10 13:30:05
  • 树状数据结构怎么实现2025-01-10 13:30:05
  • 如何读取xml2025-01-10 13:30:05
  • ormapping 框架2025-01-10 13:30:05
  • 红外循迹小车pid算法计算公式2025-01-10 13:30:05
  • 编写swap函数交换结构体2025-01-10 13:30:05
  • jstatd2025-01-10 13:30:05
  • 爬虫数据抓取软件2025-01-10 13:30:05
  • 多线程无锁技术2025-01-10 13:30:05
  • 格栅数据2025-01-10 13:30:05