繁体   English   中英

如何在Java中使用哈希表进行搜索?

[英]How can I do search method using hashtable in java?

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->高

7-> G

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

我想以这种形状搜索;

产量

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

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

在代码的开头:

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

然后您得到:放置,获取,包含,删除

您有几种选择:

要了解更多信息,请参阅以下内容: linkedhashmap,hashmap,map,hashtable之间的区别

这: HashMap和Hashtable之间的区别?

如果要保留订单,请改用LinkedHashMap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM