This question already has answers here:
How to implement a Map with multiple keys? [duplicate]
(27 answers)
Closed 9 years ago.
Have a class with attributes attr1, attr2, attr3 and attr4.
I am looking for a way to load a bunch of objects of that class in an array (or ArrayList or Map or whatever) and then being able to retrieve (search) these objects based on attr1 or attr2.
It looks like the way to go is HashMap but I will have to:
if I want only one HashMap, I will have two entries for each object, one with key attr1 and one with key attr2
have two HashMap objects, one with key attr1 and the other with key attr2 and based on what I am searching for, use the appropriate Map.
Is there any other elegant way of doing this? Is there a Map or Collection object that will allow me to provide multiple keys for an object?
I find the second solution with two Map objects quite elegant, each Map being a sort of index of your data. If you really want one single structure, then you can use Guava's Table, who is mapping values to a pair of keys.
Related
I have a list of HashMap objects in Java.
I would like to conditionally add more HashMap objects to this list if the list does not already contain a HashMap having the same key value pair as in the new HashMap.
Here is an example HashMap list. Note that in reality, there are more keys. Here, I am just including "contact_id" for simplicity.
[{contact_id=16247115}, {contact_id=16247116}, {contact_id=16247117}, {contact_id=16247118}, {contact_id=16247119}]
Adding {contact_id=16247117} to this list should not be allowed.
Adding {contact_id = 74857983}, should be allowed.
Ideally, I would like to be able to conditionally add several HashMaps into this list in one line of code. If I were not to perform the conditional check, I could just use the syntax listname.addAll(batchOfHashMaps). I'd like to do something similar, but precluding redundant HashMaps in the list.
What is the most efficient way to achieve this conditional insert in Java?
I reckon there must be a more efficient solution than evaluating each element in the list inside a for-loop.
If you are only wanting to look at one key-value pair of the maps as an identifier, then you could use a Map instead of a List to hold everything. For example,
Map<String, Map<String, String> mapOfMaps;
Then you could add one like:
mapOfMaps.putIfAbsent(mapToAdd.get("contact_id"), mapToAdd);
you could add multiple like:
batchOfHashMaps.forEach(m -> mapOfMaps.putIfAbsent(m.get("contact_id"), m));
To get a collection of your maps simply call values()
mapOfMaps.values();
This question already has answers here:
HashMap: One Key, multiple Values
(15 answers)
Closed 4 years ago.
I would like to create an object with Map<String,Map<Integer, String>> the inner should not be a Map type because the inner key (Integer) is not a primary key (unique). And as far as I know when it comes to Map if there is a similar key value it will override the previous similar data.
What should be the datatype of my inner Map?
It depends on what you are going to do with the map. If the values of the outer map are just pairs, you can use Map<String, Set<ClassContainingIntAndString>>, or if you already know what it is (like you said there are only three values?) Map<String, SomeClassThatMakesSense>. However, if you want fast access to the final Strings given the first and second Integer, you should use Map<String, Map<Integer, List<String>>> (or something similar except encapsulated in some user-defined classes, as it may be bad practice to have too many nested generics).
This question already has answers here:
Difference between HashSet and HashMap?
(20 answers)
Closed 8 years ago.
I have Question regarding the best practice of using a Collection regarding the memory. I need to call a method which returns pairs of (Key,Value) frequently, so which way is the best, using HashMap or creating an Object that contains Key and value and save this object in a HashSet?
Thanks & Regards.
It depends on whether you need to search the data structure based on the key alone or both the key and the value.
If you search by the key alone (i.e. map.containsKey(key)), you should use a HashMap.
If you search for existence of a key-value pair (i.e. set.contains(new Pair(key,value)), you should use a HashSet that contains those pairs.
Another thing to consider is how you determine the uniqueness of the elements. If it is determined by the key alone, you should use a HashMap. If it is determined by both key and value (i.e. you can have the same key appear twice with different values), you must use HashSet, since HashhMap doesn't allow the same key to appear more than once.
This question already has answers here:
Sort a Map<Key, Value> by values
(64 answers)
Closed 8 years ago.
I have a generic Map which is defined thus
Map<Integer, Book> bookCollection
The Integer is the book ID and the Book is obviously a Book object.
The Book object has a member called publicationYear.
How do I sort the Map so that its Book objects are in order by the Book's publicationYear member. So, if I iterate through the sorted map, the oldest book appears first up to the newest book.
At the moment, the Map is randomly sorted.
the publicationYear has to be part of the key. to sort based on that.
One solution to this sort of problem is to write your own data structure that uses a two or more standard data structures to implement the messy details.
For example, you could use a HashMap<Integer, Book> to allow rapid look-up by Id. You could also have a TreeSet<Book> in the required order. If it is not the case that Book is Comparable with the key you need for this, use a Comparator<Book> to create the TreeSet.
Most methods for the composite data structure will be two or three lines long. The fact that the two underlying structures exist and are being kept consistent is hidden from the rest of the program, which just accesses the composite structure.
I am trying to create a trigram model using LinkedHashMap>
where Entry is the entry of last inputed bigram (whose structure is:
LinkedHashMap
Now the problem is, being a map it does not store multiple keys (overwrites the existing key-value pair with new key-value pair for existing key).
I am not sure whether a multimap will be useful for this or not? If so, how?
Every collections that implements Map interface keeps it's keys in Set implementation. Set can not keep multiple values.
Multimap is just a Map<Key, Collection<Value>>. It allows to keep multiple values under one key.
Read more about Maps.