aspose file tools*
The moose likes Java in General and the fly likes How to store two similar values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to store two similar values" Watch "How to store two similar values" New topic
Author

How to store two similar values

shalu sharma
Ranch Hand

Joined: Sep 09, 2002
Posts: 51
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

Joined: Sep 28, 2004
Posts: 1228
Think about storing an array in a collection.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
shalu sharma
Ranch Hand

Joined: Sep 09, 2002
Posts: 51
can u be a bit more elaborate..
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: Jan 29, 2003
Posts: 8791
If your task is to later find everybody with key of "2" you can use a Map of Lists ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Originally posted by Stan James:
list = new List();
I hope that's a joke right?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Don't drink and type. Even Coke Zero, apparently.
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
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

Joined: Oct 05, 2005
Posts: 102
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

Joined: Oct 07, 2005
Posts: 637
Browse the Jakarta Commons Collections API (http://jakarta.apache.org/commons), you might find something useful.


ASCII silly question, Get a silly ANSI.
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store two similar values