-1

How can I transform the following data result

[a={x=0, y=43, z=57}, b={x=1, y=90, z=9}, c={x=1, y=83, z=16}]

into

{x=[0,1,1], y=[43,90,83], z=[57,9,16]}

using Map and Treemap.

Basically, I am iterating over a,b,c.. then while reading its values I want to sort them by Key(x),values(0,1,1), Key(y),values(43,90,83) and so on.

Thanks a lot for your help.

Devz
  • 5
  • 1
  • 2
  • 9
  • All that has been posted is a program description. However, we need you to [ask a question](http://stackoverflow.com/help/how-to-ask). We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know [what is on-topic here](http://stackoverflow.com/help/on-topic), asking us to write the program for you and suggestions are off-topic. – gla3dr Oct 13 '15 at 15:32
  • @gla3dr I just edited my question. Is it ok now? – Devz Oct 13 '15 at 15:41
  • how is your list contains but alphabets like x,y,z and integers? – Kumar Abhinav Oct 13 '15 at 15:44
  • @KumarAbhinav my ASIS series is : "Map> series= new TreeMap>();" and TOBE "Map> series = new TreeMap>();" – Devz Oct 13 '15 at 16:03

2 Answers2

0

Obviously

Map<String, Map<String, Integer>> mapOfMaps = ...
TreeMap<String, List<Integer>> series = mapOfMaps.values().stream()
        .map(Map::entrySet)
        .flatMap(Set::stream)
        .collect(Collectors.groupingBy(Entry::getKey, TreeMap::new,
                Collectors.mapping(Entry::getValue, Collectors.toList())));

http://ideone.com/LHYhE7

zapl
  • 63,179
  • 10
  • 123
  • 154
  • thanks a lot zapl. I fondly appreciate your help. I am struggling to get clear the concepts map,treemap,list, listarray. Can you suggest me some tutorials please? Thanks again – Devz Oct 13 '15 at 16:27
  • @Devz I'm not sure what your level is but if your struggle is fundamental, http://stackoverflow.com/tags/java/info lists some nice tutorials & courses. Especially those EDX/Coursera/Udemy/.. learning platforms have typically excellent (free) courses that should cover lists and maps and those things in details. – zapl Oct 13 '15 at 17:01
  • Hi again! @zapl, the solution you proposed works just fine with JRE8 but when I switch to JRE 7.80 and set eclipse build path to 1.7. (For deployment reason) error "- Method references are allowed only at source level 1.8 or above" – Devz Nov 25 '15 at 14:12
  • @Devz That is correct, it's not compatible with Java 7 at all. But if you read the description of all those Java 8 stream operations, they typically give you a description of what they do in plain old Java. With that, you should be able to translate this. – zapl Nov 25 '15 at 15:22
0

You cannot transform data using a Map. You organize 'transformed data' into a map. Organizing the data can be done with methods exposed in String class.

It is safe to assume that the following is a String at your end.

[a={x=0, y=43, z=57}, b={x=1, y=90, z=9}, c={x=1, y=83, z=16}]

Use String API to get hold of each of the set content. http://docs.oracle.com/javase/7/docs/api/java/lang/String.html You can look at methods like indexOf or split or substring. This helps you divide the string into chunks of {.....}.

You now have to traverse and identify the integer values for x,y,z and as you find them, you can add them to a List. Example: ArrayList.

Finally, you add the string x,y,z and their corresponding ArrayLists to a TreeMap.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47