• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to create and return an empty set/list

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm very new to Java and I have been struggling loads with my course. The problem I am having at the moment is only worth 1 mark for each part so it feels like it should be really simple but I just can't get it!

I am asked to "write a public class method makeTestSet() which takes no argument, that creates and returns an empty unsorted set whose elements are declared to be of type EOHoverFrog."

My most recent attempt is this:


There is also a similar question, exactly the same as above but I need to create and return an empty list.


I can find loads of information about creating empty sets/lists but nothing about returning them, any help would be much appreciated!
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kim,

Could you post the error you are facing ...
I can see a space is missed here -


and Welcome to the Ranch ...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote:


you are creating an empty object(hoverSet) and returning another empty object(). why that unused reference hoverSet ?
 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely the return type for the method should be Set<EOHoverFrog> rather than HashSet<etc>?

And welcome again
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Surely the return type for the method should be Set<EOHoverFrog> rather than HashSet<etc>?


Is that a good practice... it wont give error right...
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get an error message with the code I was using, but when I try and test it, the display pane shows '[]' when I think it is meant to be empty....unless that indicates an empty set?? :s
campbell ritchie - i've just tried changing it to Set<EOHoverFrog>etc and i got the error message "java.util.set is abstract; cannot be instantiated"

thank you for the welcomes
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good catch! Campbell
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote:i got the error message "java.util.set is abstract; cannot be instantiated"


He told about *return type of the method* . not about instantiating an object! you cant instantiate an interface!
I mean it wont compile : new Set<Something>();
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look up the toString() method from the HashSet documentation, it leads to that webpage. That says you get [ then the elements separated by commas, then ]. If you have no elements, surely you will get [ followed by nothing followed by ], ie [].
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy is correct
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aah ok, so now I've got


I still get '[]' in the display pane when I'm testing, but I'm still unsure if that's right or not...but is it necessary to have twice? Is it doing something different each time or is one of them irrelevent?
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops there seems to be a delay in your posts arriving, I didn't see the last couple before I posted sorry!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote:aah ok, so now I've got


Can you explain line by line what this does? In particular, why do you have line 3 in there?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote: . . . is it necessary to have twice? Is it doing something different each time or is one of them irrelevent?

No, it's not necessary. One is irrelevant, as you were told here. I told you about [] earlier.
And Jesper is just telling you something as I write. Probably beating me to the mark, saying the same
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote:oops there seems to be a delay in your posts arriving, I didn't see the last couple before I posted sorry!

If you open the website and write a reply, the previous posts are not updated. So I often find myself repeating what somebody else said, because I didn't realise it had been said while I had the page open.
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm good question
public class method, return type Set<EOHoverFrog>, makeTestSet() is the name of the method, and it has no argument.
I thought this line was creating the new empty set and calling it hoverSet
and I thought this line was returning that new empty set
.....but now it appears that I don't need the 2nd line? is that last line creating an empty set as well?
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and yes I see what you mean about [] thanks, I never knew that, I was assuming the display pane should be blank.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have got it

You want to get rid of the second line. You are twice creating empty Sets, but you are doing nothing with the first. That simply occupies memory until you run short, when it will be deleted to make way for something you actually need.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kim george wrote:and yes I see what you mean about [] thanks . . .

You're welcome

Did you look at the API documentation? When we give you a link, be sure to look at it. You learn a lot more by finding things for yourself.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I am returning an empty set/list/collection, I always return the appropriate collection from Google's free Guava library.


Because its immutable, users of the set will not be able to modify the set (say add()) things to it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just need an empty Set, List, Collection or Map, java.util.Collections has methods for returning those (emptySet, emptyList, etc). And the good thing about these is that they are shared objects - not one instance for each empty whatever you need, but the same one over and over again.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:If you just need an empty Set, List, Collection or Map, java.util.Collections has methods for returning those (emptySet, emptyList, etc). And the good thing about these is that they are shared objects - not one instance for each empty whatever you need, but the same one over and over again.


These are immutable, which is good. But they are not generic. The Guava equivalents are generic.

I'm not too concerned about having lots of small immutable objects. I have not looked at the Guava code carefully to see if they also use a shared common object, the Javadocs don't mention it. The generic information would be lost during runtime with type erasure anyway.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:

Rob Spoor wrote:If you just need an empty Set, List, Collection or Map, java.util.Collections has methods for returning those (emptySet, emptyList, etc). And the good thing about these is that they are shared objects - not one instance for each empty whatever you need, but the same one over and over again.


These are immutable, which is good. But they are not generic. The Guava equivalents are generic.


The static final fields are not generic, but there are (since Java 5.0) methods that make them generic. Since there will never be any elements the cast from the non-generic fields to generic return types is safe.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable objects are very nice, but I thought the idea of the Set was to put the Frogs into. In which case they don't want to be immutable, and the emptySet() method is not going to be helpful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic