• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I initialize a Set on creation?

 
Cathy Chlevner
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean something similar to the array syntax :
int[] array =new int[]{1,2,5}

but for Set collection?

Thanks a lot in advance
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "katy246 "-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, there's no direct way. It's somewhat hackish, but I think this should work:
 
Cathy Chlevner
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer -it does look a bit hackish , but is indeed along the lines of what I thought too.


Originally posted by Ulf Dittmer:
Hello "katy246 "-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, there's no direct way. It's somewhat hackish, but I think this should work:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 5 or higher you can use the varargs syntax, but you should also use the generics syntax:


[ August 24, 2008: Message edited by: Garrett Rowe ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other day I saw this wonderfully hackish idiom for the first time:



That's an instance initializer in an anonymous class definition.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The other day I saw this wonderfully hackish idiom for the first time
That is so bad it is good!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic