To implement the SQL GROUP BY function in Java using Comparator, the comparator will compare your column data and sort them. Basically, if you save sorted data that looks like grouped data, for example, if you have the same duplicate column data, then sort the mechanism by sorting them, keeping the same data on the one hand, and then look for other data that are heterogeneous data. This is indirectly viewed as GROUPING from the same data.
public class GroupByFeatureInJava { public static void main(String[] args) { ProductBean p1 = new ProductBean("P1", 20, new Date()); ProductBean p2 = new ProductBean("P1", 30, new Date()); ProductBean p3 = new ProductBean("P2", 20, new Date()); ProductBean p4 = new ProductBean("P1", 20, new Date()); ProductBean p5 = new ProductBean("P3", 60, new Date()); ProductBean p6 = new ProductBean("P1", 20, new Date()); List<ProductBean> list = new ArrayList<ProductBean>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); list.add(p5); list.add(p6); for (Iterator iterator = list.iterator(); iterator.hasNext();) { ProductBean bean = (ProductBean) iterator.next(); System.out.println(bean); } System.out.println("******** AFTER GROUP BY PRODUCT_ID ******"); Collections.sort(list, new ProductBean().new CompareByProductID()); for (Iterator iterator = list.iterator(); iterator.hasNext();) { ProductBean bean = (ProductBean) iterator.next(); System.out.println(bean); } System.out.println("******** AFTER GROUP BY PRICE ******"); Collections.sort(list, new ProductBean().new CompareByProductPrice()); for (Iterator iterator = list.iterator(); iterator.hasNext();) { ProductBean bean = (ProductBean) iterator.next(); System.out.println(bean); } } } class ProductBean { String productId; int price; Date date; @Override public String toString() { return "ProductBean [" + productId + " " + price + " " + date + "]"; } ProductBean() { } ProductBean(String productId, int price, Date date) { this.productId = productId; this.price = price; this.date = date; } class CompareByProductID implements Comparator<ProductBean> { public int compare(ProductBean p1, ProductBean p2) { if (p1.productId.compareTo(p2.productId) > 0) { return 1; } if (p1.productId.compareTo(p2.productId) < 0) { return -1; }
The output here for the ProductBean list above is executed with GROUP BY criteria, here, if you see the input data that is listed in the ProductBean list for Collections.sort (list, Comparator object for the desired column). It will be sorted based on your comparator implementation, and you can see the GROUPED data in the lower format. Hope this helps ...
******** BEFORE GROUPING INPUT DATA LOOKS THIS WAY ******
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 30 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P2 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P3 60 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
******** AFTER GROUP BY PRODUCT_ID ******
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 30 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P2 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P3 60 Mon Nov 17 09:31:01 IST 2014]
******** AFTER GROUP BY PRICE ******
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P2 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 20 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P1 30 Mon Nov 17 09:31:01 IST 2014]
ProductBean [P3 60 Mon Nov 17 09:31:01 IST 2014]
Ravi Beli Nov 17 '14 at 4:14 2014-11-17 04:14
source share