skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, October 29, 2012

Grouping products or items based on common categories

Posted by Raju Gupta at 6:00 PM – 0 comments
 

The objective is to group the items together based on common categories.
For e.g.1, if there are two items(7000090069,7000090070) associated with three categories (264433,264431,264432) as below :
7000090069 --> 264431
7000090069 --> 264433
7000090070 --> 264433
7000090070 --> 264432
then the output will be 7000090069,7000090070_264433,264431,264432
e.g.2, if there are two items(7000090069,7000090070) associated with three categories
(264431,264432) as below :
7000090069 --> 264431
7000090070 --> 264432
then the output will be 7000090069_264431 and 7000090070_264432 in a list.


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {

 /**
  * This class contains items and categories. 
  * The objective is to group the items together based on common categories.
  * For e.g.1, if there are two items(7000090069,7000090070) associated with three categories (264433,264431,264432) as below :
  *   7000090069 --> 264431
  *   7000090069 --> 264433
  *   7000090070 --> 264433
  *   7000090070 --> 264432
  *   then the output will be 7000090069,7000090070_264433,264431,264432
  *   
  *   e.g.2, if there are two items(7000090069,7000090070) associated with three categories (264431,264432) as below :
  *   7000090069 --> 264431
  *   7000090070 --> 264432
  *   then the output will be 7000090069_264431 and 7000090070_264432 in a list.
  */
 static class A {
  long item;
  long category;

  public A(long q, long f) {
   super();
   this.item = q;
   this.category = f;
  }

  public long getItem() {
   return item;
  }

  public void setItem(long q) {
   this.item = q;
  }

  public long getCategory() {
   return category;
  }

  public void setCategory(long f) {
   this.category = f;
  }
 }

 public static void main(String[] args) {
  A[] arr = new A[9];

  arr[0] = new A(7000089688L, 264431);
  arr[1] = new A(7000089707L, 264432);
  arr[2] = new A(7000090069L, 264433);
  arr[3] = new A(7000090047L, 264431);
  arr[4] = new A(7000090051L, 264432);
  arr[5] = new A(7000084083L, 264432);
  arr[6] = new A(7000089688L, 264432);
  arr[7] = new A(7000090070L, 264433);
  arr[8] = new A(7000090070L, 264431);

  List<String> list = new ArrayList<String>();
  int length = 0;

  for (int i = 0; i < arr.length; i++) {
   length = list.size();
   A a = arr[i];
   if (list.isEmpty()) {
    list.add(String.valueOf(a.getItem()) + "_"
      + String.valueOf(a.getCategory()));
    continue;
   }
   boolean added = false;
   for (int j = 0; j < length; j++) {
    String str = list.get(j);
    String[] strArr = str.split("_");
    if (!strArr[0].contains(String.valueOf(a.getItem()))
      && strArr[1].contains(String.valueOf(a.getCategory()))) {
     list.set(j, strArr[0] + "," + String.valueOf(a.getItem())
       + "_" + strArr[1]);
     added = true;
    } else if (strArr[0].contains(String.valueOf(a.getItem()))
      && !strArr[1].contains(String.valueOf(a.getCategory()))) {
     boolean isRemoved = false;
     for (int k = 0; k < length; k++) {
      String str1 = list.get(k);
      String[] strings = str1.split("_");
      if (strings[1]
        .contains(String.valueOf(a.getCategory()))) {
       list.set(
         j,
         strArr[0]
           + ","
           + strings[0]
           + "_"
           + (strArr[1].contains(strings[1]) ? strArr[1]
             : strArr[1] + ","
               + strings[1]));
       list.remove(k);
       length = list.size();
       isRemoved = true;
      }
     }
     if (!isRemoved) {
      list.set(
        j,
        strArr[0] + "_" + strArr[1] + ","
          + String.valueOf(a.getCategory()));
     }
     added = true;
    } else if (strArr[0].contains(String.valueOf(a.getItem()))
      && strArr[1].contains(String.valueOf(a.getCategory()))) {
     added = true;
    }
   }
   if (!added) {
    list.add(String.valueOf(a.getItem()) + "_"
      + String.valueOf(a.getCategory()));
   }
  }

  for (Iterator iterator = list.iterator(); iterator.hasNext();) {
   String string = (String) iterator.next();
   System.out.println(string);
  }
 }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger