0

I have two different set of values.First set of values i put in a linkedhashmap and take it using a key.now i need to put and take the second set of values using same key.but both set of value are different and the parameters have same name...is it possible?

example: I get three parameters from server namely name,age,class i store it linkedhashmap and take it using name as the key.now iam going to get the second set of values with same parameters name,age,class in the same linkedhashmap..i need to take using the same name as key ..is it possible??

Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Feb 10, 2014 at 6:58
9
  • Considering that a hash map works on the assumption of unique keys, I doubt that this is going to work. Do you have any other unique piece of data which you could use? Commented Feb 10, 2014 at 7:01
  • I am not able to get the last part of your example. Can you just add specific example with what do you expect as output? Commented Feb 10, 2014 at 7:02
  • @Deepak name is key of map so make sure it will be unique otherwise value will be updated Commented Feb 10, 2014 at 7:02
  • Create a compsite of AllValues = <Values1,Values2> and then put in the map as key: AllValues pairs Commented Feb 10, 2014 at 7:19
  • @AnkurShanbhag I have hashmap A now i put three values in hashmap with some key.Also i get second set of values from server now also i store them in the same hashmap using the same key. wat ll happen? Commented Feb 10, 2014 at 7:23

1 Answer 1

0

You should define an object that has fields for each of the values you intend to store, e.g.:

public class Thing {
 private final String name;
 private final int age;
 private final String cls;
 // constructor, getters
}

Then you'd do something like this to add an element to your map:

map.put(name, new Thing(name, age, cls));

You can then get back a particular Thing by looking it up by name:

Thing found = map.get(name);
answered Dec 25, 2016 at 7:47
2
  • You've named an instance variable class Commented Dec 25, 2016 at 8:08
  • @pvg thanks, I caught that in the usage example but forgot to fix the class definition. You're welcome to make such trivial edits yourself, btw. Commented Dec 25, 2016 at 16:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.