2

I have HashMap with ArrayList as key and value as Integer, how can I get value from specific key.

Map< List<Object>,Integer> propositionMap=new HashMap<List<Object>,Integer>();  

my key are:[Brand, ID], [Launch, ID], [Model, ID], [Brand, UserModelNoMatch], [ProducerPrice, UserModelMatch], [ProducerPrice, ID]]
my values are:[3, 5, 4, 2, 1, 6]

In my program in several time in different place i need to find a specific value for the specific key. i do not want to use for loop evry time to get value. how can i do that?

8
  • 12
    this is a bad idea. Using collections as keys is rarely a good idea Commented Aug 6, 2013 at 10:43
  • It's going to be pretty difficult. Commented Aug 6, 2013 at 10:43
  • Seeing as how you use it, you really might want to make separate classes for Brand, Launch, Model and ProducerPrice. Commented Aug 6, 2013 at 10:45
  • 3
    I guess you have some severe issue with your data model if you want to map a whole list of objects to an Integer. Is this an index? Elaborate on your underlying problem, probably there is a better solution than putting lists in maps as a key. Commented Aug 6, 2013 at 10:46
  • 2
    @SomeshMukherjee Using mutable collections as keys is a bad idea. Commented Aug 6, 2013 at 10:57

3 Answers 3

4

Putting aside that this is a bad idea (as described in the comments), you don't need to do anything special:

List<Object> list = new ArrayList<Object>();
// add objects to list

Map<List<Object>,Integer> propositionMap = new HashMap<List<Object>,Integer>();  
propositionMap.put(list, 1);
Integer valueForList = propositionMap.get(list); // returns 1

You can get the same value when constructing a list independently:

List<Object> list2 = new ArrayList<Object>();
// add the same objects (by equals and by hashcode) to list2 as to list

Integer valueForList = propositionMap.get(list2); // returns 1

But you need to be careful not to change the list after you use it as a key in the map!

list.add(new Object());
Integer valueForList = propositionMap.get(list); // likely returns null

Again, it's very likely a bad idea.

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

Comments

0

Seeing as how you want the same behavior, I strongly recommend using an interface with classes.

public interface Proposition
{
    public int getID();
}

public class Brand implements Proposition
{
    private int id;

    public Brand(int _id_)
    {
        this.id = _id_;
    }

    public int getID()
    {
        return this.id;
    }
}

public class Launch implements Proposition
{
    private int id;

    public Launch(int _id_)
    {
        this.id = _id_;
    }

    public int getID()
    {
        return this.id;
    }
}

public class ProducerPrice implements Proposition
{
    private int id;
    private int UserModelMatch;

    public ProducerPrice(int _id_, int _UserModelMatch_)
    {
        this.id = _id_;
        this.UserModelMatch = _UserModelMatch_;
    }

    public int getID()
    {
        return this.id;
    }

    public int getUserModelMatch()
    {
        return this.UserModelMatch;
    }
}

And then using a hashmap for proposition objects

Map<Integer, Proposition> propositionMap = new HashMap<Integer, Proposition>();

Proposition newprop = new ProducerPrice(6, 1);
propositionMap.put(newprop.getID(), newprop);

Proposition someprop = propositionMap.get(6);

if (someprop instanceof ProducerPrice)
{
    ProducerPrice myprodprice = (ProducerPrice)someprop;
    // rest of logic here
}

Comments

0

You can get the value as usual way :

propositionMap.get(arrayListN)

until you modify the list itself after adding.

Comments

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.