I have a processToTaskIdHolder
Map which contains processId
as the key and taskId
as the value. Now I am iterating this map and at the end I am making a String in particular format.
For example:-
- Let's say I have 123 as the key and 009 is the value in the
processToTaskIdHolder
map. - Now I will make a "activityKey" using 123 and then get data basis on this key.
- Now I will iterate all the categories for that
activityKey
and check whether thosecategoryId
are already present inprocessToTaskIdHolder
keyset or not. If they are present, then I will extract taskId for thatcategoryId
from the map and also extract score for thatcategoryId
and store it in anInfo
class. - Same category can be present with different score for different
processId
.
Now I need to repeat above steps for each activity I have in activities
list.
So my formatted string will be like this:-
A,B,C:Score1,D:Score2 P,Q,R:Score1,S:Score2
- Where A is the
categoryId
for theprocessId
C and D, andScore1
is the score forcategoryId
A forprocessId
C.Score2
is the score forcategoryId
A but forprocessId
D. We have different scores for same categories for two different processes. It means,categoryId
A was present in bothprocessId
C and D so I need to get the score for both the cases and make a string like that. And B is thetaskId
forcategoryId
A which will be present in the map. - Where P is the
categoryId
for theprocessId
R and S, andScore1
is the score forcategoryId
P forprocessId
R.Score2
is the score forcategoryId
P but forprocessId
S. We have different scores for same categories for two different processes. It means,categoryId
P was present in bothprocessId
R and S so I need to get the score for both the cases and make a string like that. And Q is thetaskId
forcategoryId
P which will be present in the map.
I have this code which does the job but I think it's not the right and efficient way to achieve above formatted string. I believe it can be done in a much better way.
private static final List<String> activities = Arrays.asList("tree", "gold", "print", "catch"); public static void reverseLookup(final String clientId, final Map<String, String> processToTaskIdHolder) { Multimap<String, Info> reverseLookup = LinkedListMultimap.create(); for (String activity : activities) { for (Entry<String, String> entry : processToTaskIdHolder.entrySet()) { String activityKey = "abc_" + activity + "_" + clientId + "_" + entry.getKey(); Optional<Datum> datum = getData(activityKey); if (!datum.isPresent()) { continue; } List<Categories> categories = datum.get().getCategories(); for (Categories category : categories) { String categoryId = String.valueOf(category.getLeafCategId()); if (processToTaskIdHolder.containsKey(categoryId)) { Info info = new Info(entry.getKey(), String.valueOf(category.getScore())); reverseLookup.put(categoryId + ":" + processToTaskIdHolder.get(categoryId), info); } } } String formattedString = generateString(reverseLookup); System.out.println(formattedString); } } private static String generateString(final Multimap<String, Info> reverseLookup) { StringBuilder sb = new StringBuilder(); for (Entry<String, Collection<Info>> entry : reverseLookup.asMap().entrySet()) { sb.append(entry.getKey().split(":")[0]).append(",").append(entry.getKey().split(":")[1]) .append(","); String sep = ""; for (Info info : entry.getValue()) { sb.append(sep).append(info.getLeafCategoryId()).append(":").append(info.getScore()); sep = ","; } sb.append(System.getProperty("line.separator")); } return sb.toString(); }
In the reverseLookup
map, I have a key in this format - "a:b", so I'm not sure instead of making a key like that. Maybe it can be done in some other better way?
Note: I am working with Java 7. Categories
and Info
class is a simple immutable class with basic toString
implementations.
Info
andCategories
classes. You can assume that they are immutable classes.\$\endgroup\$