creating Trigrams using LinkedHashMap java - java

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.

Related

What is the purpose of placing a HashSet inside of a HashMap?

For example:
private HashMap<Integer, HashSet> variableName;
I understand that HashMap implements Map and doesn't allow duplicate keys and HashSet implements Set and doesn't allow for duplicate values, but what is the purpose of placing a HashSet inside of a HashMap? Do they not achieve similar tasks by themselves (though in different ways and with different performance)? What functionality does doing this provide that a HashMap by itself couldn't?
You do this when you want to map a key to more than one thing. A Map can only map a key to a single thing, but that single thing can be a Set which holds multiple things.

How to conditionally insert a Map into a list in Java if the list does not already include a map containing a key value pair

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();

Why key in HashMap can't be duplicated

I know how hash map works. We can't use duplicate keys in Hashmap. I want to know the logic behind this used by Sun people. How it has been coded that we can't store duplicate key in HashMap.
It's part of the contract of the Map interface:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
What should Map.get(key) return if a key has multiple values?
An HashMap is a data structure where is possible to save couples of key values. This data structure implements Map.
From javadoc:
A map cannot contain duplicate keys; each key can map to at most one value.
It is possible to retrieve directly a value using its key, so is not possible to have more values associated to the same key.
Instead is possible to have multiple keys pointing to the same value.
If you need a different data structure where multiple values are associated to the same key you can use libraries like Guava that has the concept of Multimap
Note: Using standard Map is possible to create a Map<MyKey, List> where you associate a list to a key. So you can also add multiple values to that key storing them in the associated list.

Duplicates in LinkedHashMap

In my code I am using a set of interleaved LinkedHashMaps inside each other as below. The code is fine and gives me the result I want except it automatically removes the duplicates. I couldnt find out how I can use TreeMap or Set in order to keep the duplicates.
LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, Vector<String>>>>
dataAll =new LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String,
Vector<String>>>>();
LinkedHashMap is still a Map data structure. It maps a unique key to a value. If you assign two different values to a key the second value will simply replace the first value assigned to that key.
Also imagine why do you need a Map of duplicated key? The sole purpose of Map is to provide a one to one relationship between key/value pair. It does not handle one to many relationship.
If you have to map a key with a list of values, use something like:
LinkedHashMap<String, List<..>>
This allows you to have one key maps to a list of values.

Hash Multi-Map Issues

I have two questions on Java HashMap:
1) Is it possible in any implementation of Java HashMap to get corresponding Key from the value ? I am using HashMultiMap (key -) Multiple values).
2) Is it possible in any implementation of Java HashMap to get Key position in the HashMap ? If so, then If I add new key, is it possible that the key position is changed ? I am using HashMultiMap (key -) Multiple values).
If both of them answer is NOT, how is it possible to implement manually (any idea ?) ?
1) Yes, but not in an efficient way, and there are no methods in interface Map to do this with one method call. You'd have to iterate over the entries of the map until you find one with the value you're looking for; then you have the key of the corresponding entry. There are implementation such as Google Guava's BiMap that do let you do reverse lookups efficiently.
2) No, because a map is not an ordered collection: keys do not have a defined position in a map. If you need this, you could use for example LinkedHashMap, which keeps key-value pairs in the order they are inserted in the map.

Categories