ConcurrentSkipListMap put in order - java

I have a ConcurrentSkipListMap of keys and values. It is very important to hold the order of the keys.
The problem appears when I try to insert a new value in a particular position. The only one method to insert a value is the put() that put this value in the last position.
With the replace method it can only edit the value, not the key.
Is it possible? What can I do? Can you tell me another class to do it?

The ConcurrentSkipListMap holds the order of the keys on its own as they are sorted.
So either you were not aware of it or you actually do not want to hold order of the keys but manipulate them yourself.
If you meant that you want the keys returned in the order in which they were put into the map than use the separet List (ConcurrentQueue if you need concurrency) and place the keys there manually.

Related

Unsupported add/addAll operations for Map<K,V>.keySet()

Regarding the Map<K,V> interface:
Why does keySet() return a Set that supports the remove operation but doesn't support add() and addAll() operations?
The Set returned by keySet is backed by the Map, so changes to the map are
reflected in the set, and vice-versa. This means that calling remove on that Set removes the matching Entry from the Map.
It would make no sense to call add or addAll on that Set, since you can't add key[s] without corresponding value[s] to the Map.
Think about what you are asking for:
you want to retrieve all KEYS of a map (and that set is not a "copy" of the keys; it represents the keys of the map).
And then you ask to add elements to those KEYS. In other words: the "data set" you are looking at has the semantic meaning of keys coming from a map. And you want to increase that "data set" - but without providing the corresponding entries for that map.
Deletion on the other hand is straight forward; deleting a key will also delete the corresponding entry from the map.
It's because each key in the set is linked to a value in the map. Removing a key will remove the associated value, but to add you'll need a value and not just a key.

Sorting a hashmap

I need to sort a hash map according to the key.The key is a string(so I need to sort it alphabetically) and the value is an integer.
I was trying to search online and found that tree set automatically sorts it once you put it. Could somebody guide me in the right direction as to how I could convert it into a tree set or maybe even if i could just sort it using a hash map.
Thanks in advance
Since hashmaps are unsorted maps by definition you'd need to use another container for that. Depending on your needs there are several options, some being:
Use a TreeMap instead of a HashMap either temporarily or as a replacement. This would be the best option unless you have to keep the hashmap.
Use a TreeSet to sort the keys, then iterate over the keys and extract the values from the HashMap.
Do the same as in option 2 but fill a new LinkedHashMap during iteration. This will result in a map that returns the values in insert order which happens to be sort order due to use of a sorted set. Note that adding elements to the LinkedHashMap will append any new elements to the end - LinkedHashMap is still ordered by insertion order.

Merge maps in java

What i'm trying to do is pretty simple.I want to merge two maps.
Say
map1={(1,"one"),(2,"two"),(3,"three");
map2={(1,"onetoo"),(4,"four")};
if i follow this->
map3.putall(map1);
map3.putall(map2);
then value of 1 is onetoo but when i follow the reverse it is one.
is there anyway i could change that?what i mean is that java overwrites and puts only the latest value for a key.
i.e if onetoo was added after one (in their respective maps)then no matter what the order of putall calls to map3 the value remains onetoo.
There is no way to do that, unless you store the actual time when the values were added.
Say map1={(1,("one", 15:15)), (2, ("two", 15:16))}
Then you can add all of map1 and then iterate over map2 adding only if the key is not already there or if it's there but with a earlier timestamp.
That's how maps work, they use the hashcode of the object you set as key as a way to identify its self within the map entries, and as you can see it has to be unique.
So you would have to specify another key since an integer value of 1 has a hashcode of 1.
Well, If your programe worked as you want, then predict the output of the following
map3.get(1);
You could never know whether it is "One" or "Onetoo".
Too prevent any such problem, Maps in Java are designed to contain only unique keys.
So , if you write
map3.putall(map1);
the value of 1 is "one". but as soon as you write
map3.putall(map2);
the value of 1 is reset and it becomes "onetoo". reverse happens when you reverse it. Possible solutions could be.
Put in your keys in maps in such a way that they (keys) uniquely identifies an object. So that whenever in future you merge maps, no clash happens(in terms of duplicity) in keys.
If you can't do it, then a possible solution could be to get all keys of every map and check for duplicity and change the duplicate keys in such a way that you can retrieve your objects without hassle.

Java HashSet key/value pair

Why doesn't Java provide functions to get the key/value pairs in a HashSet like exists in Hashtable? It seems like a real pain to have to iterate over it every time you need to get at something. Or is there an easier way to do this?
HashSet doesn't have key/value pairs. It is a Set of objects and you would use an implementer of Set to ensure that a collection of objects contained no duplicates.
Implementers of Map like HashMap have key/value pairs and provide a get(Object key) method to get the value associated with a key.
Since a Set doesn't contain keys and values, there is no way such a view could be provided.
What would you consider to be the key and what would be the value in a Set?
A Set don't have any key/value pairs, just (unique) values. As you already said you get these values via the Iterator or by returning an array with these values with the toArray() method.
Maybe you are looking for a List instead.

Does a HashMap retain the order of its elements on the next read if it is constructed and "filled" as a LinkedHashMap?

Suppose I have a Java method that returns a HashMap object.
Because a LinkedHashMap is a subclass of HashMap, I can return a LinkedHashMap from this method just fine.
On the next "read" action (no adding/removing/modifying of K/V pairs), would iterating over the keys of the resulting method (which returns a HashMap) go in the same order as the originating LinkedHashMap, even though the HashMap lacks the key links?
Yes. The actual instance of the object is still the returned LinkedHashMap, therefore it will have its iterating order.
However, I wouldn't depend on this for anything. Why are you using HashMaps if iterating order is important? This might be a code smell.

Categories

Resources