java - How to Make a HashMap Key Case Insentive? -


i have homework hashmaps , i'm supposed use following tester class, mistake in 2 lines:

for( map.entry entry : salaries ) , hashmap accounts = new hashmap<>(new caseinsensitive());

i'm using eclipse btw

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 identical keys allow same map entry replaced.     salaries.put( "1000", 55000.0 );     salaries.put( "1000", 45000.0 );     system.out.println( "new value " + salaries.get("1000"));      // test #1b: add several items , display map 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 size , tostring operations     system.out.println("\nprinting tostring():");     system.out.println( salaries.tostring() );     system.out.printf("size = %d\n", salaries.size() );      // test #1c: use iterator     system.out.println("\nprinting iterator loop");     for( map.entry<string, double> entry : salaries ) {         system.out.print( entry.tostring() + " " );     }     system.out.println(); }  void test2()  {     // test 2a: implement custom hashing function 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 case-insensitive insertion used.      system.out.println("\n" + accounts.tostring() );     // expected output: [abcdef,25000.0], [aabcde,67000.0], [bddefg,57000.0]      // test 2c: verify function ignores case differences:     system.out.printf("\nlooking abcdef , abcdef: %.2f = %.2f\n",             accounts.get("abcdef"), accounts.get("abcdef"));     // expected output: values equal. }  void test3() {     // test 3: making sure created 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();     } 

}

java hashmaps not take "hashing function" argument constructor.

it looks intent "hey, hash map, want use hashing function key hash code existing hashcode lowercase version of string." that's not how in java.

if want go route, have make class called mystring wraps string , overrides hash code did in attempt. (you cannot override java's string because final class.)

your hashmap of form hashmap<mystring, double>. invalidate test unless subclassed hashmap put , get took in strings. if assignment is, guess have play along.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -