0

I am trying to instantiate a map structure with the following

Map<Timestamp, Test> map = new Map<Timestamp, Test>();

where Test is a class with 3 different types of variables and Timestamp is a java.sql.Timestamp type.

But I am getting the following error

Can not instantiate type Map<Timestamp, Test>

My primary objective is to create a map structure where I can store multiple values/objects (of different types) from a Class implementation under the same timestamp key.

2

5 Answers 5

9
  1. Map<Timestamp, Test>

    You can't instantiate Map because it is interface. You need to do use one of the implementations like HashMap.

  2. You can't store multiple values in HashMap, for same Key unless values are either collection of objects (or) array. Another alternative is Google MultiMap

Sign up to request clarification or add additional context in comments.

4 Comments

using HashMap now I am getting the following error: HashMap cannot be resolved to a type any suggestion, pls?
@JoarderKamal:Make sure imports are there.
Just wondering, how can I now put multiple values using the Test class variables (I have 3 variables of different types in Test). Any guidance kindly?
@JoarderKamal: I would suggest to add all 3 instances of Test to either ArrayList (or) HashSet etc., and add this collection to your map.
3

YOu can't instantiate an Interface.

Use HashMap on the righthand side

Comments

3

Map is an interface. You cannot instantiate an interface.

You need to use a class that implements the Map interface. Have a look here.

Comments

2

Do this:

Map<Timestamp, Test> map = new HashMap<Timestamp, Test>();

Comments

2

Do this

Map<Timestamp, Test> map = new HashMap<Timestamp, Test>();

instead of

Map<Timestamp, Test> map = new Map<Timestamp, Test>();

as you cannot instantiate the interface Map

The other thing you have mentioned that you want to store the values of different types, so use Object as the value instead of Test:

    Map<Timestamp, Object> map = new HashMap<Timestamp, Object>();

1 Comment

Thanks for the guide. No more error message now. Btw, I actually wanted to use multiple values of different types not a single Object (of different type).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.