Advertisement
DulcetAirman

group by in Java

Aug 13th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package ch.claude_martin;
  2.  
  3. import java.security.SecureRandom;
  4. import java.time.LocalDate;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.Random;
  10. import java.util.stream.Collectors;
  11.  
  12. public class SomeClass {
  13.  
  14.   /** This is the data bean. It's a plain old java object. */
  15.   public static class Bike {
  16.  
  17.     private String modelName;
  18.     private LocalDate purchaseDate;
  19.     private String contact;
  20.     private int price;
  21.  
  22.     public String getModelName() {
  23.       return modelName;
  24.     }
  25.  
  26.     public void setModelName(String modelName) {
  27.       this.modelName = modelName;
  28.     }
  29.  
  30.     public LocalDate getPurchaseDate() {
  31.       return purchaseDate;
  32.     }
  33.  
  34.     public void setPurchaseDate(LocalDate purchaseDate) {
  35.       this.purchaseDate = purchaseDate;
  36.     }
  37.  
  38.     public String getContact() {
  39.       return contact;
  40.     }
  41.  
  42.     public void setContact(String contact) {
  43.       this.contact = contact;
  44.     }
  45.  
  46.     public int getPrice() {
  47.       return price;
  48.     }
  49.  
  50.     public void setPrice(int price) {
  51.       this.price = price;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.       return String.format("Bike(%s, %s, %d, %s)", modelName, purchaseDate, price, contact);
  57.     }
  58.   }
  59.  
  60.   /**
  61.    * This is just a simple pair, but Java doesn't have a type for this. You could
  62.    * use Map.Entry for this. For a group over 3 fields you need a three-tuple
  63.    * instead.
  64.    */
  65.   public static class Pair<A, B> {
  66.     private A a;
  67.     private B b;
  68.  
  69.     public Pair(A a, B b) {
  70.       super();
  71.       this.a = a;
  72.       this.b = b;
  73.     }
  74.  
  75.     @Override
  76.     public int hashCode() {
  77.       return a.hashCode() + b.hashCode() * 31;
  78.     }
  79.  
  80.     @Override
  81.     public boolean equals(Object obj) {
  82.       if (this == obj)
  83.         return true;
  84.       if (obj == null || getClass() != obj.getClass())
  85.         return false;
  86.       Pair other = (Pair) obj;
  87.       return this.a.equals(other.a) && this.b.equals(other.b);
  88.     }
  89.  
  90.     @Override
  91.     public String toString() {
  92.       return String.format("Group(%s, %s)", a, b);
  93.     }
  94.   }
  95.  
  96.   public static void main(String[] args) {
  97.     final List<Bike> bikes = new ArrayList<>();
  98.     final List<String> contacts = List.of("Bob", "Jill", "Jack", "Susie", "Burt", "Emma");
  99.     final Random rng = new SecureRandom();
  100.     for (int month = 1; month < 13; month++) {
  101.       for (int day = 1; day < 29; day++) {
  102.         for (int i = 0; i < rng.nextInt(4); i++) {
  103.           for (String contact : contacts) {
  104.             Bike b = new Bike();
  105.             b.setContact(contact);
  106.             b.setPurchaseDate(LocalDate.of(2018, month, day));
  107.             b.setPrice(rng.nextInt(5000) + 500);
  108.             b.setModelName(Long.toString(Math.abs(rng.nextLong() % 3656158440062976L), 36));
  109.             bikes.add(b);
  110.           }
  111.         }
  112.       }
  113.     }
  114.     System.out.println("Database is ready");
  115.  
  116.     Map<Pair<String, LocalDate>, List<Bike>> groups = bikes.stream()
  117.         .collect(Collectors.groupingBy(b -> new Pair<>(b.getContact(), b.getPurchaseDate())));
  118.  
  119.     for (Entry<Pair<String, LocalDate>, List<Bike>> entry : groups.entrySet()) {
  120.       System.out.println("=====");
  121.       System.out.print(entry.getKey());
  122.       System.out.print(": ");
  123.       System.out.println(entry.getValue());
  124.     }
  125.     System.out.println("=====");
  126.     System.out.println("THE END");
  127.  
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement