Reputation: 484
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.
Upvotes: 1
Views: 4403
Reputation: 115338
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.
Upvotes: 1
Reputation: 76788
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).
Upvotes: 9