Results 1 to 8 of 8
  1. #1
    pandeyalok is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default How to create a new HashMap from a HashMap entries of other methods

    I am trying to make a new HashMap from the HashMap entries of the methods. Need to add the entries of a HashMap inside the methods to make a new HashMap.

    Java Code:
    public class Test {   
      private static HashMap mp;   
    public static void main(String[] args) {   
      
    for (some criteria){   
      method.invoke() // invoke methods written below one by one   
    } 
    }  
      
    void Method_1(){   
      mp.put(num1, "sfjbs");   
    }   
    void Method_2(){   
      mp.put(num2, "sfhvs");   
    }   
    void Method_3(){   
      mp.put(num3, "sfshb");   
    }   
    void Method_4(){   
      mp.put(num4, "sjnfs");   
    }   
    }
    Can anyone please suggest any way to do that. Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    There's a constructor in the HashMap class that can do it.

    kind regards,

    Jos

  3. #3
    pandeyalok is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    There's a constructor in the HashMap class that can do it.

    kind regards,

    Jos
    > Could you please elaborate a little. I have tried .putAll but its not working
    Thanks!!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pandeyalok View Post
    > Could you please elaborate a little. I have tried .putAll but its not working
    Thanks!!
    Did you actually read my reply and/or the API documentation for the HashMap class? I wrote: the HashMap class has a constructor that can do it, meaning this:

    Java Code:
    HashMap original = ...
    HashMap copy= new HashMap(original);
    kind regards,

    Jos

  5. #5
    pandeyalok is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Did you actually read my reply and/or the API documentation for the HashMap class? I wrote: the HashMap class has a constructor that can do it, meaning this:

    Java Code:
    HashMap original = ...
    HashMap copy= new HashMap(original);
    kind regards,

    Jos
    Actually what I am trying to do is

    Java Code:
    Map mp= new HashMap();
    mp.put(1, "abc");
    mp.put(2, "def");
    mp.put(3, "ghj");
    
    Map mp1 = new HashMap();
    mp1.putAll(map);
    From above, the first block of code (for mp) is in one method (like this I have several methods in which I udpate mp using method.invoke() ).(see original post)

    And I am trying for a new HashMap "mp1" and it should contain entries of mp from all the methods after "for loop" for invoke is complete - basically a copy of complete mp.)
    The example you have given will create a new copy of mp after each invoke.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Java Code:
    Map mp1 = new HashMap(mp);
    ?

    That's all JosAH is saying...
    I'm not entirely clear on what you're saying...which probably isn't helped by you supplying us with code that does nothing at the moment. As in I expect it bears no relationship to the code you;re working on.

  7. #7
    pandeyalok is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Java Code:
    Map mp1 = new HashMap(mp);
    ?

    That's all JosAH is saying...
    I'm not entirely clear on what you're saying...which probably isn't helped by you supplying us with code that does nothing at the moment. As in I expect it bears no relationship to the code you;re working on.
    Thanks JosAH & Tolls!!
    I have tried to build the logic here with some working code.
    Need your suggestion on the same.

    Java Code:
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
     
    public class TestHash {
    	public static HashMap hashTest;
    	public static HashMap hashTest1;
    	private static Class classObj = null;
    	static Method method=null;
    	static Class[] param=null;
    	classObj = this.getClass();
    	
    	public void method1(){
    		hashTest1 = new HashMap();
    		hashTest.put(1, "a");
    	}
    	public void method2(){
    		hashTest1 = new HashMap();
    		hashTest.put(2, "b");
    	}
    	public void method3(){
    		hashTest1 = new HashMap();
    		hashTest.put(3, "c");		
    	}
    	public 	void method4(){
    		hashTest1 = new HashMap();
    		hashTest.put(4, "d");
    	}
     
    	public static void main(String[] args){
    		hashTest1 = new HashMap();
     
    		//Logic to invoke methods above based on some criteria - lets say method1 and method3
    		for(int count=0;count<10;count++){
    			method=classObj.getMethod("method1",param);
    			method.invoke(this, args);
    		}
     
    		hashTest1.putAll(hashTest);
    		
    		Iterator it = hashTest1.entrySet().iterator();
    		
    		while(it.hasNext()){
    			Map.Entry m = (Map.Entry) it.next();
     
    			String key = m.getKey().toString();
    			String value= (String) m.getValue();
     
    			Logger.message(TestHash.class, "key: "+key+"  value: "+value);
    		}
    	}
    }

    Here I am getting java.lang.NullPointerException for .putAll().
    Here it is possible that only method1(), method3() are only invoked (based on some logic).
    So I want 1=a and 3=c to be in my new HashMap hashTest1. But result here is null;
    I hope you can figure out what exactly I am trying to do here.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    That code is a mess: you are creating a new HashMap for hashtest1 in every method )so the old HashMap is lost) while you are trying to assign a tuple to hashtest (which is null).

    kind regards,

    Jos

Similar Threads

  1. Hashmap to Methods
    By hotinlavacoolinjava in forum New To Java
    Replies: 7
    Last Post: 09-08-2009, 02:55 AM
  2. HashMap Help
    By ScaryJello in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 09:18 AM
  3. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 08:08 PM
  4. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •