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

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

Related

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.

How to create a map that takes values from other map and then maps them to its field?

So I have a map of ids to systemUsers and now I want to create a map of systemUser keys and login values. Login is a field inside systemUser class. I have a problem with how to write the mapper functions or even if it's the right way to go about it
Map<Long, PHSystemUser> systemUserMap = getPersistenceLogic()
.getSystemUsersMap(serviceClientMap.values());
Map<PHSystemUser, String> loginMap = systemUserMap.values().stream()
.map(PHSystemUser::getLogin)
.collect(Collectors.toMap(, ));
All you need is to collect directly using two functions:
systemUserMap.values().stream()
.collect(Collectors.toMap(Function.identity(), PHSystemUser::getLogin));
The problem with .map(PHSystemUser::getLogin) is that it changes the stream to Stream<String>, leaving you no chance to have the entire PHSystemUser object downstream.

Google trends api results in java

I am using Google trends to get trends for particulate keyword. it will returning JSON but main problem is that i want to create class that holds data and used in java code as array List.
I am confused what is the class structure for it when i get result look like below
{"version":"0.6","status":"ok","sig":"1248242565",
"table":
{ "cols":
[{"id":"date","label":"Date","type":"date","pattern":""},
{"id":"query0","label":"linkedin","type":"number","pattern":""},
{"id":"query1","label":"facebook","type":"number","pattern":""}],
"rows":[{"c":[{"v":new Date(2004,0,1),"f":"January 2004"},{"v":0.0,"f":"0"},{"v":0.0,"f":"0"}]},
{"c":[{"v":new Date(2004,5,1),"f":"June 2004"},{"v":0.0,"f":"0"}, {"v":0.0,"f":"0"}]},
{"c":[{"v":new Date(2004,8,1),"f":"September 2004"},{"v":0.0,"f":"0"},{"v":0.0,"f":"0"}]},
{"c":[{"v":new Date(2013,9,1),"f":"October 2013"},{"v":1.0,"f":"1"},{"v":83.0,"f":"83"}]}]
}
}
It will return row and cols on search query if i search two individual word the the result is like above JSON. nay idea to how can i can make class Trend.java and that list object that holds all this informations
How would you represent those values? I'd go for a List<HashMap<String, String>> implementation.
You can assign each item in a row to a HashMap with the column header as the key. So:
HashMap<String, String> row = new HashMap<String, String>();
row.put("id", "c");
// add the rest.
Then you can cycle through each row, and request the column data by name. This will also make for some very semantically nice code!

how to filter following map?

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

find item from item name lookup api

I'm currently working on a project,
I need something that I can provide a name, then it can return what kind of item it is.
Say I have word starcraft,
then the API or some database can return something like game
or nba -> sports
or nike -> sports/shoe
or sadkljasd -> unknown
I saw something did this like months ago, but I can not recall.
I need something that has this kind functionality and data, and it does not have to be accurate
Anyone has any idea?
Thanks a lot
HashMap provides the kind of API you are looking for..
You can have a mapping from your item to their type.. In a Map, you store your mapping in the form of Key-Value Pair... If all your items are unique, it will be the best bet for you..
Here I will give a brief example of how a Map works.. Rest you can get from the link I have given..
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("nba", "sports");
String type = mapping.get("nba");
System.out.println(type); // Will give you `sports`
And if you have multiple types for some items, you can have a Mapping from items to the list of their types: -
Map<String, ArrayList<String>> mapping =
new HashMap<String, ArrayList<String>>();
Use Collection.....
- Using Map will be appropriate at this stage...
Eg:
Map<String, ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
Or
Map<String, String> map = new HashMap<String,String>();

Categories

Resources