Results 1 to 4 of 4
- 08-20-2011, 02:46 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
- 08-20-2011, 03:26 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Any collection has a constructor which takes as an argument another collection. Try seeing if you can find something which turns an array into a list, and pass it into a sets constrctor.
- 08-20-2011, 04:12 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Found something that turns an array to a list.
Set<String> YesNo = (Set<String>) Arrays.asList("y|yes|n|no|-ignoreCase".split("|"));
Thanks.
-
Are you sure that you want to cast a List<String> into a Set<String>? Does that even compile?
Edit: it in fact does compile, but if run will throw the expected ClassCastException. Original poster: use the Set constructor as has already been recommended. Since Set is an interface, you'll need to use one of its concrete children such as HashSet. Also, there's danger when splitting on "|" because split uses a regular expression String and | has special meaning in regex. Better to escape it:
i.e.,
Java Code:Set<String> YesNo = new HashSet<String>(Arrays.asList("y|yes|n|no|-ignoreCase".split("\\|")));Last edited by Fubarable; 08-20-2011 at 04:18 PM.
Similar Threads
-
Convert object to String
By vinnie in forum New To JavaReplies: 10Last Post: 12-12-2010, 05:45 AM -
Convert object to String
By innspiron in forum New To JavaReplies: 3Last Post: 03-14-2010, 01:26 PM -
How to convert string array into object in java
By kgkamaraj in forum New To JavaReplies: 4Last Post: 02-12-2010, 12:33 PM -
How to convert a string array into object in java
By kgkamaraj in forum Advanced JavaReplies: 2Last Post: 02-12-2010, 09:12 AM -
convert a string to an object in java
By jforce93 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks