469,268 Members | 942 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Compare two lists contaning different objects

77
Dear all,

I have a list object, which contains list objects. This list objects contains objects of different type like String, long, BigDecimal and so on..

for eg.,
if we print it in console it will be like this,

list object is :
[
[jerald,26,"#32 chennai", 43.0098],
[Roses,25,"#06 kanya kumari", 13.00108]
]

this example object has two records and should be compared with similar two record list object. comparision should be done by its data type. For eg., BigDecimal objects , we shoud take care of comparing like this,

bigdecimal1.compareTo(bigdecimal2)

I have an array of datatype {String, int,String,BigDecimal} to compare. please any one help.
Apr 25 '09 #1
8 32139
JosAH
11,448 Expert 8TB
You should've made a little class for every 'row' in your List. That little class should implement the Comparable interface; the rest would be easy then. Your approach is too Fortanesque.

kind regards,

Jos

ps. and get rid of those arrays as well.
Apr 25 '09 #2
pjerald
77
Thank you JosAH,

I am trying to do this. I have written some code. I will reply when i am finished.

Regards
jerald
Apr 26 '09 #3
pjerald
77
Dear josAH,

Here is my code,


Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class ExpenseComparator {
  8.     public static void main(String[] args) throws Exception
  9.     {
  10.         List<Object> expected = new ArrayList();
  11.         List<Object> received = new ArrayList();
  12.  
  13.         List<Object> exp1 = new ArrayList();
  14.         List<Object> exp2 = new ArrayList();
  15.         List<Object> rec1 = new ArrayList();
  16.         List<Object> rec2 = new ArrayList();
  17.  
  18.         exp1.add(Long.parseLong("100"));
  19.         exp1.add(new BigDecimal("100.009"));
  20.         exp1.add("32, cross cut road. Chennai.");
  21.  
  22.         exp2.add(Long.parseLong("100"));
  23.         exp2.add(new BigDecimal("100.00"));
  24.         exp2.add("32, cross cut road. Chennai.");
  25.  
  26.         expected.add(exp1);
  27.         expected.add(exp2);
  28.  
  29.         rec1.add(Long.parseLong("100"));
  30.         rec1.add(new BigDecimal("100.00000"));
  31.         rec1.add("32, cross cut road. Chennai.");
  32.  
  33.         rec2.add(Long.parseLong("100"));
  34.         rec2.add(new BigDecimal("100.009"));
  35.         rec2.add("32, cross cut road. Chennai.");
  36.  
  37.         received.add(rec1);
  38.         received.add(rec2);
  39.  
  40.         int a = compareLists(expected, received);
  41.         if(a == 0)
  42.         {
  43.             System.out.println("Both the lists are equal.");
  44.         }
  45.     }
  46.  
  47.  
  48.     public static int compareLists(List expected, List received) throws Exception
  49.     {
  50.         if(expected.size() != received.size())
  51.         {
  52.             throw new Exception("Records size missmatch.");
  53.         }
  54.         int flag = 0;
  55.         for(int i=0;i<expected.size();i++) // loop through records.
  56.         {
  57.             List expList = (List)expected.get(i);
  58.             CompareRecords tc = new CompareRecords(expList);
  59.             List recList = null;
  60.             for(int j=0;j<received.size();j++)
  61.             {
  62.                 recList = (List) received.get(j);
  63.                 int c = tc.compareTo(recList);
  64.                 {
  65.                     if(c == 0)
  66.                     {
  67.                         received.remove(j);
  68.                         expected.remove(i);
  69.                         flag = 1;
  70.                         break;
  71.                     }
  72.                 }
  73.             }
  74.             if(flag == 0)
  75.             {
  76.             throw new Exception("Excepted record : "+expList + " is not matched with any records.");
  77.             }
  78.         }
  79.         return 0;
  80.     }
  81. }
  82. class CompareRecords implements Comparable
  83. {
  84.     List fList = null;
  85.     public CompareRecords(List l)
  86.     {
  87.         fList = l;
  88.     }
  89.     public int compareTo(List list) throws Exception {
  90.         if(fList.size() != list.size())
  91.         {
  92.             throw new Exception("Columns size missmatch");
  93.             //return -1;
  94.         }
  95.         int flag = 0;
  96.         for(int i = 0;i<fList.size();i++)
  97.         {
  98.             Object fo = fList.get(i);
  99.             Object o = list.get(i);
  100.             if(o instanceof String)
  101.             {
  102.                 String fs = (String)fo;
  103.                 String s = (String)o;
  104.                 if(fs.equals(s))
  105.                 {
  106.                     continue;
  107.                 }
  108.                 flag = 1;
  109.             }
  110.             else if(o instanceof Long)
  111.             {
  112.                 Long fl = (Long)fo;
  113.                 Long l = (Long)o;
  114.                 if(fl.equals(l))
  115.                 {
  116.                     continue;
  117.                 }
  118.                 flag = 1;
  119.             }
  120.             else if(o instanceof BigDecimal)
  121.             {
  122.                 BigDecimal fbd = (BigDecimal)fo;
  123.                 BigDecimal bd = (BigDecimal)o;
  124.                 if(bd.compareTo(fbd) == 0)
  125.                 {
  126.                     continue;
  127.                 }
  128.                 flag = 1;
  129.             }
  130.         }
  131.         if(flag == 1)
  132.         {
  133.             return 1;
  134.         }
  135.         return 0;
  136.     }
  137.  
  138.     public int compareTo(Object o) {
  139.         throw new UnsupportedOperationException("Not supported yet.");
  140.     }
  141.  
  142. }
  143.  
I have some problem with this code. I cannot able to identify which expected column value is not get matched with the exact value in received column value ..

Any comments please.

Regards
Jerald
Apr 26 '09 #4
pjerald
77
So here is my entire code.....


I am comparing two csv files taking care of their data types..


Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import com.csvreader.CsvReader;
  10. import java.io.BufferedReader;
  11. import java.io.InputStreamReader;
  12.  
  13. public class ExpenseComparator {
  14.     public static void main(String[] args) throws Exception
  15.     {
  16.         String[] dataType = {"String", "Long", "String", "BigDecimal"};
  17.         List l1 = getListFromFile("/home/pjerald/test1.xml",dataType);
  18.         List l2 = getListFromFile("/home/pjerald/test2.xml",dataType);
  19.         int b = compareLists(l1, l2);
  20.         if(b == 0)
  21.         {
  22.             System.out.println("Both the lists are equal.");
  23.         }
  24.  
  25.     }
  26.  
  27.  
  28.     public static int compareLists(List expected, List received) throws Exception
  29.     {
  30.         if(expected.size() != received.size())
  31.         {
  32.             throw new Exception("Records size missmatch.");
  33.         }
  34.         int flag = 0;
  35.         for(int i=0;i<expected.size();i++) // loop through records.
  36.         {
  37.             List expList = (List)expected.get(i);
  38.             CompareRecords tc = new CompareRecords(expList);
  39.             List recList = null;
  40.             boolean matched = false;
  41.             for(int j=0;j<received.size();j++)
  42.             {
  43.                 recList = (List) received.get(j);
  44.                 int c = tc.compareTo(recList);
  45.                 {
  46.                     if(c == 0)
  47.                     {
  48.                         received.remove(j);
  49.                         expected.remove(i);
  50.                         matched = true;
  51.                         break;
  52.                     }
  53.  
  54.                 }
  55.             }
  56.             if(matched)
  57.             {
  58.                 continue;
  59.             }
  60.  
  61.                 throw new Exception("Excepted record : "+expList + " is not matched with any records.");
  62.  
  63.         }
  64.         return 0;
  65.     }
  66.  
  67.     private static List getListFromFile(String fileName, String[] dataType) throws FileNotFoundException, IOException, Exception
  68.     {
  69.         CsvReader reader = new CsvReader(new BufferedReader(new InputStreamReader(new FileInputStream(fileName))),',');
  70.         List<List> list = new ArrayList();
  71.         if (!reader.readHeaders())
  72.         {
  73.             throw new Exception("cannot read headers");
  74.         }
  75.         String[] headers = reader.getHeaders();
  76.         int count = -1;
  77.         while (reader.readRecord())
  78.         {
  79.             List sb = new ArrayList();
  80.             for (int i = 0; i < headers.length; i++)
  81.             {
  82.                 count++;
  83.                 String str = reader.get(headers[i]);
  84.                 if (str == null || str.trim().length() == 0)
  85.                 {
  86.                     sb.add(",");
  87.                     continue;
  88.                 }
  89.                 str = str.trim();
  90.                 if(dataType[i].equals("Long"))
  91.                 {
  92.                     Long l = Long.parseLong(str);
  93.                     sb.add(l);
  94.                 }
  95.                 else if(dataType[i].equals("BigDecimal"))
  96.                 {
  97.                     BigDecimal bd = new BigDecimal(str);
  98.                     sb.add(bd);
  99.                 }
  100.                 else
  101.                 {
  102.                     sb.add(str);
  103.                 }
  104.             }
  105.             list.add(sb);
  106.             count = -1;
  107.         }
  108.         return list;
  109.  
  110.     }
  111. }
  112. class CompareRecords implements Comparable
  113. {
  114.     List fList = null;
  115.     public CompareRecords(List l)
  116.     {
  117.         fList = l;
  118.     }
  119.     public int compareTo(List list) throws Exception {
  120.         if(fList.size() != list.size())
  121.         {
  122.             throw new Exception("Columns size missmatch");
  123.             //return -1;
  124.         }
  125.         int flag = 0;
  126.         for(int i = 0;i<fList.size();i++)
  127.         {
  128.             Object fo = fList.get(i);
  129.             Object o = list.get(i);
  130.             if(o instanceof String)
  131.             {
  132.                 String fs = (String)fo;
  133.                 String s = (String)o;
  134.                 if(fs.equals(s))
  135.                 {
  136.                     continue;
  137.                 }
  138.                 flag = 1;
  139.                 break;
  140.             }
  141.             else if(o instanceof Long)
  142.             {
  143.                 Long fl = (Long)fo;
  144.                 Long l = (Long)o;
  145.                 if(fl.equals(l))
  146.                 {
  147.                     continue;
  148.                 }
  149.                 flag = 1;
  150.                 break;
  151.             }
  152.             else if(o instanceof BigDecimal)
  153.             {
  154.                 BigDecimal fbd = (BigDecimal)fo;
  155.                 BigDecimal bd = (BigDecimal)o;
  156.                 if(bd.compareTo(fbd) == 0)
  157.                 {
  158.                     continue;
  159.                 }
  160.                 flag = 1;
  161.                 break;
  162.             }
  163.         }
  164.         return flag;
  165.     }
  166.  
  167.     public int compareTo(Object o) {
  168.         throw new UnsupportedOperationException("Not supported yet.");
  169.     }
  170.  
  171. }
  172.  
But i cannot able to find which column of the record miss matched.
Apr 26 '09 #5
JosAH
11,448 Expert 8TB
That is not what I expected: a row (e.g. from a database table) represents an entity, a class, say a Person. Every row from that table makes up an object from the Person class. A table would simply be a list of Persons:

Expand|Select|Wrap|Line Numbers
  1. List<Person> table= new List<Person>();
  2.  
You can add and remove Person objects to/from this list/table. If you want to compare two Person objects for equality you have to implement the equals() and hashCode() methods in the Person class. The List interface itself will help you to do all sorts of clever things with a bunch of Persons but you do have to write such a class; another list of (unrelated) little objects won't make it.

kind regards,

Jos
Apr 26 '09 #6
pjerald
77
Thanks for the reply.

My understanding is this ...

1) Make objects(Say person) from each and every record.
2) Person class should implement equals and hashcode methods...
3) Then easily compare each and every (Person objects) records for equality.

is am i wright ?

Thanks and Regards
Jerald.
Apr 26 '09 #7
JosAH
11,448 Expert 8TB
@pjerald
Yep, that's basically it; that way you're hiding the details where those Person objects came from, i.e. you just have to deal with those Person objects.

kind regards,

Jos
Apr 26 '09 #8
pjerald
77
Thank you josAH.

I have done the code. Used constructors to make my objects(records from list)

works well. Thanks a lot for your replies.

regards
jerald
Apr 26 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Fazer | last post: by
4 posts views Thread by Nicolas Fleury | last post: by
8 posts views Thread by laniik | last post: by
4 posts views Thread by Gaby | last post: by
7 posts views Thread by Prabhudhas Peter | last post: by
1 post views Thread by Ron | last post: by
50 posts views Thread by titan nyquist | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.