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>();
Related
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.
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.
I was working on a java program that takes a string and splits it into different levels.
Map<String, ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>> courseInfoMap = new HashMap<String, ArrayList<Map<String,ArrayList<Map<String,Map<String, String>>>>>>();
ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>> courseNumList = new ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>();
Map<String, ArrayList<Map<String,Map<String, String>>>> courseNumTextbookMap = new HashMap<String, ArrayList<Map<String,Map<String, String>>>>();
ArrayList<Map<String,Map<String, String>>> listOfTextbooks = new ArrayList<Map<String,Map<String,String>>>();
Map<String, Map<String, String>> textbookMap = new HashMap<String, Map<String, String>>();
Map<String, String> isbnMap = new HashMap<String,String>();
Essentially,
I'm trying to have a course code --> list of course num --> course num (key) --> list of textbooks --> textbook --> isbn number(str) --> price
So because some course codes have multiple course numbers it points to an arraylist.
I have been adding stuff backwards, so loops within loops. Assuming I did not screw up there. I've been trying to loop through the elements in the same sort of way assuming they were probably added.
for(Map.Entry<String, ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>> entry : courseInfoMap.entrySet()){
System.out.println(entry.getKey());// COURSE CODE
for(int i = 0; i < entry.getValue().size();i++){//loops through arraylist of course numbers
System.out.println("i("+i+" - " + entry.getValue().get(i));
for(Map.Entry<String, ArrayList<Map<String,Map<String,String>>>> entry2 : entry.getValue().get(i).entrySet()){
System.out.println(" " + entry2.getKey());
// . . . and so on
This hasnt worked it just loops through everything. On each level.
My question is how do I navigate through a large number of hashmaps and/or is there a better way to do this.
Example:
AAAA
100
Name
###
$$$
Name
### <-- different from past
$$$
200
Name
###
$$$
BBBB
101
Name
###
$$$
This seems exceedingly complicated, and frankly if I had to maintain that I'd really not look forward to it. Off the top of my head there are several other approaches you could probably take...
Use a relational database
Either an in-memory database or something like mysql or postgresql. They are designed for this sort of thing - organizing data and allowing for ad-hoc queries.
Put your data in Objects
Instead of Maps and Lists, just create an object that defines most of it in one place. Store that object in the List or Map and search for what you need directly. If you don't have too much data, a brute-force uninindexed search probably won't be that bad in wall-clock time.
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!
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>>>();