Solved

MAP of multiple datatypes

Posted on 2009-04-03
3
644 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
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
wordcount challenge 11 78
Free Alternative to JIRA 4 58
HSSFWorkbook cannot be resolved error 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now