Comparing two Objects

Hi, I want to compare two objects
using the formula below
if((Integer)v.elementAt(i) == new Integer(comp.getCard(j).getValue())){but it doesn't work
I am trying to compare an element in a vector which was of a type Integer
key1 = new Integer(dk.getCard(f).getValue());note that key1 is actually a key used for a mapping
can anyone tell me how I can compare the elements in vector and the other object was an int.

A: Comparing two Objects

Hello zainuluk,
You can not use '==' operator to compare to objects in Java, 'cause it just compare the reference of two oprands. You should use the "equals()" method, just like this:
if(Integer)v.elementAt(i).equals(new Integer(comp.getCard(j).getValue())){...}
Or you can get the int value of element in the vector to compare using '==' operator:
if(Integer)v.elementAt(i).intValue() == comp.getCard(j).getValue()){..}

Hello zainuluk,
You can not use '==' operator to compare to objects in Java, 'cause it just compare the reference of two oprands. You should use the "equals()" method, just like this:
if(Integer)v.elementAt(i).equals(new Integer(comp.getCard(j).getValue())){...}
Or you can get the int value of element in the vector to compare using '==' operator:
if(Integer)v.elementAt(i).intValue() == comp.getCard(j).getValue()){..}

Similar Messages

  • How to compare two Objects !!!!

    Hi All,
    I know that this question has been asked 100 times till now. But trust me I have checked all of them but couldn`t find answer. Here is my question:
    I have an objecs. In that object I am setting (Id,Name,DOJ). Now I want to check whether the object I am trying to save in database already exists or not. If exists then I need to check whether all the setters are same for the two objects. Now can anyone tell me ,how to compare two objects directly without comparing the setters individually.
    Thanks in advance.

    pavan13 wrote:
    That is pretty good idea. However I have a query. Does that code actually compare all the setters in a two beans. I don`t want to check each setter seperately.Well, it's pretty meaningless to talk about "comparing setters", setters are methods, they don't have values to compare. Because equals is inside the class, you can simply compare the fields that get set by the setters. "Properties" is probably a better name.
    In principal you could write something that tried to find all relevant fields and compare them, using reflection or bean info stuff. The resulting code would be about 50 times longer and take about 50 times longer to run. It's hardly asking a lot to put three comparisons between && operators.
    Remember, though, not to compare string fields with ==, you should call .equals on the string fields.
    p.s. don't let the bean terminology confuse you. Beans are just ordinary objects which follow a few rules. Personally I wish the term had never been coined.
    Edited by: malcolmmc on Dec 6, 2007 4:15 PM

  • Compare two objects

    If I want to make a program that have say 2 person objects.
    I have info about these two persons like name , id number , age......
    Then I want sort these two persons after age.
    that is that info about the person that is youngest comes first in the massage window.
    I have to enter information about these two persons and get info of bot hof them in the same massage window.
    Can someone here give me example of how I can do that????

    This could be a hint ..
    //If you do not use 5.0, omit "<Object>" in the code.
    import java.util.ArrayList;
    import java.util.*;
    class Student{
         String id;
         int yr;
         Student(String studentId, int year){
              this.id = studentId;
              this.yr = year;
         public String toString(){
              return id + " " + String.valueOf(yr);
    class OrderStudents implements java.util.Comparator<Object>{
         public int compare(Object a, Object o){
            if(!(a instanceof Student)||!(o instanceof Student)) throw new IllegalArgumentException();
             Student stuA = (Student)a;
              Student stuO = (Student)o;
              int n;
            if((n=(stuA.id).compareTo(stuO.id))!=0) return n;
            else if(stuA.yr - stuO.yr==0)return 0;
              return (stuA.yr - stuO.yr)>0? 1:-1;
    public class CompareStudents{
      public static void main(String[] args){
              List<Object> list = new ArrayList<Object>();
               list.add(new Student("D3", 2004));
               list.add(new Student("D3", 2001));
               list.add(new Student("D1", 2001));
               list.add(new Student("D3", 2002));
               list.add(new Student("D1", 2003));
               list.add(new Student("D1", 2002));
              java.util.Collections.sort(list, new OrderStudents());
              System.out.println(list.toString());
    }

  • Compare two similar objects

    Do you know about any possible Java bugs with JDK update?
    In my case, my code stopped working.
    Here is what I have. There are two similar objects, but Object 1 has key, and Object 2 (same as Object 1) has no key. I need to compare both, and if they are equal, I need Object 1 to assign key to Object 2.
    It worked before. How I can get around.
    Please help me if you can.
    Respectfully,
    Alex

    Let me change the question:
    "Can we compare two objects one with key and another one without key? How?

  • How to compare two database objects?

    Hi,
    I need to compare two objects with embedded objects/collections in it to tell exact difference between the two.
    e.g.,
    create type t1 as object (name varchar2(10), age number);
    create type t2 as table of t1;
    create type t3 as object (user t1, family t2);
    ... and so on.. this type hirarechy is big in my case..
    I want a procedure/function which can tell me what and where is the difference.
    PROCEDURE (obj1 T1 , obj2 T1) IS
    BEGIN
    END;
    e.g., t1.name = 'John' and t2.name = 'Mich' then it should return t1.name etc..
    Is it possible?
    Thanks

    Why a new thread?
    how to read objects/collections dynamically?

  • Compare 2 objects - urgent

    hi all
    i have a problem.
    I want to compare two objects but dono how.
    Object obj1=null, obj2=null;
    Vector vec = new Vector()
    obj1=vec
    Vector vec1=new Vector()
    obj2=vec
    now i want to compare if(obj1 == obj2)
    how can i do this
    help out plzzzzzzzzzzzzzzzzz
    thanx

    Nope, I'm still scratching my head over this one.
    However, there are two comparisions in java.
    ==, and .equals() method
    == just checks to see if the Object is the same Object
    .equals() is defined to check if one Object is equal to another.
    quick example:
      String s1 = new String("Hello");
      String s2 = s1;
      String s3 = new String("Hello");
      String s4 = new String("GoodBye");
      s1 == s2;  // true because they are the same object
      s1.equals(s2) // true because they have the same underlying value
      s1 == s3  // false because they are different objects
      s1.equals(s3) // true, because the are the same underlying value
      s1 == s3 // false
      s1.equals(s4)// also falseAny time you want to compare the VALUE of two objects, use the .equals method.
    So in this case,
    if (oldObj.equals(newObj)) is the way to go
    If you are dealing with vectors/lists, the equals method will compare every item in oldObj with every item in newObj. If they are all equal, then it will return true.
    Hope this helps,
    evnafets

  • How to compare the object's properties

    Hi ,
    I have a requirement like to compare two objects data. But the object will have different types of parameters and it will have some other classes as properties. My utility has to compare for all the properties of the object including other classes properties which are included as properties in the main object also.
    Can we achieve using java reflection this requirement.
    If any one has idea reg this , plz help me.
    thanks
    Prakash

    hari_honey wrote:
    No , actually i have to generate the change log of object data.
    If any property of the object is changed and that has to go to the change log table with the
    property name and old and new value.Sounds expensive and complex.
    Presumably you can't change the code.
    Presumably you can't provide a proxy layer.
    Have you consider code injection?

  • Compare two Java Objects without Comparable / Comparator

    Hi Friends,
    I would like to compare two java objects (lots o attributes) without using Comparable / Comparator.
    Any suggestion/sample code would be helpful.
    Thanks,
    Sachin

    I suppose you could design another feature to compare Objects... but that would involve a design process, so asking for sample code is definitely premature. And as EJP says, what would be the point?
    At least that's what I would answer if this was one of those stupid interview questions.

  • Adobe XI Pro trial verison, when compared two pdf files it gives error  "Expected a Name Object"..Can some one say how to fix this?

    Installed Adobe XI Pro trial verison, when compared two pdf files it gives error  "Expected a Name Object"..Can some one say how to fix this?

    Installed Adobe XI Pro trial verison, when compared two pdf files it gives error  "Expected a Name Object"..Can some one say how to fix this?

  • Comparing two vectors of objects

    Hi,
    This is just a small example of what I am trying to do.
    Trying to just compare the objects of type FileInfo in the vectors, do I have to create a compareTo function or something to define what is equal when comparing the objects? Where would I create this and how would I implement it?
              Vector v1 = new Vector();
              Vector v2 = new Vector();
              v1.addElement(new FileInfo("blah", "name"));
              v1.addElement(new FileInfo("blah", "name"));
              System.out.println(v1.equals(v2));
    // Returns falseThanks

    Only way I have thought of is change the vector to an
    array and use Sort(Object, Compator)
    Seems the easiest way. It is wierd [weird] how Vectors don't
    have any functions that allow you to compare things.Vector DOES compare things. But, it uses "equals" on those objects. If you didn't write an "equals" method for FileInfo, it will only compare the REFERENCES of FileInfo objects, not the data within those objects. I assume you want to have your "equals" methods compare the two "blah"s and the two "name"s (using String.equals). Give it a shot. If your "equals" doesn't work, then post it and tell us what "doesn't work" about it.

  • Images XOR, AND, etc... logical operators to compare two distinct objects

    is there a way of comper two images without comparing all it bytes...
    the problem: actually I�m parsing the values of the bytes of the two images and then comparing it...
    idea: if I have the two byte arrays containing the image info.. may I use a logical operator to compare these objects ??

    FelipeGaucho again,
    Did you come up with an optimized method for logical operators? I actually want to perform the boolean operations to turn most pixels of an image to zero, then compress that (as in your post 'String compressor II'). Are these two posts related for you as well? They are separated by several months.
    abnormal,
    I'm assuming that Java's classes would be optimized, but if my code will execute at the same speed as theirs, then I guess I can stop searching for optimized (or already provided) classes. Do you know if Java's provided classes run faster than classes we create? I'm going to test the provided System.arraycopy against my own while waiting for a reply.

  • Functinality to compare two material price with reference to BOM in CO

    Hi All,
    Is there is any functionality in Controlling which can compare the costing for two FERT materials.
    or
    is there is  any functionality where we can compare two material price with reference to BOM in controlling
    Please help me to compare the material price with reference to BOM
    Regards
    nandu

    Hi,
    You can use this report to compare two itemizations. The report compares the characteristics item number, item category, cost element, resource, material, cost center, plant/work center, cost center/activity type, operation number, BOM item, assembly indicator, and cost component.
    You can access this report as follows:
    Accounting ® Controlling ® Product Cost Controlling ® Product Cost Planning ® Material Costing ® Cost Estimate with Quantity Structure or Cost Estimate Without Quantity Structure ® Compare
    or
    Accounting ® Controlling ® Product Cost Controlling ® Product Cost Planning ® Information System ® Object Comparisons ® For Material ® Itemization Comparison.
    For Detail Please reffer following link:
    http://help.sap.com/saphelp_46c/helpdata/en/56/abd108f1a611d28a950000e8214595/content.htm
    Thanks and Regards
    Binoj M D

  • Compare two schemas in oracle 11g

    Hi All,
    Can any one please help me how to compare two schemas in oracle 11g.
    Thanks
    Edited by: 793914 on Oct 22, 2010 9:33 AM

    You can do it with gridcontrol. Look for dictionary comparisons and dictionary baselines.
    If you don't have gridcontrol, you can also use SQLDeveloper (free download on otn).
    There are two ways to do it with SQLDeveloper.
    1) Use Tools->Database Diff. However, you need a change management license to use this functionality. I've never used this.
    The free way is to...
    2) Use Tools->Database Export. Run it once for each schema and make sure the "Show Schema" option is NOT checked before exporting the schema. Also make sure you don't export data. You can choose which types of objects to export as well.
    Once you have the two export files for the two schemas then simply run a diff tool on your platform. Use the diff command line for linux or winDiff or something similar in windows.

  • Compare Two Structures or Itab at Runtime

    Hi Experts;
    I want to do compare two structures or itabs in my program at runtime.
    I will give two structures or itab names and I want to get difference between two structures names or itabs.
    I know Diff Screen of ABAP Debug . I want to make same process but only with FM or Class.
    Are there any FMs or Classes working this way?
    Best regards.

    I created my interface success . But It give error at runtime. Error mesaage : 'Error during interprocess communication; Debugger will be closed . Message Number TPDA151'
    DATA l_ref_diff TYPE REF TO CL_TPDA_DIFF_STRUC.
    data : xyz  TYPE REF TO IF_TPDA_DIFF_SERVICE.
    data : p_var1 type TPDA_VAR_NAME,
              p_var2 type TPDA_VAR_NAME,
              lv_x type TPDA_DIFF_ATTR.
    data : l_it_value_diff TYPE TPDA_DIFF_VALUE_DIFF_IT,
              l_it_main_diff TYPE tpda_diff_main_diffs_it.
    p_var1 = 'CLIENTDATA1'.
    p_var2 = 'CLIENTDATA2'.
    lv_x-maxhits = 100.
    CREATE OBJECT l_ref_diff.
    xyz ?= l_ref_diff.
    xyz->init( ).
    xyz->DIFF( EXPORTING p_var1 = p_var1
                         p_var2 = p_var2
                         p_diff_attr = lv_x
               CHANGING p_it_value_diff_it = l_it_value_diff
                        p_it_main_diff_it  = l_it_main_diff ).

  • Performance issues comparing two vectors

    I need some advice on what is the fastest way to compare two vectors. My question is all about performance;
    I have two vectors one(Vect A) of which has more than 20000 string values and the other Vector B would hold typically around 200 strings.I compare them both to find out if Vector B has values in Vector A and remove those.
    I iterate thro each element in a loop from the smaller to the bigger vector comparing element by element.But it takes a long time.
    Is there any quick way to do this comparison and can I use contains/compare methods instead of iterating thro the vectors.will it speeden up things
    Arn

    first of all, you should probably examine your collection choice and make sure that java.util.Vector is your best option (assumming you can change the class). Do not use a Hashtable as the previous poster suggested- it doesn't sound like you have a key/value type relationship in your elements, making a Map implementation superflous. Will there be duplicate elements in either Collection? If you will not have duplicate elements, you should probably use a Set collection instead of a List. Does your collection have to be thread-safe? If not, one of the unsynchronized collections should give you better performance (e.g. ArrayList should be quicker then Vector). For the best possible performance, you could use an ordered collection and write your own comparator, so that it does not iterate beyond what ever element assurres that there is no match (i.e. if the first element is "fred" in an ordered collection, you know there will be no match for "adam"). Sun has some good tutorials on the java.util.Collection package that will help you understand the benefit of each implemenation...
    although, and I suppose this is neither here nor there, I wouldn't use java objects to sort through 20K string items- sounds like storing them in a database might be superior. Take care.

Maybe you are looking for