【问题标题】:How can I do search method using hashtable in java?java - 如何在java中使用哈希表进行搜索?
【发布时间】:2015-12-04 23:31:32
【问题描述】:
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-删除:..某人..

【问题讨论】:

  • 不清楚。您有一个地图,并且您已经使用了插入(放置)、搜索(包含)、检索(获取)。您也可以删除(remove)。你想做什么?
  • 是的,不清楚。我开始新,所以我不知道我该如何写模具。我不知道如何编写有关搜索、删除、插入的方法,所以我想要所有这些方法,但仅搜索方法就足够了。 @guillaume girod-vitouchkina
  • .contains 和 .get == 搜索

标签: java arrays hash


【解决方案1】:

使用 HashMap,您只需这样做:

在代码的开头:

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

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

您有多种选择:

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

还有这个:Differences between HashMap and Hashtable?

如果您想保留订单,请改用 LinkedHashMap

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2011-07-16
    • 2015-07-20
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多