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

Thread: Get a new key in HashMap with values of another key.

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Question Get a new key in HashMap with values of another key.

    I have made a part of my program that needs to match something out of an arrayList with Hashmap key.
    When there is a match, I would like it to replace the key with the key from the ArrayList. (ArrayKey2)

    So I made up the folowing:
    Look if there is a similair key between key from multihashmap (mapFile1) and the ArrayKey2.
    If yes, then get the value out of the mapFile1.
    And put the ArrayKey2 with the values in the mapFile1 again.

    But I don't understand why it won't get in the Hashmap.

    With my MultiHashMap the keys are Strings, and the values Objects.

    // Get the keys from mapFile1. 
                    Set<String> keySet = mapFile1.keySet();
                    Iterator<String> keyIterator = keySet.iterator();
     
                    // Go through the keys one by one. 
                    while (keyIterator.hasNext()) {
                    	// Put the key in an Object. (It can't become a String right away.) 
                    	Object keyObj = keyIterator.next();
                    	// Put key in String. 
                    	String key = "" + keyObj; 
     
                    	for (int i = 0; i < ArrayListFileData2.size(); i++) {
                    		// Get the data out of the arrayList and put it in a String. 
        					String ArrayKey2 = ArrayListFileData2.get(i);; 
     
        					// Look if they match. 
        					if (key.equalsIgnoreCase(ArrayKey2)) {
        						//System.out.println("Loop key: " + key);
                        		//System.out.println("Loop ArrayKey2: " + ArrayKey2);
                        		System.out.println("Match: " + key + ArrayKey2);
     
                        		// Get the values from that certain key.
        						Collection values = (Collection) mapFile1.get(key);
        						Iterator valuesIterator = values.iterator();
     
                                while(valuesIterator.hasNext()) {
                                	Object ValueIT = "" + valuesIterator.next();
                                	System.out.println(ValueIT);
                                	//
                                	//mapFile1.put(ArrayKey2, ValueIT);
                                }
     
        					}
                    	}
                    }

    If I activate this lime: mapFile1.put(ArrayKey2, ValueIT)
    Then it says:

    Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    	at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    	at java.util.ArrayList$Itr.next(Unknown Source)
    	at SecondMatcherGridLayout.Matcher(SecondMatcherGridLayout.java:436)

    And this: at SecondMatcherGridLayout.Matcher(SecondMatcherGridL ayout.java:436) would be this line: Object ValueIT = "" + valuesIterator.next();
    The code as it is shown above works.
    But the problem is that when I activate mapFile1.put(ArrayKey2, ValueIT) I get the error.

    And I realy have no clue about why....


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Get a new key in HashMap with values of another key.

    I would like it to replace the key with the key from the ArrayList
    Not sure what that means. Are the two keys different? By replace I assume you mean you want to remove one key and add the other key with the same value that the first key had.
    I also assume that the matching is on the value not the key. For example: Say key1 has value ThisValue, you search the hashmap for a key with value=ThisValue (there could be more than one). When ThisValue is found, key1 is removed and key2 is added with value=ThisValue.

    java.util.ConcurrentModificationException

    have no clue about why
    Have you read the API doc for that error?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Get a new key in HashMap with values of another key.

    I now now why I have this error.

    Is there any way I can stop that while loop when
    if (key.equalsIgnoreCase(ArrayKey2))
    is runned?

    Or like to break this: Object keyObj = keyIterator.next();

    Becasue I realy nedd al those loops.
    And I already tried placing them in a diffrent row.
    That all didn't work. -_-
    Last edited by Purple01; September 20th, 2012 at 08:02 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Get a new key in HashMap with values of another key.

    To stop a loop from continuing to execute use the break statement to exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Get a new key in HashMap with values of another key.

    But then everything breaks.
    And that is also not what I want.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Get a new key in HashMap with values of another key.

    You will have to describe what you want to do and post all the code involved in the problem.
    The small pieces you have posted are not enough to understand the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Get a new key in HashMap with values of another key.

    Isn't there just a fiction to make in a HashMap a key diffrent?

    Like:
    (key) Index = (value) text
    (key) Max = (value) boy
    (key) Rose = (value) girl

    And then change it to:
    (key) Index = (value) text
    (key) Jonas = (value) boy
    (key) Rose = (value) girl

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Get a new key in HashMap with values of another key.

    Is that what I suggested in post#2?
    Say key1 has value ThisValue, you search the hashmap for a key with value=ThisValue (there could be more than one). When ThisValue is found, key1 is removed and key2 is added with value=ThisValue.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Purple01 (September 21st, 2012)

  10. #9
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Get a new key in HashMap with values of another key.

    Oh wait, I understand.
    So I have done that now, and it all goes a I wish it would.

    Thanks for the help Norm.
    Last edited by Purple01; September 21st, 2012 at 06:30 AM.

Similar Threads

  1. retrieve data from database using Hashmap (multiple values in one key)
    By ashin12 in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 06:22 AM
  2. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 03:48 AM
  3. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 09:27 PM
  4. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 08:44 PM
  5. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 09:58 PM