DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: HashMap

  1. #1
    Join Date
    Oct 2003
    Posts
    2

    Arrow HashMap

    Hi,
    i'm trying to edit a hashmap in a method, i can get it to return the non-key value (using get() ) but it returns it as an object and i'm strugling to convert in into an integer to edit it. I don't know if i'm being an idiot (more then likely ) or not.

    If anyone could help i'd be very grateful

    Andy

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    You need to cast objects can come out of HashMap:
    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    public class Test {
            public static void main(String[] args) {
                    Map myMap = new HashMap(); // Program by interfaces.
                    myMap.put("One", new Integer(1));
                    myMap.put("Two", new Integer(2));
                    Integer associatedWithOne = (Integer)myMap.get("One"); // Note the cast
                    Integer associatedWithTwo = (Integer)myMap.get("Two"); // Note the cast
                    System.out.println(associatedWithOne);
                    System.out.println(associatedWithTwo);
            }
    }
    It seems a little strange to begin with, but you soon get used to it (the next version of Java will make this much nicer).

    Note - you can't put primitives (int, char, long) directly into a HashMap, you have to put them in 'wrapper' classes (Integer, Character, Long).
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Oct 2003
    Posts
    2
    ok cheers for that, got one error less now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links