• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Find common elements between 2 lists where list elements are objects

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to find common elements between 2 lists where elements are of type Object.

For example


similarly


I need to find out the entries from employees which has same first name as that of student.

I did the following




This works, however I am trying to find a solution where common attributes are not limited to a single field (say for example first and last name).

The list can contain many elements but irrespective of the size, I am wondering if a better solution exist using FP.
 
Bartender
Posts: 4667
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it is a better solution, but a way is:
 
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll have to think about your question a bit, but I noticed right away your use of "float". DON'T DO IT. Use double. "float" should only be used to interface with legacy code that requires it. Today's CPUs typically handle a double in hardware whereas often floats have to be promoted to a double behind the scenes in order to perform operations. "float" is especially bad for anything financial because of its limited precision. For financial calculations BigDecimal is the preferred type, use double if you must, but never float.
 
Master Rancher
Posts: 4052
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, float could also be useful in arrays, where the size of a float array is generally half the size of the corresponding double array.  For certain types of calculations it may be worthwhile to save space like that, but it's pretty rare nowadays.  

For financial applications it can be nice to do everything in cents (or tenths of a cent or pesos or whatever minimal unit is required) and use an int or long.  Can be faster, less memory-intensive, and above all, more readable, than BigDecimal code.  Though you do have to watch out for overflow...
 
Piet Souris
Bartender
Posts: 4667
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The calculations I made at my work resulted in tens of billions of euro's outcome. Model simplifications or otherwise not leading to more error than 5M were okay. Doubles therefore.
 
Piet Souris
Bartender
Posts: 4667
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was some critics about my reply being incomprehensible, so I like to give an explanation.

In your code snippet, you have


You now have only one Predicate that does the filtering. But you can use more than one Predicate to test. For instance:
What I did in my example was to put all these Predicates in a List, and have a method that processed all these Predicates. In this example I "and".ed the Predicates, but you can use "or" as well, or some other combination like
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All lists have methods (actually declared in Collection) for the three bulk operations, which in Sets are called, ∩=intersection, which appears to be what you want here, ∪=union, and \=difference. You will probably find it quicker to use them than to write a Stream. You will of course have to override equals() and hashCode() correctly in your Student objects. The Collections Framework is written to depend on the correct functioning of equals() and hashCode().

The Java™ Tutorials link I gave you has five bulk operations (on the Collections interface page). It includes testing for containing lots of elements and removing everything.
Note:-
  • 1: The two code snippets are not equivalent to each other; they do different things.
  • 2: You can't use both lines 4 and 5 in the first code block.
  • 3: Neither version of toList() makes any guarantee about the runtime type of the List created. Links: 1 2.
  •  
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:. . . not leading to more error than 5M were okay. Doubles therefore.

    You weren't using the figures to model money then. More like the way an engineer would use them to model the weight of the load on a bridge. If the engineer is within ±5% of the correct figures, they will be happy.
     
    Sheriff
    Posts: 8094
    569
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    nitinram agarwal wrote:This works, however I am trying to find a solution where common attributes are not limited to a single field (say for example first and last name).


    Not necessarily suggesting (a bit naive but easy to understand approach given current information), but just saying that also very similar to yours approach supporting 2 or more fields would be, just to concatenate all fields you want to check against, and then handle them in the same way as you do now.

    i.e.

    But I see there are suggested much more sophisticated approaches than this.

     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am going to be naughty and say that no object has any business being in a Collection until it has both equals() and hashCode() overridden correctly.
     
    Saloon Keeper
    Posts: 1606
    52
    Eclipse IDE Postgres Database C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I am going to be naughty and say that no object has any business being in a Collection until it has both equals() and hashCode() overridden correctly.



    Just saw this now.  I am starting to feel that way strongly.

    I keep seeing Collection code examples that ignore this that just happen to work "by accident" for the specific data they use.
    I can break almost any of them rather trivially.

    I've decided to describe the tendency to cover topics by just rushing to the descriptions of all the API methods in the Javadocs, while ignoring the Sage Advice contained in the expository material preceding them the "Race To the Bottom".

    Since I love to have names for things, feel free to use that phrase for any and all such tutorial material.  It is legion.
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Most “tutorials” I have seen are poor. In most cases that is because they are incomplete and omit important facts like having to override equals() nd hashCode().
     
    Saloon Keeper
    Posts: 13367
    295
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I am going to be naughty and say that no object has any business being in a Collection until it has both equals() and hashCode() overridden correctly.


    I assume you meant "no object has any business being in a Set, or used as a key in a Map, until it has both equals() and hashCode() overridden correctly.

    Even so, I disagree.

    While this is good advice in general, especially for beginners, there are good use cases for objects to be comparable only by identity, and there are good use cases to keep those same objects in a (key) set. You simply need to be aware that comparisons will be done based on object identity.
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did say I was being naughty when I said that.
     
    Jesse Silverman
    Saloon Keeper
    Posts: 1606
    52
    Eclipse IDE Postgres Database C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:

    Campbell Ritchie wrote:I am going to be naughty and say that no object has any business being in a Collection until it has both equals() and hashCode() overridden correctly.


    I assume you meant "no object has any business being in a Set, or used as a key in a Map, until it has both equals() and hashCode() overridden correctly.

    Even so, I disagree.

    While this is good advice in general, especially for beginners, there are good use cases for objects to be comparable only by identity, and there are good use cases to keep those same objects in a (key) set. You simply need to be aware that comparisons will be done based on object identity.



    Well, you did say Set, so the fact that this option of data structure choice for a particular Map exists doesn't gainsay that:
    https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/IdentityHashMap.html

    I note that there is no IdentityHashSet, and the above structure uses Reference Equality for BOTH keys AND values.

    Great beginners and decent intermediate programmers know all the Rules and follow them.

    Great programmers know when the Rules Should Be Broken.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 13367
    295
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The purpose of IdentityHashMap is different. It's meant for when your key and value types DO override equals() and hashCode(), but you want to fall back to reference equality. It's not meant for types that don't override equals() and hashCode() in the first place. You can use a plain old HashMap for that.

    Anyway, the use cases for IdentityHashMap are probably so rare that they didn't bother to create an IdentityHashSet. You can just use IdentityHashMap for the same purpose. After all, I seem to recall that HashSet is really just a thin wrapper around a HashMap.
     
    Marshal
    Posts: 26909
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:Anyway, the use cases for IdentityHashMap are probably so rare that they didn't bother to create an IdentityHashSet...


    Indeed.

    The API wrote:This class is not a general-purpose Map implementation!


    It's extremely uncommon for the API to use an exclamation mark. In fact I can't recall seeing one before now. So they are seriously warning you off from using the class.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic