Results 1 to 8 of 8
  1. #1
    apurv291 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Can some one help me in Writing Java code using Hashmap?

    I need help in writing java code using Hashmap as I need to first store data from three Different file into hash map and then write it into a excel.

    File 1: Yr2001
    Employee Salary Drawn
    ======= ==========
    A
    100
    B
    200
    C
    300

    File 2: Yr2002
    Employee Salary Drawn
    ======= ==========
    A
    200
    B
    300
    C
    400

    File3: Yr2003
    Employee Salary Drawn
    ======= ==========
    A
    300
    B
    400
    C
    500

    All these Files need to be read and stored into a HashMap to rewrite them into an Excel like this:
    Employee Yr2001Sal Yr2002Sal Yr2003Sal
    ======= ======= ======= =======
    A
    100 200 300
    B
    200 300 400
    C
    300 400 500

    Please suggest how to use Hashmap to store it as I am new to java and does not know too much about hashmap.

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Can some one help me in Writing Java code using Hashmap?

    What have you tried? Googling "java how to use hashmap" generates a ton of promising results.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can some one help me in Writing Java code using Hashmap?

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Can some one help me in Writing Java code using Hashmap?

    In Java, every Object has a function int hashCode() that, when overwritten, should return a *somewhat* unique integer (or at least pretty well distributed so that there are as few repetitions as possible). So, in Objects you want to put into a HashMap, you should override the hashCode() method (AND the equals method!). Most common classes already implement these methods (e.g. Integer, String, etc.). As for using them, a Map is just a set of key-value pairs that maps one set of Objects (keys) to another (values). So, a Map<Integer, String> would map Integers to Strings.

    Java Code:
    public static void main(String args[])
    {
    	Map<Integer, String> numbers = new HashMap<Integer, String>();
    	
    	// add key-value pairs to the map
    	numbers.put(1, "one");
    	numbers.put(2, "two");
    	numbers.put(3, "three");
    	numbers.put(4, "four");
    	
    	// getting value from a key
    	System.out.println(numbers.get(2));
    	
    	// iterating through keys
    	for(Integer i : numbers.keySet())
    	{
    		System.out.println(i + "   " + numbers.get(i));
    	}
    }

  5. #5
    apurv291 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Can some one help me in Writing Java code using Hashmap?

    well i have tried to put single value foe key.
    But i don't kknow to add multiple value for same key

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can some one help me in Writing Java code using Hashmap?

    add multiple value for same key
    A way to do that would be for the value to be a list that can contain many objects.
    Create the list with the first object and add it to the Map using the key. As more objects are found for the same key, get the list from the Map and add the new object to the list.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    apurv291 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Can some one help me in Writing Java code using Hashmap?

    thanks andrew however my scenario is different.

    for single value-key, i know how to use hash map.
    i want to know, how to use hashmap for multi-value/key pair

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Can some one help me in Writing Java code using Hashmap?

    Quote Originally Posted by apurv291 View Post
    thanks andrew however my scenario is different.

    for single value-key, i know how to use hash map.
    i want to know, how to use hashmap for multi-value/key pair
    Norm just told you how to do that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  2. New to Java. Need help writing code. (Classes and Objects)
    By AustinStanley in forum New To Java
    Replies: 6
    Last Post: 11-09-2012, 11:19 AM
  3. help writing a java code
    By joshsmith3400 in forum Forum Lobby
    Replies: 1
    Last Post: 02-02-2011, 07:06 AM
  4. Need help on writing Java source code
    By k4it0xtr3me in forum New To Java
    Replies: 29
    Last Post: 01-24-2011, 03:56 PM
  5. Is this the right way of writing html code in java??
    By raju.i in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2010, 08:05 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
  •