Advertisement
Guest User

Untitled

a guest
May 4th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. ArrayList<Object[]> csvArray = new ArrayList<Object[]>();
  2.  
  3. Stream<Person> people = Stream.of(new Person("Paul", 24), new Person("Mark",30), new Person("Will", 28));
  4. Map<Integer, List<String>> peopleByAge = people
  5. .collect(groupingBy(p -> p.age, mapping((Person p) -> p.name, toList())));
  6. System.out.println(peopleByAge);
  7.  
  8. final List<Object[]> data = new ArrayList<>();
  9. data.add(new Object[]{"NL", "Rotterdam", "Kees", 38});
  10. data.add(new Object[]{"NL", "Rotterdam", "Peter", 54});
  11. data.add(new Object[]{"NL", "Amsterdam", "Suzanne", 51});
  12. data.add(new Object[]{"NL", "Rotterdam", "Tom", 17});
  13.  
  14. final Map<String, List<Object[]>> map = data.stream().collect(
  15. Collectors.groupingBy(row -> row[0].toString() + ":" + row[1].toString()));
  16.  
  17. for (final Map.Entry<String, List<Object[]>> entry : map.entrySet()) {
  18. final double average = entry.getValue().stream()
  19. .mapToInt(row -> (int) row[3]).average().getAsDouble();
  20. System.out.println("Average age for " + entry.getKey() + " is " + average);
  21. }
  22.  
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27.  
  28. public class CSVExample {
  29.  
  30. public static void main(String[] args) {
  31. ArrayList<Information> csvArray = new ArrayList<>();
  32.  
  33. csvArray.add(new Information(new Object[] {"France", "Paris", "Pierre", 34}));
  34. csvArray.add(new Information(new Object[] {"France", "Paris", "Madeleine", 26}));
  35. csvArray.add(new Information(new Object[] {"France", "Toulouse", "Sam", 34}));
  36. csvArray.add(new Information(new Object[] {"Italy", "Rom", "Paul", 44}));
  37.  
  38. Map<String, List<Information>> collect = csvArray.stream().collect(Collectors.groupingBy(s -> (s.getCountry() + " " + s.getCity())));
  39. collect.forEach((k, v) -> System.out.println(k + " " + v.stream().collect(Collectors.averagingInt(Information::getAge))));
  40. }
  41. }
  42.  
  43. class Information {
  44. private String country;
  45. private String city;
  46. private String name;
  47. private int age;
  48.  
  49. public Information(Object[] information) {
  50. this.country = (String) information[0];
  51. this.city = (String) information[1];
  52. this.name = (String) information[2];
  53. this.age = (Integer) information[3];
  54.  
  55. }
  56.  
  57. public Information(String country, String city, String name, int age) {
  58. super();
  59. this.country = country;
  60. this.city = city;
  61. this.name = name;
  62. this.age = age;
  63. }
  64.  
  65. public String getCountry() {
  66. return country;
  67. }
  68.  
  69. public String getCity() {
  70. return city;
  71. }
  72.  
  73. public String getName() {
  74. return name;
  75. }
  76.  
  77. public int getAge() {
  78. return age;
  79. }
  80.  
  81. @Override
  82. public String toString() {
  83. return "Information [country=" + country + ", city=" + city + ", name=" + name + ", age=" + age + "]";
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement