【问题标题】:How to Make a HashMap Key Case Insentive?如何使 HashMap 键不区分大小写?
【发布时间】:2014-04-13 02:39:40
【问题描述】:

我有一个关于 HashMaps 的作业,我应该使用以下测试器类,但我在 2 行中出现错误:

for(Map.Entry entry:salary) and HashMap accounts = new HashMap(new CaseInsensitive());

我正在使用 Eclipse 顺便说一句

import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;

public class Assignment {

class CaseInsensitive implements HashingFunction<String> {

   public int hashCode( String key ) {      
    return key.toLowerCase().hashCode();
   }

   public boolean equals( String lhs, String rhs ) {        
    return lhs.equalsIgnoreCase(rhs); 
   }    
}

void test1()  {

    System.out.println("Test 1:");
    HashMap<String, Double> salaries = new HashMap<>();

    // test #1a: verify that identical keys allow the same map entry to be replaced.
    salaries.put( "1000", 55000.0 );
    salaries.put( "1000", 45000.0 );
    System.out.println( "New value is " + salaries.get("1000"));

    // test #1b: add several items and display the map as a string
    salaries.put( "1234", 25000.0 );
    salaries.put( "2001", 43000.0 );
    salaries.put( "2010", 67000.0 );
    salaries.put( "2020", 37000.0 );
    salaries.put( "3010", 57000.0 );
    salaries.put( "3020", 87000.0 );

    // test #1c: test the size and toString operations
    System.out.println("\nPrinting with toString():");
    System.out.println( salaries.toString() );
    System.out.printf("Size = %d\n", salaries.size() );

    // test #1c: use the iterator
    System.out.println("\nPrinting with an iterator loop");
    for( Map.Entry<String, Double> entry : salaries ) {
        System.out.print( entry.toString() + " " );
    }
    System.out.println();
}

void test2()  {
    // test 2a: Implement a custom hashing function that ignores case differences
    System.out.println("\nTest 2:");    
    HashMap<String, Double> accounts = new HashMap<>(new CaseInsensitive());
    accounts.put( "AbCDef", 45000.0 );
    accounts.put( "abcdEF", 25000.0 );  // final value
    accounts.put( "aabCDE", 43000.0 );
    accounts.put( "AABcde", 67000.0 );  // final value
    accounts.put( "BDDEfg", 37000.0 );
    accounts.put( "bddEFG", 57000.0 );  // final value

    // Test 2b: Show that a case-insensitive insertion was used. 
    System.out.println("\n" + accounts.toString() );
    // Expected output: [AbCDef,25000.0], [aabCDE,67000.0], [BDDEfg,57000.0]

    // Test 2c: verify that the get function also ignores case differences:
    System.out.printf("\nLooking for abcdEF and AbCDef: %.2f = %.2f\n",
            accounts.get("abcdEF"), accounts.get("AbCDef"));
    // Expected output: the values are equal.
}

void test3() {
    // test 3: just making sure you created a generic HashMap class
    System.out.println("\nTest 3:");    
    HashMap<Integer, List<String>> courses = new HashMap<>();
    courses.put(1130, new LinkedList<String>());
    courses.get(1130).add( "COP 3337" );
    courses.get(1130).add( "MAD 3105" );
    courses.get(1130).add( "PHY 2048" );
    courses.get(1130).add( "CDA 3033" );

    courses.put(1131, new LinkedList<String>());
    courses.get(1131).add( "COP 3530" );
    courses.get(1131).add( "CDA 4101" );
    courses.get(1131).add( "PHY 2049" );
    courses.get(1131).add( "ENC 2301" );
    System.out.println(courses);
}

public static void main(String[] args) {

    Assignment app = new Assignment();
    app.test1();
    app.test2();
    app.test3();    
}

}

【问题讨论】:

  • 您的问题是什么?另外,为什么不使用包含大小写字母混合的String 键进行测试?
  • 您遇到了什么错误?
  • 据我所知,没有一个 JDK 有一个 java.util.HashMap 接受 HashingFunction 类型的构造函数参数(另一种非标准类型)。您确定您应该使用内置的java.util.HashMap,而不是为此作业提供的吗?也许这是一个简单的错误import 问题。

标签: java hash hashmap hashtable hashcode


【解决方案1】:

Java 哈希图不将“哈希函数”作为其构造函数的参数。

看起来您的意图是说“嘿,对于这个哈希映射,我希望您对键使用哈希函数,以便其哈希码是字符串小写版本的现有哈希码。”但这不是你在 Java 中的做法。

如果你想走这条路,你必须创建一个名为 MyString 的类,它包装一个字符串并覆盖你在尝试中所做的哈希码。 (您不能覆盖 Java 的 String,因为它是最终类。)

您的哈希图将采用HashMap&lt;MyString, Double&gt; 的形式。但这将使测试无效,除非您将HashMap 子类化,以便putget 接收字符串。如果任务是这样的话,我想你必须一起玩。

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 2013-03-22
    • 2011-10-14
    • 1970-01-01
    • 2010-09-23
    • 2013-10-06
    • 2013-03-06
    • 1970-01-01
    相关资源
    最近更新 更多