Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to compare 2 different type object list

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to compare 2 different type object list

    hi,

    First of all, my english is not good and I'm new at java.

    I have two different classes as shown below, and 2 lists

    How can I compare these two lists without iterative code?

    I need a new list which is support this condition
    if (a.id = b.rowNumber)

    List<class A> list1;
    List<class B> list2;

    	public class A
    	{
    		private int id;
    		private String description;
     
    		getters...
    		setters...
     
    		ctor...
    	}

    and

    	public class B
    	{
    		private int rowNumber;
    		private String description;
    		private double price;
    		private String adress;
     
    		getters...
    		setters...
     
    		ctor...
    	}

    thanx...


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to compare 2 different type object list

    Its been quite a while for me, but if I remember correctly you need to use an interface for your class a and b and write a compare method.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: how to compare 2 different type object list

    Um, there are a few ways of doing this. One way that might work is by making one (or both) of the classes implement the Comparable Interface, and then write the code to compare the two of them in the compareTo(...) method. But you would have to cast the Object sent to the compareTo method into one of your objects so you have access to their variables. This way might also lead to several runtime errors if you send the wrong Object to the method. I'm not entirely sure how to describe this without doing it for you (which I don't want to do).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: how to compare 2 different type object list

    Override equals and hashCode to define object equality. This allows you to use the Collections framework directly through a Set, or using List methods to retain or remove equal objects or Collections of objects.

Similar Threads

  1. [SOLVED] what data type should i use in MySql for a java object to be saved
    By chronoz13 in forum JDBC & Databases
    Replies: 4
    Last Post: October 9th, 2011, 07:17 AM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. How to perform operations on arguments with type OBJECT?
    By hexwind in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 3rd, 2011, 12:17 PM
  4. saving list of data from JSP into java object
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: January 12th, 2011, 10:24 AM
  5. store a list/string for each object
    By tommy in forum Object Oriented Programming
    Replies: 5
    Last Post: May 28th, 2010, 09:24 AM