Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Hashtable loadFactor

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hashtable loadFactor

    So I have this hash constructor:
    class chainingHash<String>{
     
    	private static int DEFAULT_TABLE_SIZE = 101;
    	private List<String>[] theList;
    	static int currentSize;
     
            public chainingHash(int size){
    		theList = new LinkedList[nextPrime(size)];
    		for(int i = 0; i < theList.length; i++){
    			theList[i] = new LinkedList<>();
    		}
    	}
    }
    What I want to know, is how can I change the loadFactor of the hash. All I know is that the default loadFactor is .75 but what if I wanted it to be something like .25. How would I be able to introduce the parameter float loadFactor into the constructor?
    Here is my main method too:
    public static void main(String[] arg){
                    chainingHash s = new chainingHash(10);
    		for(int i = 0; i < 10; i++){
    			String word = words[(int)(Math.random()*nLine)];
    			s.insert(word);
    		}
    		s.print()
    };

    The array words is just where I have a bunch of random words. Any help would be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Hashtable loadFactor

    the parameter float loadFactor into the constructor
    Are you asking how to pass a float value to a constructor?
    The current code passes an int to the constructor. Use that as a template to code a float value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hashtable loadFactor

    I guess what I'm asking is how can I have a custom loadFactor that I can change as much as I want. I'm trying to test how hashtables behave differently with different loadFactors. In order to do that, I need to be able to change it. So like the constructor would be something like:
    public chainingHash(int size, float loadFactor){
    		//type code here
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Hashtable loadFactor

    Yes, that looks like the way to pass a float value to the constructor.

    To change a value in an existing instance of the class, you would need a setter method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. PLEASE HELP! (hashtable in Java)
    By hedyeh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2013, 06:28 AM
  2. hashtable dilema
    By Javanoob12 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2012, 02:29 PM
  3. [SOLVED] Question about hashtable
    By leonne in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 11:09 AM
  4. I need help with Enumeration in hashtable
    By jaouharia1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 08:17 PM

Tags for this Thread