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.

Similar Messages

  • 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.

  • Comparing two Vectors

    I have a vector which initially reads a file line by line and is displayed by an applet. The file has 'N' constant lines & is dynamic . When the applet gets refreshed, the file is read into an another Vector. Now how do i compare the two vectors i.e in case there is any change in the new Vector the new 'n' lines should be appended to the old vector by replacing the 'n' lines.
    Thanks

    If I understand your question correctly, you have an input file of fixed length (for argument's sake, we will say 5 lines) as in:
    line #1
    line #2
    line #3
    line #4
    line #5You read these lines into a Vector (we'll call it Vector A). Some time passes, and you look at the file again, and it's contents are:
    line #3
    line #4
    line #5
    line #6
    line #7Now you read these lines into Vector B. This is the part I get confused about. If you are just planning on detecting change(i.e., remove 1 & 2, add 6 & 7), why not just overwrite Vector A with Vector B?
    If you need to know specifically which lines are new, you could write simple methods:
    * This method will return a Vector containing the lines no longer in
    * the file.
    * @param vecOld the Old Vector
    * @param vecNew the New Vector
    * @return a Vector containing entries in Old not found in New
    public Vector findRemovals(Vector vecOld, Vector vecNew) {
      Vector vecReturn = new Vector();
      for (int i = 0; i < vecOld.size(); i++) {
        if (!vecNew.contains(vecOld.get(i))) {
          vecReturn.add(vecOld.get(i));
      return vecReturn;
    * This method will return a Vector containing the new lines in
    * the file.
    * @param vecOld the Old Vector
    * @param vecNew the New Vector
    * @return a Vector containing entries in New not found in Old
    public Vector findAdditions(Vector vecOld, Vector vecNew) {
      Vector vecReturn = new Vector();
      for (int i = 0; i < vecNew.size(); i++) {
        if (!vecOld.contains(vecNew.get(i))) {
          vecReturn.add(vecNew.get(i));
      return vecReturn;
    }I hope this helps. If not, could you post a clearer description of your problem?

  • Comparing two vectors using index of

    Hi
    I have two vectors .Vector 1 holds around 100 strings.Vector 2 holds 10000 strings.I need to compare iterate through the vector to see if the there any strings matching and pull them out.How do I use the index of method to compare the two
    Thanks
    Arnold

    Hi
    I have two vectors .Vector 1 holds around 100
    strings.Vector 2 holds 10000 strings.I need to compare
    iterate through the vector to see if the there any
    strings matching and pull them out.How do I use the
    index of method to compare the two
    Thanks
    ArnoldInstead of using a vector why don't you use a array, this would allow you to sort using a Comparator, also I would try and get away from a Vector, they are syncronized and can be very slow, try using a ArrayList if you must use a Collection.
    If you do use a array, sort it then when you are searching it just cut the 10000 size array in half, this gives you only 5000 and then do the same until you have chunked it down to a min amount, If I am not mistaken this is how db with large bitmap indecs work.
    HTH

  • 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()){..}

  • 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?

  • 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.

  • Sorting a vector of objects using attribute of object class as comparator

    i would like to sort a vector of objects using an attribute of object class as comparator. let me explain, i'm not sure to be clear.
    class MyObject{
    String name;
    int value1;
    int value2;
    int value3;
    i've got a Vector made of MyObject objects, and i would sort it, for instance, into ascending numerical order of the value1 attribute. Could someone help me please?
    KINSKI.

    Vector does not implement a sorted collection, so you can't use a Comparator. Why don't you use a TreeSet? Then you couldclass MyObject
      String name;
      int value1;
      int value2;
      int value3;
      // Override equals() in this class to match what our comparator does
      boolean equals (Object cand)
        // Verify comparability; this will also allow subclasses.
        if (cand !instanceof MyObject)
          throw new ClassCastException();
        return value1 = cand.value1;
      // Provide the comparator for this class. Make it static; instance not required
      static Comparator getComparator ()
        // Return this class's comparator
        return new MyClassComparator();
      // Define a comparator for this class
      private static class MyClassComparator implements Comparator
        compare (Object cand1, Object cand2)
          // Verify comparability; this will also allow subclasses.
          if ((cand1 !instanceof MyObject) || (cand2 !instanceof MyObject))
            throw new ClassCastException();
          // Compare. Less-than return -1
          if ((MyObject) cand1.value1 < (MyObject) cand2.value1))
            return -1;
          // Greater-than return 1
          else if ((MyObject) cand1.value1 > (MyObject) cand2.value1))
            return 1;
          // Equal-to return 0.
          else
            return 0;
    }then just pass MyObject.getComparator() (you don't need to create an instance) when you create the TreeSet.

  • 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?

  • 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

  • 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?

  • Need to sort two vectors at the same time! please help!

    I have
    vectorA: 2 4 9 1 7 6 8
    vectorB: two four nine one seven six eight
    i want to sort these two vectors in parallel
    what is the best way?
    i could just do Collection.sort(vectorA) and get
    1 2 4 6 7 8 9
    but i want the vectorB to have a corresponding order
    please help!!
    thanks

    public class Pair implements Comparable {
    private int value;
    private String name;
    public int getValue() {
    return this.value;
    public void setValue(int value) {
    this.value = value;
    public String getName() {
    return this.name;
    public void setName(String name) {
    this.name = name;
    public int compareTo(Object o) {
    Pair that = (Pair) o;
    return this.value - that.value;
    place both in a Collection (vector is a bad choice if you are going to do sorting LinkedList is better) the Collections.sort method will sort
    according to the compareTo method.

  • Efficient compare among vector elements

    Hi guys,
    I've a basic question, I've solved it with lots of nester for and I'm sure it's not the best way to do it, so I hope you can help me.
    I've a vector of object User, so Vector<User> and user object has a property age.
    What I want is simply:
    1) iterating this vector and make comparing of age field for each user with each user (for example, 3 user, user1 - user2, user2 -user3,user3-user1).
    2) User older for each compare must be entered in a new vector.
    So starting from a vector of n element I should have a new vector with dimension equal to n*(n-1)/2.
    Could someone help me to find most efficient code? (please show some code example)
    Thanks,
    Regards

    new_to_java123 wrote:
    What I want is simply:
    1) iterating this vector and make comparing of age field for each user with each user (for example, 3 user, user1 - user2, user2 -user3,user3-user1).
    2) User older for each compare must be entered in a new vector.
    So starting from a vector of n element I should have a new vector with dimension equal to n*(n-1)/2.Well what you want is simply a very inefficient way to solve the problem. As you describe it you will use an algorithm of complexity O(n**2), while it can be done in O(n)!
    What you should do:
    1) Parse the vector once and determine the minAge and maxAge
    2) Create an array[min-age, max-age] which contains a list of users for each index. As this is not directly supported in Java, use an ArrayList of size (maxAge - minAge ++1) and populate it with an empty List for each index 0 .. maxAge - minAge. Each entry with index i in that ArrayList is a List of persons whose age is (i ++ minAge).
    3) Now iterate over the original Vector and append each user to the List of persons of his age (in code: ageList.get(user.getAge() - minAge()).add(user); )
    Voilá. Only two scans of the original Vector and you have a structure that holds
    1) List of the users of a given age (ageList.get(age - minAge()).
    2) All users older than the given age can be found by iterating the lists with greater index as (age - minAge()).
    And of course the memory requirements are much lower also!
    It needs to store only maxAge - minAge + 1 plus the outer ageList. if you store null instead of an empty list if no user has a specific age this can be further reduced at the cost of havein to do more null checks.

  • 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

  • PS opens vector smart object all pixilated

    I have this problem when using graphics from Illustrator as vector smart objects in Photoshop. I have followed tones of heated discussions on this but I haven't got any further. So here is what I do following the steps on the PS help: I copy graphics in AI and paste them in PS as smart object. A layer will be added that even has the name vector smart object but the graphic itself is all pixilated. Now the size of my PS-file is 150X150 Pixels with a resolution of 300. I need this size because I want to implement the file into my website and the layout requires this size for all my graphics. I would say it doesn't matter much what size the file is anyway since according to the PS help whatever vector smart object I place in Photoshop it won't change image quality when transformed. Therefor I'd say that not even the small file should be a problem here. But obviously there is something not working... because even when, to compare, I paste the graphic as pixel it shows the same result. Strange enough that I have used this vector smart objects before and it worked just fine... So I tried to paste the graphic in different ways, saving it as pdf in AI and then placing it in PS, insert it directly as smart object, making sure the file handling and clipboard references are adjusted, and finally after no success trashing the preferences in both applications. Still not closer to a satisfying result. I attach the graphics (first the AI graphic, then the vector smart object from PS) to show the difference and maybe there is someone out there that can tell what I am doing wrong. By the way I just converted the files to jpegs to post them here so the sizes might not match on the screen...

    One other thing to check...
    Are you viewing the display at 100%?
    Photoshop's OpenGL-based image resizing algorithms are smoother and fuzzier than they used to be (or than they are with OpenGL disabled).  What I'm getting at is that you may be perceiving fuzziness in the display that's not really there in the image.
    How does the fuzziness look if you zoom in a lot?
    -Noel

  • Maybe you are looking for

    • Can't open flash

      i can't view flash content with my safari. i followed the window that says get flash player, i've checked my java script and tried safari help, but it still doesn't work. it's all fine on my internet explorer though. i'd be glad if you could help.

    • Problem in Contract update  SRM

      Hi experts, I'am trying tlo update a contract using the FM :   'BBP_PD_CTR_UPDATE' . I use the folowing logic :    * Lock contract     CALL FUNCTION 'BBP_PD_CTR_LOCK'       EXPORTING         i_header_guid = l_header_updated-guid. *Update     CALL FUN

    • Icloud with phone and ipad

      I am using icloud for reminders on both my phone and a work ipad.  They reminders are not showing up on my phone under icloud -- only under my gmail account.  How do I set up my phone to have the icloud reminders?

    • Entity cache

      hi all have a question regarding entity cache behaviour. how do I make sure there are NO More than N beans in the entity cache. If I set max-beans-in-free-pool, problem seems to be this: if I need to retrieve more than (N+1) rows from DB, weblogic re

    • Setup of NI-TimeSync with MAX in LabVIEW 2010

      Hi, I have a set of PXI systems running LabVIEW 2010 RT and want to start using NI-TimeSync for my timestamping.   My systems have NI-TimeSync 1.0.1 installed and NI-Sync 3.2.1 installed.  I have a system with a PXI-6682 card in slot 2, and have succ