Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package q42847216;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Random;
- import com.google.common.collect.ArrayListMultimap;
- import com.google.common.collect.Ordering;
- import com.google.common.collect.TreeMultimap;
- public class Example {
- static final Random r = new Random();
- // An example product class.
- static class ABProduct {
- // I don't have a database so just make random values.
- private final int category = r.nextInt(5);
- private final String id = String.format("%08X", r.nextInt());
- private final float unitCost = r.nextFloat() * 100.0f;
- private final float operatingCost = r.nextFloat() * 100.0f;
- public int getCategory () { return category; }
- public String getId () { return id; }
- public float getUnitCost () { return unitCost; }
- public float getOperatingCost () { return operatingCost; }
- // Do our calculations here (made up math, I don't know your real math).
- public float getPaybackTime () {
- return Math.abs(unitCost - operatingCost) / 10.0f;
- }
- @Override public String toString () {
- return String.format("%8s category=%d ucost=%.2f ocost=%.2f pbtime=%.2f",
- id, category, unitCost, operatingCost, getPaybackTime());
- }
- }
- // A comparator for sorting by payback time.
- static class PaybackTimeComparator implements Comparator<ABProduct> {
- @Override public int compare (ABProduct x, ABProduct y) {
- // Highest payback time sorts first.
- return Float.compare(y.getPaybackTime(), x.getPaybackTime());
- }
- }
- public static void main (String[] args) {
- // 1000 random products, pretend this is a database query of all products
- Collection<ABProduct> products = new ArrayList<ABProduct>(1000);
- for (int n = 0; n < 1000; ++ n)
- products.add(new ABProduct());
- // Two demos here, one with an ArrayListMultimap, one with a TreeMultimap:
- System.out.println("Best products, ArrayListMultimap:");
- for (ABProduct bestProduct : exampleWithArrayList(products))
- System.out.println(" " + bestProduct);
- System.out.println("Best products, TreeMultimap:");
- for (ABProduct bestProduct : exampleWithTree(products))
- System.out.println(" " + bestProduct);
- }
- // This is an example that uses Guava's ArrayListMultimap.
- static Collection<ABProduct> exampleWithArrayList (Collection<ABProduct> products) {
- ArrayListMultimap<Integer,ABProduct> productsByCategory = ArrayListMultimap.<Integer,ABProduct>create();
- Collection<ABProduct> bestProductsByCategory = new LinkedList<ABProduct>();
- // Divide products into categories.
- for (ABProduct product : products)
- productsByCategory.put(product.getCategory(), product);
- // For each category, find the one with the highest payback time.
- for (Integer category : productsByCategory.keySet()) {
- // Get products in category and sort.
- List<ABProduct> productsInCategory = productsByCategory.get(category);
- Collections.sort(productsInCategory, new PaybackTimeComparator());
- // Store the highest one so we can do something with it later (we know productsInCategory is not empty).
- bestProductsByCategory.add(productsInCategory.get(0));
- }
- return bestProductsByCategory;
- }
- // This is an example that uses Guava's TreeMultimap.
- static Collection<ABProduct> exampleWithTree (Collection<ABProduct> products) {
- TreeMultimap<Integer,ABProduct> productsByCategory = TreeMultimap.<Integer,ABProduct>create(
- Ordering.natural(), // Natural ordering for categories.
- new PaybackTimeComparator() // Payback time ordering for products.
- );
- Collection<ABProduct> bestProductsByCategory = new LinkedList<ABProduct>();
- // Divide products into categories.
- for (ABProduct product : products)
- productsByCategory.put(product.getCategory(), product);
- // For each category, the tree map already has it sorted for us.
- for (Integer category : productsByCategory.keySet()) {
- // Store the highest one so we can do something with it later (we know productsInCategory is not empty).
- bestProductsByCategory.add(productsByCategory.get(category).first());
- }
- return bestProductsByCategory;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment