【问题标题】:Correct Way to Use "Hashtable"?正确使用“哈希表”的方法?
【发布时间】:2014-02-10 22:21:10
【问题描述】:

我是 Hashtables 的新手,正在尝试了解它们如何充分发挥作用。我需要确定一个字符串是否出现在一个包含大约 100,000 个字符串的大文件中,每个字符串都在自己的行中。有人告诉我,HashTable 比 LinkedList 或 ArrayList 更有效,因为两者的运行时间都是 O(n)。

我已经查看了 Java 中的 HashTable 类,但由于“put”方法需要一个键以及对象,我不明白我将如何准确地从文件中输入每个字符串。

我想我可以使用 Scanner 来遍历文件中的每个字符串,但是如何将它们输入到 Hashtable 中,以及如何在 HashTable 中使用 contains() 方法?

【问题讨论】:

  • 不要使用Hashtable,而是使用由HashMap支持的Map
  • 为您的情况使用HashSet,它再次使用散列,但它只需要值,而不是键。尽管HashMap 的性能比旧版Hashtable 好得多,后者只是为了向后兼容。
  • 这里是the documentation HashSet
  • @AmirPashazadeh 实际上,您需要的是密钥,而不是值。输入的 将用于支持 Map<Key, Boolean>

标签: java hashtable


【解决方案1】:

对我来说,这听起来像是HashSet 的用例。看看吧!

您只放入以不同方式存储的值(没有双倍值)。然后你可以调用contains 方法来检查你的字符串是否在集合中。

【讨论】:

    【解决方案2】:

    Hashtable 已经很老了,已经被HashMap 取代了。还有HashSet。两者都使用哈希表,但它们有不同的用途。 HashMap 用于将某种值与每个键相关联;例如,您可以使用它来查找某人的姓名并获取他们的电话号码。然而,HashSet 只存储没有任何值的键。您可以使用它来将名称添加到集合中,然后再检查该名称是否在集合中。

    正如 Luiggi 在 cmets 中提到的,HashMapHashSet 只是 MapSet 的具体实现;这些实现使用哈希表,但这些类的其他实现是可用的。构造表时需要使用HashMapHashSet,但通常应将变量简单地声明为MapSet,因为这样您可以将HashMapHashSet 替换为其他一些实现相同方法的类。这样一来,您就不会被绑定到特定的实现。

    【讨论】:

      【解决方案3】:

      您可以将字符串放入HashSet

      Set yourStrings = new HashSet<String>();
      
      for (String line : yourFile) {
          yourStrings.add(line);
      }
      

      然后检查是否存在特定字符串:

      if (yourStrings.contains("Hi!")) {
          // It's present
      }
      else {
          // It's not present
      }
      

      【讨论】:

        【解决方案4】:

        您需要一个 HashSet 来存储文件的每一行。

        *只有当您对文件中每个字符串的出现次数感兴趣时,您才可能需要一个 HashMap。

        import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.HashSet;
        import java.util.Scanner;
        import java.util.Set;
        
        
        public class MyFileUtils {
        
        //this can be omitted, just added to increase speed 
        //when requiring multiple searches in the same file, to avoid recalculations.
        //Use it only if you need to search in one file ONLY
        private static Set<String> stringLines = null;
        
        /*
         * Get a HashSet of all the (distinct) lines in a file
         */
        public static Set<String> getStringLinesFromFile (String filePath) {
            //this can be omitted, just added to support fast multiple calls of this function 
            if (stringLines != null) return stringLines;
        
            Set<String> stringLines = new HashSet<String>(); 
        
            Scanner scanner = null;
            try {
                scanner = new Scanner(new File(filePath));
                while (scanner.hasNextLine())
                    stringLines.add(scanner.nextLine());
            } catch (FileNotFoundException e) {
                System.out.println("File does not exist");
            } finally {
                if(scanner != null)
                    scanner.close();
            }
            //as the first line, this can be omitted, just added to support fast multiple calls of this function 
            MyFileUtils.stringLines = stringLines;
        
            return stringLines;
        }
        
        /*
         * Call this method to search for a stringLine in a file
         */
        public static boolean checkIfStringExistsInFile(String filePath, String aStringLine) {
            return getStringLinesFromFile(filePath).contains(aStringLine);
        }
        
        //Test
        public static void main (String args[]) {
            System.out.println(checkIfStringExistsInFile("test.txt", "Hello World"));
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-20
          • 1970-01-01
          • 2015-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-10
          • 1970-01-01
          相关资源
          最近更新 更多