• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Converting set to list

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

My question is on below code. My input is three lists:[-1,-1,2],[-1,-1,2],[-1, 0, 1].  I want the output without duplicates and as below.

Expected output: [[-1,-1,2],[-1, 0, 1]]]

Actual output: [[-1, 2], [-1, 2], [-1, 0, 1]]

 
Saloon Keeper
Posts: 4500
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do:

then the result is the Set(-1, 2), since -1 is double and thus gets eliminated.

What you want is the Set of the three Lists. And it will succeed if a List has a suitable 'equals' method. See the API of a List to see if that is indeed the case.
 
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any purpose in using Set here. The stated goal seems to be to take a List of Lists and output a new List of Lists that has no duplicate Lists within it.

I just did a quick check on List to see how intelligent its comparison mechanism are and I think you can do that as simply as this:


That's predicate on the equals methods doing an element-by-element comparison, which is how I interpreted the java.util.List docs.

This is obviously a candidate for a stream expression as well.
 
Piet Souris
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals method of a List is clearly defined in the API. So this is what I had in mind:

And no, you don't need a Set, it's just convenient here.
Wonder if OP managed.
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not only do you not need Set, Set has one very distinctive difference. Unlike List, it does not have a guaranteed ordering.

On the other hand, a Set by definition doesn't hold duplicates. So if you want an unodered collection with no duplicates, this will work:


Duplicate additions are ignored.

A "Set" that allows duplicates in it is known as a Bag. It's part of the Collections classes of SmallTalk, but there's no separate class Bag defined for Java.
 
Piet Souris
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that will not work. Should be:
srcList.forEach(...)      
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:No, that will not work. Should be:
srcList.forEach(...)      



Memory isn't what it used to be (and it was pretty rotten then!)
 
Marshal
Posts: 3560
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Not only do you not need Set, Set has one very distinctive difference. Unlike List, it does not have a guaranteed ordering.


Doesn't the LinkedHashSet implementation maintain the order that the elements were inserted?
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Tim Holloway wrote:Not only do you not need Set, Set has one very distinctive difference. Unlike List, it does not have a guaranteed ordering.


Doesn't the LinkedHashSet implementation maintain the order that the elements were inserted?



Yes. but that's a specialized subclass of Set, just like TreeSet is.

Sets in the abstract do not have ordering. Ask a mathematician.
 
Marshal
Posts: 26601
81
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

Tim Holloway wrote:Sets in the abstract do not have ordering. Ask a mathematician.



Since you asked... yes, that's true.

But the Axiom of Choice implies that a well-ordering exists for every set, so although a set doesn't have a built-in ordering, it's always possible to create one. Which is what happens in Java when you convert a Set to a List.
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More accurately, there exists AT LEAST one well-ordering for members of a Set. It also says that the ordering is applied externally to the collection.

This has real-world consequences. IIRC you're not a big fan of JavaServer Faces, but since nevertheless is is part of the JEE standard, I'll give a common example of why that's important.

In JSF, certain types of controls such as tables and selection lists are backed by (modelled by) collections. Because of how JSF processes items, it's possible that a property of a JSF Backing Bean (Model) might be queried multiple times during the progress of an HTTP request/response cycle.

These controls require that their backing collections be in a determinate order. It doesn't matter what the order is, but the order must be idempotent. If you make 5 property-request calls for a collection in a cycle, all 5 must return their collection contents in the exact same sequence. Failure to do so may cause graphical rendering to get scrambled and/or control item choices to return the wrong item from the collection. A java.util.Set doesn't independently guarantee an order of selection. It will probably return items in the same order every time, but without examining the source code for java.util.Set, it will be impossible to determine what that order is. A Set is a "black box". And as we move towards more parallelizeable operations like what Java Streams allows, we cannot in fact, even be certain that Set's internal storage will always decant in the same order.

Lest this seem a bit extreme, I'll return to JSF and show a very common case. The Model classes for JSF can be backed by a number of different retrieval mechanisms, including database queries. Absent an ORDER BY clause, SQL absolutely CANNOT guarantee order of returned results. And in particular, if the database is active, additions/deletions/changes AND concurrent queries returning ordered results (for some databases I've see) can ALL alter returned element sequence. Which has burned more than one JSF novice.

Just to put icing on the cake, I'm pretty sure that some releases of PostgreSQL returned unordered SELECT results in random order every time, just to be perverse. Or so it appeared to me.

So, in sum, a List is not interchangeable with a Set - or even a Bag, since unlike Sets, Lists can contain the same element more than once. If you expect to maintain sequence, you need to have a suitable choice function and not hope that Java will do the right thing for you.

 
Marshal
Posts: 7982
560
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

Tim Holloway wrote:I don't see any purpose in using Set here. The stated goal seems to be to take a List of Lists and output a new List of Lists that has no duplicate Lists within it.
...

...
Not only do you not need Set, Set has one very distinctive difference. Unlike List, it does not have a guaranteed ordering.


There is another very distinctive difference. Search operation's performance. And you may feel a significant difference, whether that matters much or not in OPs case, we don't know.
 
Piet Souris
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geez,

I was just giving a one line example using the good old Collection Framework,  not expecting to get a complete lecture about sets and their properties.
 
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because you may have to “create” information when you convert a set to a list. You may lose information converting a list to a set.
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is because you may have to “create” information when you convert a set to a list. You may lose information converting a list to a set.



And... now we've added Stephen Hawking to the chat. Black holes and entropy!
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Richard Feynman and ?Oliver? Penrose.
 
Tim Holloway
Saloon Keeper
Posts: 23692
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chomsky.
 
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Liutauras - I would even say that the performance difference makes using a HashSet or LinkedHashSet the natural way that we, as good programmers, should try to remove duplicates.  Don't use O(N^2) when O(N) is available.  Especially when it's so easy.

Of course, we can instead delegate to the smart programmers who implemented streams

(which uses a HashSet internally, because of course)

These both also guarantee that they preserve the order of the first occurrence of each element, simply dropping all later occurrences.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic