Java 正确的使用方法;哈希表;?

Java 正确的使用方法;哈希表;?,java,hashtable,Java,Hashtable,我对哈希表还不熟悉,正在努力了解它们是如何充分工作的。我需要确定一个字符串是否出现在一个包含大约100000个字符串的大文件中,每个字符串都在各自的行上。有人告诉我,哈希表要比LinkedList或ArrayList更高效,运行时两者都是O(n) 我已经研究了Java中的HashTable类,但是由于“put”方法需要一个键和一个对象,我不知道如何准确地输入文件中的每个字符串 我想我可以使用扫描器扫描文件中的每个字符串,但如何将它们输入哈希表,以及如何在哈希表中使用contains()方法?对我

我对哈希表还不熟悉,正在努力了解它们是如何充分工作的。我需要确定一个字符串是否出现在一个包含大约100000个字符串的大文件中,每个字符串都在各自的行上。有人告诉我,哈希表要比LinkedList或ArrayList更高效,运行时两者都是O(n)

我已经研究了Java中的HashTable类,但是由于“put”方法需要一个键和一个对象,我不知道如何准确地输入文件中的每个字符串


我想我可以使用扫描器扫描文件中的每个字符串,但如何将它们输入哈希表,以及如何在哈希表中使用contains()方法?

对我来说,这听起来像是一个。看看这个


您只在中输入以不同方式存储的值(无加倍值)。然后,您可以调用
contains
方法,检查字符串是否在集合中。

对我来说,这听起来像是一个用例。看看这个


您只在中输入以不同方式存储的值(无加倍值)。然后,只需调用
contains
方法,检查字符串是否在集合中。

只需将字符串放入
哈希集合中即可

Set yourStrings = new HashSet<String>();

for (String line : yourFile) {
    yourStrings.add(line);
}

您只需将字符串放入
哈希集

Set yourStrings = new HashSet<String>();

for (String line : yourFile) {
    yourStrings.add(line);
}

Hashtable
非常古老,已被
HashMap
取代。还有
HashSet
。两者都使用哈希表,但用途不同。当您希望将某种类型的值与每个键关联时,将使用
HashMap
;例如,您可以使用它查找某人的姓名并获取其电话号码。但是,
HashSet
只存储键,不包含任何值。您可以使用它向集合中添加名称,然后稍后检查名称是否在集合中


正如Luiggi在评论中提到的,
HashMap
HashSet
只是
Map
Set
的具体实现;这些实现使用哈希表,但这些类的其他实现也可用。在构造表时,需要使用
HashMap
HashSet
,但通常应该将变量声明为
Map
Set
,因为这样可以用实现相同方法的其他类替换
HashMap
HashSet
。这样您就不必依赖于特定的实现。

Hashtable
非常古老,已经被
HashMap
取代。还有
HashSet
。两者都使用哈希表,但用途不同。当您希望将某种类型的值与每个键关联时,将使用
HashMap
;例如,您可以使用它查找某人的姓名并获取其电话号码。但是,
HashSet
只存储键,不包含任何值。您可以使用它向集合中添加名称,然后稍后检查名称是否在集合中


正如Luiggi在评论中提到的,
HashMap
HashSet
只是
Map
Set
的具体实现;这些实现使用哈希表,但这些类的其他实现也可用。在构造表时,需要使用
HashMap
HashSet
,但通常应该将变量声明为
Map
Set
,因为这样可以用实现相同方法的其他类替换
HashMap
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"));
}

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.HashSet;
导入java.util.Scanner;
导入java.util.Set;
公共类MyFileUtils{
//这可以省略,只是为了提高速度而添加
//当需要在同一文件中进行多次搜索时,避免重新计算。
//仅当您只需要在一个文件中搜索时才使用它
私有静态集stringLines=null;
/*
*获取文件中所有(不同)行的哈希集
*/
公共静态集getStringLinesFromFile(字符串文件路径){
//这可以省略,只是为了支持此函数的快速多次调用而添加
如果(stringLines!=null)返回stringLines;
Set stringLines=new HashSet();
扫描器=空;
试一试{
扫描器=新扫描器(新文件(文件路径));
while(scanner.hasNextLine())
stringLines.add(scanner.nextLine());
}catch(filenotfounde异常){
System.out.println(“文件不存在”);
}最后{
如果(扫描器!=null)
scanner.close();
}
//作为第一行,这可以省略,只是为了支持此函数的快速多次调用而添加
MyFileUtils.stringLines=stringLines;
返回纵梁;
}
/*
*调用此方法在文件中搜索字符串
*/
公共静态布尔checkIfStringExistsInFile(字符串文件路径、字符串搜索行){
返回getStringLinesFromFile(filePath).contains(aStringLine);
}
//试验
公共静态void main(字符串参数[]){
println(checkIfStringExistsInFile(“test.txt”、“helloworld”);
}
}

您需要一个哈希集来存储文件的每一行

*只有当您对文件中每个字符串的出现次数感兴趣时,才可能需要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"));
}

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.HashSet;
导入java.util.Scanner;
导入java.util.Set;
公共类MyFileUtils{
//这可以省略,只是为了提高速度而添加
//当需要在同一文件中进行多次搜索时,为避免重新计算