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

Thread: Change HashMap Type

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

    Default Change HashMap Type

    I was wondering is it posible to change the Type of a HashMap?

    Becasue lets say I have used an Api (Apache POI) to create a HashMap.
    Map<Cell, XSSFCell> mapFile1 = new LinkedHashMap<Cell, XSSFCell>();

    But later on in the program I work with a lot of strings, and I would like to be able to look a string up in the HashMap.
    Is there a way to make the same HashMap:

    Map<String, String> mapFile1 = new LinkedHashMap<String, String>();


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

    Default Re: Change HashMap Type

    Why do you need to use the same variable name? When you define a variable, the compiler saves its type for use later in the program. To have two variables with the same name but different types, you would need to define them in different scopes.
    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: Change HashMap Type

    So it would be possibel to covert the data from:

    Map<Cell, XSSFCell> mapFile1 = new LinkedHashMap<Cell, XSSFCell>();

    to

    Map<String, String> mapFile1String = new LinkedHashMap<String, String>();

    Becasue I find it very hard to work with this HashMap: Map<Cell, XSSFCell> mapFile1 = new LinkedHashMap<Cell, XSSFCell>();

    I can't even get the keys out there properly.

    Like .containsKey() won't even work on that HashMap.

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

    Default Re: Change HashMap Type

    Do the Cell class and the XSSFCell class have methods that will convert their contents to a useful String?
    If so loop through the keys of the first Map, get the value for each key, convert the key and value to String and put them into the second Map.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  2. 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
  3. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  4. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM
  5. How to change File Type of a File Using java
    By akash169 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 31st, 2010, 02:58 AM