0

I am trying to calculate the load factor of my Hashtable and print out the result. However, when I run this, the load factor is equal to 2. This is currently the size of my map - so my method loadFactor is not quite working. Is there something obvious I am missing? ( i am fairly new to Java)

import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Main { Student student; Hashtable next; public Hashtable<String,Student> studentMap; public static void main(String[] args){ Hashtable<String, String> studentMap = new Hashtable<>(10000, 0.75f); studentMap.keySet().forEach((key) -> { String value = studentMap.get(key); System.out.println("Key = " + key + ", Value = " + value); }); //adding values to array studentMap.put("16012804", "Jennifer"); studentMap.put("13747732", "Beatrice"); studentMap.put("14058392", "Bob"); Set set = studentMap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mapentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mapentry.getKey() + " & Value is: "); System.out.println(mapentry.getValue()); } //Get values based on key String var= studentMap.get("16012804"); System.out.println("Value at index 1 is: "+var); // Remove values based on key studentMap.remove("16012804"); System.out.println("Map key and values after removal:"); Set set2 = studentMap.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry mapentry2 = (Map.Entry)iterator2.next(); System.out.print("Key is: "+mapentry2.getKey() + " & Value is: "); System.out.println(mapentry2.getValue()); } Set keyset = studentMap.keySet(); System.out.println("Key set values are:" + keyset); boolean val = studentMap.isEmpty(); System.out.println("Is hash map empty: " + val); System.out.println("Size of the Hashtable " + studentMap.size()); System.out.println("Load Factor: " + loadFactor(studentMap)); System.out.println("Hash: " + studentMap.hashCode()); System.out.println("Size of map is now: " + mapcapacity(studentMap)); } public static float loadFactor(Map studentMap){ int count=0; float load; for(int i=0; i<studentMap.size(); i++){ count += studentMap.size(); } load = count/(float)studentMap.size(); return load; } //if the size of the map is greater than the map capacity * load factor - then double the size of map. public static Integer mapcapacity(Map studentMap){ Integer initCapacity=2; float loadFactor=0.75f; boolean capacityFound=false; Integer capacity=initCapacity; Integer size=studentMap.size(); while(!capacityFound){ if(size>capacity*loadFactor){ capacity=capacity*2; } else { capacityFound=true; } } return capacity; } public int hash(String key){ return (Math.abs(key.hashCode())) % studentMap.size(); } } 
10
  • You know that for(int i=0; i<studentMap.size(); i++){ count += studentMap.size(); } is the same as count = studentMap.size() * studentMap.size() right ? what are you trying to do ? this is definitely not calculating the load factor... Commented Mar 6, 2017 at 18:30
  • oh dear maybe i'm a long way off ... I am trying to calculate the load factor of the size of the studentMap, which is my hash table Commented Mar 6, 2017 at 18:34
  • and how can you calculate anything like that ??? Commented Mar 6, 2017 at 18:36
  • 1
    You literally can't get the load factor of a java.util.Hashtable. It doesn't expose any API for getting the size of the hash table, just the number of distinct entries that have been put in the table. Commented Mar 6, 2017 at 18:39
  • 1
    Louis is right, it sounds like you're trying to compute size/capacity only that there is no way for you to know what is the capacity. Further, all we know is that size/capacity <= loadFactor - and in most cases it'll be < - so even that doesn't give you the load factor. Commented Mar 6, 2017 at 18:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.