简体   繁体   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());

} 

} }

I am new so I will learn hash with time. 我是新手,所以我会随着时间学习哈希。 Now, I want to learn how can I do static search,insert,delete method in main. 现在,我想学习如何在main中进行静态搜索,插入,删除方法。 Also how can I store key-value in the array? 另外,如何将键值存储在数组中? Thank you in advance. 先感谢您。 Output Search this person 1 information: A Is the person I wrote type of KEY: true Is the person I wrote type of VALUE: false 输出搜索此人1信息:A是我写的人KEY类型:true是我写的人VALUE类型:false

Hash Table Values: 哈希表值:

6 --> F 6-> F

5 --> E 5-> E

4 --> D 4-> D

3 --> C 3-> C

2 --> B 2-> B

1 --> A 1-> A

8 --> H 8->高

7 --> G 7-> G

Is student hashtable empty: false Hashtable size: 8 学生哈希表是否为空:否哈希表大小:8

I want search in this shape; 我想以这种形状搜索;

Output 产量

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

With HashMap, you only do this: 使用HashMap,您只能执行以下操作:

At the beginin of your code: 在代码的开头:

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

and you get : put, get, contains, remove 然后您得到:放置,获取,包含,删除

You have several options: 您有几种选择:

to learn more, see this: difference between linkedhashmap, hashmap, map, hashtable 要了解更多信息,请参阅以下内容: linkedhashmap,hashmap,map,hashtable之间的区别

and this: Differences between HashMap and Hashtable? 这: HashMap和Hashtable之间的区别?

If you want to keep your order, use LinkedHashMap instead 如果要保留订单,请改用LinkedHashMap

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

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