how to filter following map? - java

I have got these values in my HashMap ,
"ver":"a"
"ver":"b"
"ver":"c"
"os":"d"
"os":"e"
i need only:
"os":"d"
"os":"e"
My code is:
String[] eachPair = myString.split(",");
Map<String,String> pairs = new HashMap<String,String>();
for(String pair: eachPair) {
pairs.put(pair.substring(0, pair.indexOf(":")).trim(), pair.substring(pair.indexOf(":")+1));
}
pairs.get("os");
but its not working. please help

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Map should have unique keys only, If you add a value with the same key which is already exist in the map, value for that key will be override and you will lost the old value.

HahMap doesn't allows duplicate keys.So I recommand to use Guava's MultiMap or apache's MultiValueMap
Please look into
the sample code reference of MultiMap: http://tomjefferys.blogspot.in/2011/09/multimaps-google-guava.html
the sample code reference of MultiValueMap: http://java.dzone.com/articles/allowing-duplicate-keys-java

Related

Filter Object list with map key value , replace the Map value

I have a List of Objects
List<Person> personLst = [{"person:" {personName:AASH_01 , country :AUS, state :ADL, zip :null },
{personName:AASH_01 , country :AUS, state :MLB, zip :null}}]
and Map holds a key value and based on this need to apply the filter for the list.
Map<String, String> lstMap = new HashMap<>();
lstMap.put("ADL","12345")
Apply Filter Condition:
If the personLst.contains(ADL) this is a map key filter it and replace the zip with map value(12345).
Tried different stackoverflow question , did not help much on .
Just to be sure,
List<Person> personLst = [{"person:" {personName:AASH_01 , country :AUS, state :ADL, zip :null },{personName:AASH_01 , country :AUS, state :MLB, zip :null}}]
really refers to a list of valid java objects, right? Because in the current state those would be valid JavaScript Objects, but not valid Java objects.
Also, should a List<Person> really be called with personLst.contains(String)?
So, assuming these are valid Java objects with the correct getters and setters, and the correct method is intended, you should be able to use
personLst.forEach(p -> {
if(lstMap.containsKey(p.getState()))
p.setZip(lstMap.get(p.getState()));
});

Compare Set<String> with Map<Object> to get Map value in Java 8

I have Set<String> requests = new HashSet<>() and added dynamically some string values in it.
I have Map<TargetSystems> targetSystems = new HashMap<>();, targetSystems with name key and targetSystems object in value.
So I need those targetSystems from Map which are in in Set.
Like Map.put("LMS", TargetSystems) and many more from Database.
Set<String> requests has same target systems name.
How can I achieve this in Java 8.
I don't want to execute forEach loop into Set because I have already created thread list and looping on that. So I need solution in Java 8 with Stream.filter kind of.
Thanks in advance.
I am going to assume that you meant Map<String, TargetSystems>.
You can make a copy of the Map, and use Set.retainAll to limit which entries are in it:
Map<String, TargetSystems> copy = new HashMap<>(targetSystems);
copy.keySet().retainAll(requests);
Collection<TargetSystems> systemsForRequests = copy.values();
If you want to filter your targetSystems map:
Map<String, TargetSystems> targetSystemsFiltered = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
If you want just to get filtered targetSystem objects:
Set<TargetSystems> targetSystemsFromSet = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());
I have already got an answer and code below please see it's working fine.
Case was that, if we have Map<Object(Its entity class type)> targetSystems and put targetSystems entity objects by the name of it's entity key. Now I have multiple request by the same name exists in targetSystems Map, so I need to equalized that hashset value with hashmap.
I achieved as under:
Object[] objects = Map<String, Object>.values().stream().filter(ts -> Set.contains(ts.getName())).toArray();
I get Object[] type, so I can get my targetSystems value from Map as easy as I like.
Thanks all of you for your kind answers and thanks to stack Overflow.

get Keys from IgniteCache

I have to get all keys stored in IgniteCache, unfortunately this method is not implemented in Ignite. I'm using java client.
I thought it is a common method, what is the reason Ignite team didn't implement it?
Is there any efficient solution for getting keys?
Thanks to #alexfedotov I created a solution for my problem, I'm positng it here, since someone may find it useful.
List<K> keys = new ArrayList<>();
cache.query(new ScanQuery<>(null)).forEach(entry -> keys.add((K) entry.getKey()));
After running this code you will receive a list with keyset.
You can get all the keys using ScanQuery with a null predicate. It will return all the entries (key-value pairs).
As well you can use an SqlFieldsQuery like select _key from Entity
The easiest way for getting all keys from cache :
ICache<string, object> cache = ignite.GetCache<string, object>(cacheName);
List<string> cacheKeys = cache.Select(e => e.Key).ToList();

Jedis HMSET map insertion order

Jedis has a hmset method which allows you to set a map of fields and their values at a specific key.
I use the method this way:
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", player.getName());
map.put("ip", player.getAddress().getAddress().getHostAddress());
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
pipeline.hmset("uuid:" + uuid, map);
pipeline.sync();
core.redis.getJedisPool().returnResourceObject(jedis);
I decided to use a LinkedHashMap to retain the insertion order — however, when looking at the database, it still messes up the order.
Does anyone know how to insert it into the database without messing up the order?
Thank you.
The String-based Jedis client transcodes the strings using a temporary HashMap, so it kills your order. The BinaryClient iterates over the Map directly and retains the order.
The lettuce client keeps the order of your map in any case.
Alternatively, set the values one by one using HSET hash key value
HTH, Mark
Give a try to Redis based framework for Java - Redisson. It keeps map entries in insertion order during iteration using any codec (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization).

How to iterate Map<Map, Map> over jsf..?

i have a map which i want to iterate over the jsf. The map is as -
LinkedHashMap<Map<String,String>,Map<String,String>> propertyMap=new LinkedHashMap<Map<String,String>,Map<String,String>>();
earlier i have iterated the following map on jsf of type String, String in following manner
List documentProperties = new ArrayList(propertyMap.entrySet());
And in jsf :-
<af:iterator value="#{EditProperties.documentProperties}" var="list" id="i1">
<trh:rowLayout id="rl1">
<trh:cellFormat id= "cf3"><af:outputText value="#{list.key}"
id="ot1"/> </trh:cellFormat>
<trh:cellFormat id= "cf4">
<af:inputText id="it1"
value="#{list.value}" showRequired="false">
</af:inputText>
</trh:cellFormat>
</trh:rowLayout>
But how can i iterate a map having two map inside it on jsf..??
Thanks
Map as a key is bad idea. You should use Immutable objects as key to hashmap.
If you want to declare Map inside Map then you can do something like below.
LinkedHashMap<String,Map<String,Map<String,String>>> propertyMap=new LinkedHashMap<String,Map<String,Map<String,String>>>();

Categories

Resources