Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store two similar values

 
shalu sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

my requirement is to store key-value pairs having 2 exactly same keys but different values. eg: storing (2, sandeep) and (2, deepak). Is there any collection class or built-in datatypes in java which provides this functionlaity.

I checked HashMap.. but it simply overwrites the earlier value.

Thanks
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about storing an array in a collection.
 
shalu sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u be a bit more elaborate..
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shalu sharma:
can u be a bit more elaborate..


He may have meant you can store arrays in a different Collection such as an ArrayList. You'd have no key, but you do have an index.
someArrayList.add(new String[]{"2", sandeep}) ?

You can't have keys and have duplicates keys, it's just not possible.
So either your key is modified somehow to ensure it's unique or you switch to a collection that stores duplicates or you use an array in a collection, actually there are a number of other possible solutions but you'll need to give some more info.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalu,
Keys cannot be duplicated in Hashtable or Hashmap. It's unique. If you need to store more than one value for a key , you can store all the values in a Array or a ArrayList and use that in a collection class like Hashtable or Hashmap.

Ps:- All array's are instance of the class "Object". So you can use it in the collection class.
[ November 02, 2005: Message edited by: Srinivasa Raghavan ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your task is to later find everybody with key of "2" you can use a Map of Lists ...
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
list = new List();
I hope that's a joke right?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't drink and type. Even Coke Zero, apparently.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're in luck - that wheel has already been invented. See

http://jakarta.apache.org/commons/collections/apidocs/org/apache/commons/collections/MultiHashMap.html
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have small doubt regarding strings.

String s1="hai";
String s2="hai";
what is the difference between (s1==s2) and s1.equals(s2).
please reply to this
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Browse the Jakarta Commons Collections API (http://jakarta.apache.org/commons), you might find something useful.
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi PVSR Rao
what java does when it stores string references is that it creates just aliases for the same type of string which does not happen normally with other classes.
what i mean by this is when you say,
String s1="Hai";
String s2="Hai";
then s1==s2 would return you true and also s1.equals(s2) would return you true
this is because whenever any string is created then any references having same charatcer sequence point to the same string in the pool.
eg at a later stage if you create a string with same character sequence "Hai" then it would also be just as an alias to the above two strings s1 and s2;
This is done by java just for performance purpose so that it does not takes more space for storing same type of strings.

But do note that for Classes such as Integer if you write a code such as ,
Integer i1=new Integer(2);
Integer i2=new Integer(2);
and print the value for ,System.out.println((i1.equals(i2)));
you would get a value true..but this is because the equals method of Ineteger class is overidden.
so if you do System.out.println(i1==i2);
you would get a value false as == would check the reference and not the values.
So in strings if you do either == or .equals ,ultimately it is the characters which are being checked for the equality.

Bottomline is that all string with same characters belonging to string class have same references.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic