views:

230

answers:

9

What generics collection class can I use that provides both mapping and array-like functionality. For example, I want to map a String to a Double and I want to reference the value using the key as the index.

collection[key] = collection[key] + double

Does the Google collections library provide such functionality?

Thanks.

+4  A: 

HashMap should serve your purposes:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html

rledley
+1  A: 

I use java.util.HashMap for key-value pairs unless performance is an issue, which it nearly always isn't.

Rich Seller
HashMap is about the most performant thing you are going to come up against for direct lookups. Adds can be slightly slower than a linked list. Unless you are severely memory constrained, you aren't going to beat a hash for performance in any lookup situation except an index into an array or ArrayList.
Bill K
+2  A: 

You kind of have to choose here :-) Either your key is a String:

Map<String, Double> myMap = new HashMap<String, Double>();
myMap.put("key1", 5.0); // caution - auto-wrap
myMap.put("key1", myMap.get("key1") + 5.0); // caution - auto-wrap

or it's an int:

double[] myArray = new double[size];
int key1 = 1;
myArray[key1] = 5.0;
myArray[key1] += 5.0;
ChssPly76
+1  A: 

I don't see how your example uses array-like functionality. Do you just mean overloading the array index operator? If so, you should know that Java does not allow operator overloading.

If you really do need array operations, the LinkedHashMap allows you to do key-value lookups, as well as keep track of an ordering within the values. This is primarily useful for building a LRU cache.

Michael Donohue
i was in c# mode. array index operator is exactly what i was thinking of.
javacavaj
+2  A: 

Java won't let you use the bracket syntax with collections, but you could do something like:

HashMap<String, Double> collection = new HashMap<String, Double>();

String key = "some key";
...

if( collection.containsKey(key) )
{
    // increment the value
    collection.put( key, collection.get( key ) + 1.0 );
}
else
{
    // initialize the value
    collection.put( key, 0.0 );
}
Bill the Lizard
(If anyone is reading this in a year's time, JDK7 might have the `[]` syntax for collections.)
Tom Hawtin - tackline
Thanks Tom, I didn't know that was planned. Hopefully someone will ping me on this if I forget to come back to it when Java 7 comes out.
Bill the Lizard
A: 

yeah, i wanted to do the same thing a while back , where I wanted to represent a database table in a data structure where there was a primary key "lookup" column. HashMap wouldn't do that by itself (as far as I know).

i think what i did was make my own data structure Class that contained multiple Arrays mapped by an index value. i suppose I could have created a RowData class and stored each rowdata in a HashMap. Hmm.

someone else could probably explain much better than I. im just a beginner.

djangofan
+1  A: 

The closest I can see is LinkedHashMap which gives you predictable order of the elements, but that does not give you a fast lookup of a given index (it is kind of like LinkedList in that way). And it doesn't give you a specific method to find by a specific index, you would have to add your own.

Yishai
yep, this answer is the ticket. heres an example: http://www.java-examples.com/simple-java-linkedhashmap-example
djangofan
A: 

Do you mean array-like functionality or array-like syntax. Java doesn't allow collections to use array-like syntax, but all collections have some form of array-like functionality.

If you mean that you want to store pairs (key/value) AND be able to access them with an index (like you would an array), then there is a "LinkedHashMap" collection with your name on it.

If you mean that you want to index in both direction (either side of the collection could be a key) then you probably want to create your own collection containing two hash maps, and every time an object is added to your collection, you add it to both maps.

Bill K
A: 

Google Collections has a Multiset, which associates each key with an Integer.

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multiset.html

Jared Levy