gpt4 book ai didi

java - 如何在java中使用哈希表进行搜索方法?

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:39 25 4
gpt4 key购买 nike

import java.util.Enumeration;
import java.util.Hashtable;

//@author fsociety

public class HashFoo {

public static void main(String[] args) {
HashFoo <String,String> student = new HashFoo <String,String>();

student.put("1", "A");//Left side: Key , Right side: Value
student.put("2", "B");//Left side: Key , Right side: Value
student.put("3", "C");//Left side: Key , Right side: Value
student.put("4", "D");//Left side: Key , Right side: Value
student.put("5", "E");//Left side: Key , Right side: Value
student.put("6", "F");//Left side: Key , Right side: Value
student.put("7", "G");//Left side: Key , Right side: Value
student.put("8", "H");//Left side: Key , Right side: Value

System.out.println("Search this person 1 information: " + student.get("1"));//Get someone
System.out.println("Is the person I wrote type of KEY: " + student.containsKey("2"));
System.out.println("Is the person I wrote type of VALUE: " + student.containsValue("Z") + "\n");

Enumeration enumerationValue = student.elements();
Enumeration enumerationKeys = student.keys();
System.out.println("Hash Table Values: "+"\n");

while(enumerationValue.hasMoreElements() && enumerationKeys.hasMoreElements()){
System.out.println(enumerationKeys.nextElement()+ " --> " + enumerationValue.nextElement() + "\n");
}

System.out.println("Is student hashtable empty: " + student.isEmpty());
System.out.println("Hashtable size: " + student.size());

}

}

我是新人,所以我会随着时间的推移学习哈希。现在,我想学习如何在 main 中进行静态搜索、插入、删除方法。另外,如何在数组中存储键值?先感谢您。输出搜索此人1条信息:A我写的人是KEY类型:true我写的人是 VALUE 类型: false

哈希表值:

6 --> F

5 --> E

4 --> D

3 --> C

2 --> B

1 --> A

8 --> H

7 --> G

学生哈希表是否为空: false哈希表大小:8

我想要这种形状的搜索;

输出

1-搜索:..某人..2-插入:..某人..3-删除:..某人..

最佳答案

使用 HashMap,您只需执行以下操作:

在代码的开头:

Map< String,String> student = new HashMap < String,String>();

你会得到:放置、获取、包含、删除

您有多种选择:

要了解更多信息,请参阅:difference between linkedhashmap, hashmap, map, hashtable

还有这个:Differences between HashMap and Hashtable?

如果你想保留顺序,请使用LinkedHashMap

关于java - 如何在java中使用哈希表进行搜索方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34099223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com