aspose file tools*
The moose likes Beginning Java and the fly likes How can I initialize a Set on creation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I initialize a Set on creation?" Watch "How can I initialize a Set on creation?" New topic
Author

How can I initialize a Set on creation?

Cathy Chlevner
Greenhorn

Joined: Aug 24, 2008
Posts: 13
I mean something similar to the array syntax :
int[] array =new int[]{1,2,5}

but for Set collection?

Thanks a lot in advance


"Laziness is nothing more than the habit of resting before you get tired." - Jules Renard
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
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:


Ping & DNS - my free Android networking tools app
Cathy Chlevner
Greenhorn

Joined: Aug 24, 2008
Posts: 13
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The other day I saw this wonderfully hackish idiom for the first time:



That's an instance initializer in an anonymous class definition.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I initialize a Set on creation?