Guest User

Untitled

a guest
Mar 17th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package q42847216;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Random;
  10.  
  11. import com.google.common.collect.ArrayListMultimap;
  12. import com.google.common.collect.Ordering;
  13. import com.google.common.collect.TreeMultimap;
  14.  
  15. public class Example {
  16.    
  17.     static final Random r = new Random();
  18.  
  19.     // An example product class.
  20.     static class ABProduct {
  21.        
  22.         // I don't have a database so just make random values.
  23.         private final int category = r.nextInt(5);
  24.         private final String id = String.format("%08X", r.nextInt());
  25.         private final float unitCost = r.nextFloat() * 100.0f;
  26.         private final float operatingCost = r.nextFloat() * 100.0f;
  27.        
  28.         public int getCategory () { return category; }
  29.         public String getId () { return id; }
  30.         public float getUnitCost () { return unitCost; }
  31.         public float getOperatingCost () { return operatingCost; }
  32.        
  33.         // Do our calculations here (made up math, I don't know your real math).
  34.         public float getPaybackTime () {
  35.             return Math.abs(unitCost - operatingCost) / 10.0f;
  36.         }
  37.        
  38.         @Override public String toString () {
  39.             return String.format("%8s category=%d ucost=%.2f ocost=%.2f pbtime=%.2f",
  40.                     id, category, unitCost, operatingCost, getPaybackTime());
  41.         }
  42.        
  43.     }
  44.    
  45.     // A comparator for sorting by payback time.
  46.     static class PaybackTimeComparator implements Comparator<ABProduct> {
  47.  
  48.         @Override public int compare (ABProduct x, ABProduct y) {
  49.             // Highest payback time sorts first.
  50.             return Float.compare(y.getPaybackTime(), x.getPaybackTime());
  51.         }
  52.        
  53.     }
  54.    
  55.     public static void main (String[] args) {
  56.        
  57.         // 1000 random products, pretend this is a database query of all products
  58.         Collection<ABProduct> products = new ArrayList<ABProduct>(1000);
  59.         for (int n = 0; n < 1000; ++ n)
  60.             products.add(new ABProduct());
  61.        
  62.         // Two demos here, one with an ArrayListMultimap, one with a TreeMultimap:
  63.        
  64.         System.out.println("Best products, ArrayListMultimap:");
  65.         for (ABProduct bestProduct : exampleWithArrayList(products))
  66.             System.out.println("  " + bestProduct);
  67.        
  68.         System.out.println("Best products, TreeMultimap:");
  69.         for (ABProduct bestProduct : exampleWithTree(products))
  70.             System.out.println("  " + bestProduct);
  71.        
  72.     }
  73.    
  74.     // This is an example that uses Guava's ArrayListMultimap.
  75.     static Collection<ABProduct> exampleWithArrayList (Collection<ABProduct> products) {
  76.        
  77.         ArrayListMultimap<Integer,ABProduct> productsByCategory = ArrayListMultimap.<Integer,ABProduct>create();
  78.         Collection<ABProduct> bestProductsByCategory = new LinkedList<ABProduct>();
  79.        
  80.         // Divide products into categories.
  81.         for (ABProduct product : products)
  82.             productsByCategory.put(product.getCategory(), product);
  83.        
  84.         // For each category, find the one with the highest payback time.
  85.         for (Integer category : productsByCategory.keySet()) {
  86.             // Get products in category and sort.
  87.             List<ABProduct> productsInCategory = productsByCategory.get(category);
  88.             Collections.sort(productsInCategory, new PaybackTimeComparator());
  89.             // Store the highest one so we can do something with it later (we know productsInCategory is not empty).
  90.             bestProductsByCategory.add(productsInCategory.get(0));
  91.         }
  92.        
  93.         return bestProductsByCategory;
  94.        
  95.     }
  96.    
  97.     // This is an example that uses Guava's TreeMultimap.
  98.     static Collection<ABProduct> exampleWithTree (Collection<ABProduct> products) {
  99.        
  100.         TreeMultimap<Integer,ABProduct> productsByCategory = TreeMultimap.<Integer,ABProduct>create(
  101.                     Ordering.natural(), // Natural ordering for categories.
  102.                     new PaybackTimeComparator() // Payback time ordering for products.
  103.                 );
  104.         Collection<ABProduct> bestProductsByCategory = new LinkedList<ABProduct>();
  105.        
  106.         // Divide products into categories.
  107.         for (ABProduct product : products)
  108.             productsByCategory.put(product.getCategory(), product);
  109.        
  110.         // For each category, the tree map already has it sorted for us.
  111.         for (Integer category : productsByCategory.keySet()) {
  112.             // Store the highest one so we can do something with it later (we know productsInCategory is not empty).
  113.             bestProductsByCategory.add(productsByCategory.get(category).first());
  114.         }
  115.        
  116.         return bestProductsByCategory;
  117.  
  118.     }  
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment