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 10 of 10

Thread: HashMap

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default HashMap

    When learning HashMaps in C++ I had to create the whole algorithm. In the code I created I could simply place a string into the method and it would store the names for me by turning the string into a integer and storing is accordingly. If there was a collision it would grow linearly at that location.

    //play with Hash Tables
    	void getNames(String names)
    	{
    		HashMap<String, Integer> map = new HashMap<String, Integer>();
     
    		map.put(names,22);
    	}

    How can I do this in Java. I read about them and look at examples and they all for the most part look like this.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap

    Are you trying to write your own version of a HashMap class
    or use the version in Java SE?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: HashMap

    I was wondering if I was over looking something and there was a way to just simply send in a name and place it in a hashTable. Perhaps send in a Node and index it by a string value inside of it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap

    Start by reading the API doc for the class. If you have any questions about what it says there, copy the text that is the problem and paste it here with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: HashMap

    I dont understand the question either. What exactly do you want to do and which part of it is the problem?

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: HashMap

    I think this answered my question. Fired at the hip at this one.

    //play with Hash Tables
    	void setNames(Node node)
    	{
    		HashMap<Node, Integer> map = new HashMap<Node, Integer>(22);
     
    		map.put(node,Integer.parseInt(node.name));
    	}
     
    	class Node
    	{
    		String name;
    		String age;
    	}


    --- Update ---

    Is it possible what I am trying to do, I don't understand why it would not accept the index of a string as the key to the bucket.

     
    private HashMap< Integer,String> map = new HashMap<Integer,String>(22);
     
    //play with Hash Tables
    	void setName(String name)
    	{
    		map.put(Integer.parseInt(name),name);
    	}
     
    	String findName(String name)
    	{
     
    		String n = map.get(name);
     
    		return n;
    	}
     
     
    	public static void main(String[] args)
    	{
    		TestButton test = new TestButton();
     
    		test.setName("drew");
    		test.setName("Mike");
    		test.setName("SOA");
     
    		String name = test.findName("drew");
     
    		System.out.println(name);
     
    	}

    Error
    Exception in thread "main" java.lang.NumberFormatException: For input string: "drew"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at testButtonsLoop.TestButton.setName(TestButton.java:72)
    	at testButtonsLoop.TestButton.main(TestButton.java:89)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap

    java.lang.NumberFormatException: For input string: "drew"
    "drew" is not a valid String of numeric digits for the parse method. "12324" is a valid String

    What are the key values you want to use to access the HashMap to retrieve the Node as the value.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: HashMap

    I want to take the name and turn it into a integer value 1-22 since my HashMap is size of 22.

    Something like this but I know it does't work...

    int value = (int)name % 22;
    Last edited by jocdrew21; October 2nd, 2014 at 12:33 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap

    That makes it look like you are trying to create a hash map.
    The HashMap class takes care of doing the hashing. The user provides a key|value pair with the put and can get access to the value by passing the key to the get method.

    Go back and read the API doc for HashMap
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: HashMap

    What exactly would get from that? Why would you want to map a string to itself?

Similar Threads

  1. Hashmap
    By srkmca07 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2013, 07:36 AM
  2. Hashmap
    By ryan12345 in forum Java Theory & Questions
    Replies: 6
    Last Post: November 2nd, 2012, 07:04 PM
  3. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:44 PM
  4. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  5. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM