How to Compare Lists of Objects

Hello All,
I have two lists(self developed; not using Java's inherent list functionality) containing objects of the same class. Now I would like to compare the elements within the lists one on one.
Meaning, I would like to compare the two lists to see whether they contain identical elements or not.
How should I do this.
Please help !!
Thank you.

If there is an implicit order on your objects then
1.) Write a Comparator for your objects
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html
2.) Sort both lists
3.) int j = 0;
FOR EACH item IN list1 DO
  WHILE list2.get(j) IS LESS THAN item DO
    ++j;
  OD
  IF item EQUALS list2.get(j) THEN
    PRINT "Found dups " + item + " and " + list2.get(j)
  FI
ODIf there exists no implicit ordering, you can only use the trivial solution:FOR EACH item1 IN list1 DO
  FOR EACH item2 IN list2 DO
    IF item1 EQUALS item2 THEN
      PRINT "Found dups " + item1 + " and " + item2
    FI
  OD
OD(BTW - this is pseudo-code)
... or put you stuff in to Sets and use retainAll ...

Similar Messages

  • How to display List of objects in jsp?

    Hi,
    I have a list containing Collection of bean objects.
    Now I want to display the values in the list of objects in a jsp using JSTL.
    Any body help me how to retrieve the bean objects from the List and how can i display that values in the jsp.
    Advanced Thanks,
    Mahendra

    Have you tried to use <:forEach></c:forEach> tag available in jstl?

  • JSP: How do display list of objects in jsp table?

    sorry that my question my confuse you, let me explain more.
    in my java class, have method : public List retrieveAllVacancies(); and it returns a list of available vacancies.
    on my jsp page, i want to display the vicancies in the table have 3 rows:
    vacancy title  |  location  |  contract type
    should i use some form of for loop or iterator to get the vacancies in the list? how does the jsp page get the list of objects?
    anyone can help me? thanks

    i found out the actual thing i look for is how to pass the vector to the jsp page.
    <table>                                                                                                    
        <%
             Vacancy vacancy= new Vacancy();
            Vector allVacancies = new Vector();
            *//allVacancies = (java.util.Vector)request.getAttribute("allVacancies");*
            if(allVacancies.size() == 0) 
                out.print("<br><br><br>     There are no vacancies available!!");
            else
        %>
        <tr>
            <td>Vacancy Title</td>
            <td>Contract Type</td>
            <td>Location</td>
        </tr>
        <%
            for (int i=0;i<allVacancies.size();i++)
                vacancy = (Vacancy)allVacancies.get(i);
         %>
        <tr>
            <td><% out.print(vacancy.getVacancyTitle());%></td>
            <td><% out.print(vacancy.getContractType());%></td>
            <td><% out.print(vacancy.getVacancyLocation()); %></td>
        </tr>
    <%
    %>
    </table>*//allVacancies = (java.util.Vector)request.getAttribute("allVacancies");*
    if i have this line, when i open the jsp page it will gives NullPoniterException.
    the following was what i found for the similar problem,
    You can add the Vector to the HttpSession with_
    session.setAttribute(String name, Object value)_
    or to the ServletRequest with_
    request.setAttribute(String name, Object value)._
    In the other JSP, retrieve the value with_
    session.getAttribute(String name)_
    or to the ServletRequest with_
    request.getAttribute(String name)._
    but i tried to do in this way it didn't work. for sure i didn't do it correctly. could anybody give bit more explanation about how to pass the Vector to the jsp page?
    any help would be appreciated.

  • How to make List.contains(Object) work when you write your own .equals func

    I have a list of objcts of a class defined by me. I want to use the List.contains(String name) function to tell me if it contains an object of that name. I have overriden the .equals function as shown in [1]. I then use the code at [2] to to test it, with a bit of debugger output as shown in [3]. It is not working, my overriden method is not getting called by List.contains(String name).
    Can anyone tell me how to get this to work?
    [1]     @Override public boolean equals(Object esId) {
            if(esId.getClass().isAssignableFrom(String.class)) {
                System.out.println("object name " + this.eslimId + " compare to " + esId + " = " + this.eslimId.equals(esId));
                return this.eslimId.equals(esId);
            } else {
                System.out.println("wrong class");
                return super.equals(esId);
        }[2] graphics.getProcedures() returns private List<ProcedureGraphicSet> procedures;
    System.out.println( " graphics.getProcedures().get(0).equals(\"ESLIM_009\")= " + graphics.getProcedures().get(0).equals("ESLIM_009"));
    System.out.println( " graphics.getProcedures().contains(\"ESLIM_009\")= " + graphics.getProcedures().contains("ESLIM_009"));[3]
    object name ESLIM_009 compare to ESLIM_009 = true
    graphics.getProcedures().get(0).equals("ESLIM_009")= true
    graphics.getProcedures().contains("ESLIM_009")= false

    This is a gross violation of the contract for the equals method, and you are basically getting bitten by that. The equals method must be symmetric, meaning this must hold for all objects A and B:
    A.equals(B) == B.equals(A)This does not hold for your equals method because for any string A and custom object B:
    A.equals(B) == falseAnd, apparently, the List.contains() method is choosing to compare objects that way.
    You must iterate through the list and check the names yourself, or use some sort of functional package which provides a List.contains() type method which takes a Predicate object of some sort.
    Edited by: jtahlborn on Feb 11, 2008 11:03 AM
    Edited by: jtahlborn on Feb 11, 2008 11:03 AM

  • How to display 'List' of objects in table format

    I am trying to display bunch of records in the table format.
    I have the List of bean object populated with table datas.
    Currently the code is working fine ,if I tend to display one record.I am wondering how to display all the records.
    I know theres a 'Fieldloop'.If thats the one please explain with example.

    iterate the object list using field loop. eg. is given below
    [      <Field name='MatchTable'>
    <Display class='SimpleTable'>
    <Property name='columns'>
    <List>
    <String>title</String>
    </List>
    </Property>
    </Display>
         <FieldLoop for='name' in='userlist'>
    <Field name='username'>
    <Display class='Label'>
    <Property name='labels'>
    <ref>name</ref>
    </Property>
    </Display>
    </Field>
         <FieldLoop>
         </Field>

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

  • How to add list of objects of class to the cookie in java

    Hi all ,
    I have arraylist of the object of clases like
                           List<CLASSNAME> sampleList = new arrayList<CLASSNAME>;where CLASSNAME contains getter,setters of fields
    How can I add this sampleList to the cookies because cookie only contain cookie name and cookie value which is string only.
        public void createNewCookie(String cookieName, String cookieValue, int cookieMaxAge){
        HttpServletResponse httpServletResponse = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setMaxAge(cookieMaxAge);
        httpServletResponse.addCookie(cookie);
        public String getCookie(String cookieName){
        FacesContext vFacesContext = FacesContext.getCurrentInstance();
        ExternalContext vExternalContext = vFacesContext.getExternalContext();
        Map vRequestCookieMap = vExternalContext.getRequestCookieMap();
        Cookie vMyCookie = (Cookie)vRequestCookieMap.get(cookieName);
        if(vMyCookie != null)
           return vMyCookie.getValue();
        else
        return null;
       

    Hi Timo,
    My requirement is as follow.
    When anonymous user visit the website he can add the list of items he checked to buy .Add these items to list.
    When any user logged in assign this list to the that logged in user.When user logged out clear this list.
    So i think I have to put this list in the SessionScope it will be the best.
    But what about anonymous users . When many anonymous users visit the websites can they have different list according to the anonymous user in session scope

  • How to compare two date Objects

    Hi,
    I have two Calendar Objects, one is coming from client and one is my server time, I need to evaluate that incoming time with server time so that if it is sent before an hour back then i should not do anything on that request Object. I have converted the incoming String into Calendar Object and how can i evaluate these two Calendar Objects including their hours and minutes.
    Thanks in advance
    bajju

    bajjurireddy, ignore Mobiquity's post. It contains no useful information and will only serve to confuse and frustrate you.
    Mobiquity wrote:
    Also make sure that the two date objects are in the same date format..
    public static String DateToDateString(java.util.Date d, String dateFormat)
         throws ParseException {
              SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
              sdf.setLenient(false); // this is required else it will convert
              String dateString = sdf.format(d);
              return dateString;

  • How to extract list of object and formula used in variables as Excel for a particular Webi Report

    Hi,
    Is it possible to get the list of variables and objects, and variable formula used in a webi report extracted in excel or PDF in BO XI R3.1?
    Thanks In Advance,
    Shenbu

    Hi Shenbaga,
    You can try to get this information using the report engine sdks of BusinessObjects which is specific to webi reports.
    Please refer to the link to download the dev guide and api referrence for XI 3.1
    SAP BusinessObjects BI 3.x and Lower - Developer SDK Library
    Refer to the Report Engine Java SDK dev guide and api referrence.
    Go through the dev guide in detail and you would be able to use the report engine sdks as per your needs.
    Additional Note: There have been changes in report engine sdks from XI 3.1 to BI 4.0 and you would not be able to get all the features available in XI 3.1 report engine sdks in BI 4.0.
    Hope this helps.
    Regards,
    Chandrahas

  • How to extract List of Objects and Classes in BO

    Hi
    I need to get a list of all Classes and Objects in a universe.
    Is there a way to extract this list in an excel.
    I dont need the PDF file which can be saved from a universe with all the universe information.
    I just need a plain excel file with the classes, objects under it, object definitions etc.
    Is there a VBA code to do so?
    I know there is one to extract the user info from CMS.
    Please help.
    Thanks in advance.

    Option Explicit
    Dim DesignerApp As Designer.Application
    Dim Univ As Designer.Universe
    Dim Wksht As Excel.Worksheet
    Sub GetInfo()
        Set DesignerApp = New Designer.Application
        DesignerApp.Visible = True
        Call DesignerApp.LoginAs
        Set Univ = DesignerApp.Universes.Open
        'DesignerApp.Visible = False
        Set Wksht = ThisWorkbook.Worksheets("Objects")
        Wksht.Unprotect
        Wksht.Columns().ClearContents
        Wksht.Cells(1, 1) = "Class"
        Wksht.Cells(1, 2) = "Objects"
        Wksht.Cells(1, 3) = "Table Reference"
        Wksht.Cells(1, 4) = "Object Description"
        Wksht.Cells(1, 5) = "Object Type"
        Wksht.Cells(1, 6) = "Qualification"
        Call GetObjectInfo(Univ.Classes, 1)
        Wksht.Protect
        DesignerApp.Quit
        Set DesignerApp = Nothing
    End Sub
    Private Sub GetObjectInfo(Clss, RowNum As Long)
        Dim Cls As Designer.Class
        Dim Obj As Designer.Object
        For Each Cls In Clss
            For Each Obj In Cls.Objects
                RowNum = RowNum + 1
                Wksht.Cells(RowNum, 1) = Cls.Name
                Wksht.Cells(RowNum, 2) = Obj.Name
                Wksht.Cells(RowNum, 3) = Obj.Select
                Wksht.Cells(RowNum, 4) = Obj.Description
                If Obj.Type = 2 Then
                    Wksht.Cells(RowNum, 5) = "Character"
                ElseIf Obj.Type = 3 Then
                    Wksht.Cells(RowNum, 5) = "Date"
                ElseIf Obj.Type = 1 Then
                    Wksht.Cells(RowNum, 5) = "Number"
                Else
                    Wksht.Cells(RowNum, 5) = "Long Text"
                End If
                If Obj.Qualification = dsDimensionObject Then Wksht.Cells(RowNum, 6) = "Dimension"
                If Obj.Qualification = dsDetailObject Then Wksht.Cells(RowNum, 6) = "Detail"
                If Obj.Qualification = dsMeasureObject Then Wksht.Cells(RowNum, 6) = "Measure"
            Next Obj
            If Cls.Classes.Count > 0 Then
                Call GetObjectInfo(Cls.Classes, RowNum)
            End If
        Next Cls
    End Sub
    Above is the macro to document BO universes (Classes, Objects, TableName.FieldName, Object description, Object Type and Object Qualification ).
    You need to open the excel sheet->Go to TOOLS> MACRO --->VB EDITOR (Alt+F11) and paste this code in MODULE1
    To run the macro, You need to press Alt+F8 key and Click RUN button. Then designer window pops out, enter user id and password and select the required universe.
    Enjoy Macro

  • How to compare an Object of type class?

    Hi,
    I got a generic array and want to check which class it contains.
    Now i tried to use
    Class c = T[0].getClass();and later
    if(c instanceof Float){
    }but this don't work.
    So how to compare two instances of Class?
    Maybe by looking for the name of the class and compare them?
    regards.
    Olek

    import java.util.*;
    public class WorksForMe {
        public static void main(String[] args) {
            List<Object> list = new ArrayList<Object>();
            list.add(Float.valueOf(17));
            Class<?> cls = list.get(0).getClass();
            System.out.println(Float.class.equals(cls)); //true
            System.out.println(Float.class == cls); //true
    }But again, it's best to avoid this kind of case analysis.

  • Sort list of objects and Comparator.

    Hi. I have a problem with list sorting. There is a class with fields like these id (Long), number (String), operator (String), server (String) port (Integer). I have to sort list of these objects in accordance with the conditions specified by the user.
    Conditions for sorting:
    1. In the first account of the full compliance of the server name and port value (Object Gateway) specifed by the user.
    2. When the server name is correct and the port is different from the standard, then the objects should be placed in ascending order of the port.
    3. If the server name does not match the model, the order of the objects must reflect the declining value of a parameter port. If the list contains objects of the same port and different name, the order does not matter.
    How should Comparator looks like?

    Ok, this is the solution ;]
    public class GatewayComparator implements Comparator<Gateway> {
        private String server;
        private Integer port;
        public GatewayComparator(String server, Integer port){
            this.server = server;
            this.port = port;
        public int compare(Gateway o1, Gateway o2) {
            int result = 0;
            if (o1.getServer().equals(this.server)) {
                result = -1;
            if (o2.getServer().equals(this.server)) {
                result = 1;
            if (o1.getServer().equals(this.server) && o2.getServer().equals(this.server)) {
                if (o1.getPort().equals(this.port)) {
                    result = -1;
                if (o2.getPort().equals(this.port)) {
                    result = 1;
                } else if (!o1.getPort().equals(this.port) && !o2.getPort().equals(this.port)) {
                    result = o1.getPort().compareTo(o2.getPort());
            } else if (!o1.getServer().equals(this.server) && !o2.getServer().equals(this.server)) {
                if (o1.getPort() > o2.getPort()) {
                    result = -1;
                } else if (o1.getPort() < o2.getPort()) {
                    result = 1;
                } else {
                    result = 0;
            return result;
    }

  • How to generate an SE63 object list in batch periodically ?

    Dear experts,
    we translate program and DDIC-texts of our add-ons continiously to english and
    french, in order to keep up translations with the ongoing development we create
    the necessary SE63-object list once a week. Since this process is creating
    several background-jobs dynamically it is not possible to mark this inital job as
    to be repeated periodically. I do understand that but I don't know why there
    seems to be absolutely no way to get this done automatically once a week.
    Here is what I have already tried in vain in order to achieve this:
    - a report performing a CALL TRANSACTION on transaction SE63/SLW3
    - an eCATT-script including a TCD-Script on transaction SLW3
      (the script works fine if it is started directly, but the only FM that I have found
       to start a script with in batch ( 'ECATT_EXECUTE' ) does not do it, but comes
       back with a returncode zero, as if it did.
    - and I even tried to start the above mentioned eCATT-script by a report doing
      a CALL TRANSACTION on transaction SECATT providing the necessary
      field settings for the initial and following popup start screen.
    Nothing seems to work. The SAP notes on this topic are as useful as the
    SAP documentation on the subject "object list" (forget about it).
    Who has some experience with this issue respectively ideas for my problem?
    How can I generate an object list for translation once a week automatically in
    background?
    Thanks in advance
    Andreas

    Hi Lorant,
    attached you'll find a download of the ABAP which is to be used as a job step
    and an XML-Download of the eCATT-testscript, that processes the generation of a new object list in SE63.
    As both developing objects were in our companys namespace ("/HOAG/") which you cannot use, I made a copy in the z-namespace first, from which I have removed a couple of specialties that are certainly of no value for you at all, before I downloaded them for you.
    You need to give the script a name in your namespace or in the Z-namespace and then you have to adapt the scripts name in the BDCDATA generation procedure in the ABAP program appropriately.
    It may be possible that the eCATT-script doesn't run properly on your system. This is due to the fact, that the sequences of dynpros and fields contained in the dynpros differ a little in all of our 4 SAP development systems that we decided to be translation relevant, This is also the reason why I recorded individual eCATT-scripts for each one of them. It may be possible that you need to record your "own" system compliant script.
    An example: the popup screen with the print parameters can be personalized and may therefore look differently for every user in every system.
    This is also a good reason why the background job should be scheduled and performed under the name of the same user who recorded the eCATT script before!
    And just one last remark: The returncode of the job only gives you an information about wether or not the eCATT script has been executed properly but you do not know if the chain of jobs usually created by the SE63 objectlist creation functionality have been executed completely. The "system" that we set up for the automated objectlist creation is completed by a third ABAP program which is also scheduled daily as a background job. It is started about an hour after the the object list creation should be through and it verifies if the standard SAP jobs (the names of which are beginning with something like "OBJL") ran completely that day and that they are in the status "finished". If this is not the case, our "objectlist verification ABAP" sends a couple of SAP express mails to a certain selection of key users who are supposed to take a look at the system). This all works pretty well in combination!
    If you (or the person that you are going to give this task to) know enough of the german language to read it, you won't have any problem with adapting this to your needs because you'll find everything neatly commented in the source code.
    regards
    Andreas

  • How to get list of modified repository objects in SPAU

    Hi,
    While applying Support pack for HR (SAP ERP 6.0) I get SPAU prompt with message:
    "The system detected that 10 of the repository objects in the Support Packages have been modified in
    your system . Check whether you want to retain or restore these changes you have made. Fore more
    info about performing modification adjustment see modification adjustment help..."
    Now as I call SPAU i get the screen with following options:
    Correction in SAP Note
    With modification Assistant
    Without  modification Assistant
    etc..
    Can someone guide me How to get list of modified repository objects in SPAU.
    Thanks & Regards
    Vishal

    Hi,
    In SPAU you will get with modification assistence. just expand the subtree, and follw standard process .
    This is to retain the enhancement. If there are any data dictionary related correction go to SPDD.
    Regards
    Rahul

  • 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

Maybe you are looking for