?
Solved

MAP of multiple datatypes

Posted on 2009-04-03
3
Medium Priority
?
651 Views
Last Modified: 2013-11-23
Hi,

I'm new to JAVA and I'm trying to achieve something that the JAVA Map seems to be the solution for, but I'm hitting a wall. Basically I want to have a MAP of values BUT with different data types.  That doesn't work (maybe I'm doing something wrong) as it asks me to declare the Map parameterized.

Below is a sample snippet of what I want to do.

Note: I know I can use the Memento pattern to achieve this, but I'm wondering if this is at all possible.
public class MyClass{
 
protected Map instance;
 
  public void setName( String sName ){
    this.instance.put("sName",sName);
  }
  public void setDate(Date date){
    this.instance.put("date", date)
  }
  public Map getInstanceState(){
    return Collections.unmodifiableMap(this.instance);
  }
}

Open in new window

0
Comment
Question by:lostlinkpr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 24066456
Why not just use a HashMap? (Attached is an example)

import java.util.HashMap;
 
public class Ex
{
   public Ex()
   {
      HashMap hm=new HashMap();
      hm.put("sn","InteractiveMind");
      hm.put("size",new Integer(200));
      
   }
   
   public static void main(String[]a)
   {new Ex();}
}

Open in new window

0
 
LVL 1

Author Comment

by:lostlinkpr
ID: 24067826
Thank you, that works!  The compiler (Eclipse) still throws a warning saying the HashMap should be parameterized but it works well.

final snippet:



protected HashMap inst = new HashMap();
 
    /*
     * Get the state of the object properties at a specific moment
     * 
     * @return unmodifiable SortedMap (memento of object)
     */
    public Map getMemento(){
    	return Collections.unmodifiableSortedMap( new TreeMap(this.inst) );
    }

Open in new window

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24067977
Yes, from Java 5 onwards, parametrisation is available (and thus recommended by the compiler), but it's not necessary, just good coding practice (when viable).
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question