Map collection separate Object values - java

I have a Map collection that stores "stops" as Key and a set of grid and time results as an Object value. e.g.
Key: [stops]
Value: [[{grid_item=Grid1, time=09:30}, {grid_item=Grid13, time=10:00}, {grid_item=Grid3, time=10:15}, {grid_item=Grid10, time=10:35}]]
Is there a way to separate the Value results, because i would like to use the grid_item and time to send them to another method. How can i get those values specifically?
Or should i store again the values in a Map but now the keys are grid_item and time. But how can i do that?
Any Suggestions?

You can access a specific object in your value's array this way:
map.get(stops)[0] // grid_item=Grid1, time=09:30
map.get(stops)[1] // grid_item=Grid13, time=10:00
....
where stops is the key that you are using.

Related

Aerospike - How to insert a unique item in a given list using Java?

If I run the code below, the value coming from getUserName() gets appended to the list. But if I run it again, same value gets inserted again. My question is, how to make sure the value is appended only once(Unique)?
My current code
Operation operation = ListOperation.append("names", Value.get(usr.getUserName()));
Record record = client.operate( policy, key, operation );
Example: Consider the "name" bin to be a list of strings.
'LIST["T1", "T2"]'
If I add T2 again, the list should remain the same.
ListOperation.append() can take a ListPolicy, which takes flags. For write operations such as this, the ListWriteFlags have the fields ADD_UNIQUE and NO_FAIL which you should combine if you want a value to only be added if it's a new unique list element.
See https://www.slideshare.net/RonenBotzer/asug-tlvmeetup2talk
i'm assuming that ListOperation is backed by a List since the code isn't available. Assuming it is, Lists maintain order while allowing null values. Sets on the other hand only allow unique, non-null, values so this would be a perfect solution for you.
Set<String> values = new HashSet<>();
values.add("T1");
values.add("T1"); // already contains value
Alternative
If you need to maintain the order of your Collection but you want unique values, another option would be to check on add if the value already exists OR using Stream#distinct.
List<String> values = ListOperation.values.stream().distinct().collect(Collectors.toList());

Freemarker - access value at a particular key in map

I have Map variable in my Freemarker template. How can I fetch a value at a particular key in the map, as we do in Java (map.get(<key>)).
I know how to iterate through keys and values of a map in a FTL. But I want a solution without iteration, on the lines of Java get() method of Map interface.
map[dynamicKey] or map.staticKey if the key is a string. Due to historical limitations, if the key isn't a string, map?api.get(nonStringKey).

which collection/ data structure can handle key, value, value

I have been using a HashMap which handles key / value pairs. But, now i need to handle key: value , value. Is it possible to have one key with 2 values ?
can you recommend a data structure/collection or strategy for me?
Yes you can, assuming this is c++, you can have
std::unordered_map<key, std::pair<value, value>>;
You can make the std::pair whatever type you'd like them to be.
What'd make more sense to do is to make an object to hold your two values, like HashMap<KeyType, ContainerObject> map.
In container object, you can use something like a list, or your own defined object that's made to just hold whatever two values you need. This way, you can use the HashMap and just access whatever values you need through the object that holds them.

Storing java entities in hashmap for faster retrieval

I have an object so that
class MyObj{
public long id_1;
...
}
My HBM (hibernate mapping file) tells that this id_1 is my Id. Now what i want to do is to cache this entity in HashMap so that HashMap<MyObj, NestObj> i.e., MyObj will become the key for the hashMap.
Now the question that I wanted to ask
I want to make sure that even though i have saved the whole object as the key, I want to keep object retrieval/storing in the hashmap based on the MyObj.id_1 value. The easiest way i can do so is to after retrieving all objects, I have to do a for loop to add them in a map as Map <Integer, MyObj> but in that case i would have maintain two maps (one for MyObj and other for NestedObj) which i want to avoid.
How can i dictate my HashMap to use MyObj.id_1 column to use as comparator, hash etc. Shall i override hash and equal function ? But if i do so, would it affect hibnerate comparison while storing/retrieving entities ?

How to Update value in MultiValueMap for a specific key

I have a MultiValueMap like
{3=[c-2, c-2], 2=[b-1, b-1], 1=[a-1, a-2, a-3]}
At one point I have to update a single value of a specific key
for example I have to update the key 2 like
2=[u-1,u-2]
how can i do this?
I've never used that library - but I would expect these two examples to do what you need:
multiMap.getCollection(2).clear();
multiMap.putAll(2, Arrays.asList("u-1", "u-2"));
Or
Collection c = multiMap.getCollection(2);
c.clear();
Collections.addAll(c, "u-1", "u-2");
The safest way is to call getCollection() to retrieve the current mapping, remove(key) to clear that mapping, iterate the retrieved collection to re-insert values that you want to keep, and/or add the new values.
If you know the type of collection used for a mapping (because you've called the constructor that takes collectionFactory), you could get the collection and update it directly.

Categories

Resources