Take the 2-minute tour ×

In Java, how can I create an array where each element is a list of objects containing a pair of keys.

Then given an index of this array, I should be able to search the list at the index provided a single key value and retrieve the corresponding value.

Problem Statement

I have around 2 million webpage name and corresponding to each name are categories (one or more) .

I can hash them down to say 1,00,000 using some hash function but obviously their will be collisions.

I want to build a data structure in Java that can store all this information, and then given a webpage and then, after hashing, given the index, I can find the categories of the web page.

share|improve this question
stackoverflow.com/questions/217065/… –  mre Nov 7 '11 at 13:04
like, an ArrayList<Properties>? –  Crollster Nov 7 '11 at 13:04

2 Answers 2

up vote 9 down vote accepted

It sounds like what you want is an array of maps:

List<Map<KeyType, ValueType>> myList = new ArrayList<Map<KeyType, ValueType>>();

This is how you add a map:

myList.add(new HashMap<KeyType, ValueType>());

This is how you add value to the map at index i with key as key:

myList.get(i).put(key, value);

To search for key in the map at index i use:

ValueType value = myList.get(i).get(key);

Edit: After your edit I think that the solution to your problem is to simply use HashMap<String, List<String>>. You'd use the name of the webpage as key and the list of category-names as values. There is no need to implement your own hash-map (it sounds like that was what you were doing).

share|improve this answer
I have an object webpage which has name and category . Each element of the list should be a webpage . –  R1234 Nov 7 '11 at 13:07
@Rohan: You should use a HashMap<String, WebPage> and use name as the key. This way you get very fast lookups. If that is not what you want, please take the time and improve your problem description. –  Björn Pollex Nov 7 '11 at 13:09
added more info to problem. –  R1234 Nov 7 '11 at 13:14
@Rohan: I updated my answer. –  Björn Pollex Nov 7 '11 at 13:24

Since you added info to your description I'd recommend you to use map of maps. Everything is like @Björn Pollex said but use map instead of list to store webpages themselves.

And one more recommendation. Probably you will want to use LinkedHashMap instead of regular HashMap to be able to retrieve the data in the same order that you added it.

share|improve this answer

Your Answer


