Results 1 to 6 of 6
  1. #1
    RPFeltz is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default How do I convert a String[] object to a Set?

    I want to store a String.split("seperator") to a Set<String>.
    Example:
    Java Code:
    Set<String> = (Set<String>) "a|b|c".split("|");
    Sadly String[] and Set are incompatible types.
    About the same as using .add three times, but shorter.
    Any way to do this?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    RPFeltz is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Found something that turns an array to a list.
    Set<String> YesNo = (Set<String>) Arrays.asList("y|yes|n|no|-ignoreCase".split("|"));
    Thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by RPFeltz View Post
    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.

  5. #5
    jatinfrost is offline Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Default Re: How do I convert a String[] object to a Set?

    final String str = "y|yes|n|no|-ignoreCase";
    final Set<String> valSet = Sets.newHashSet(Arrays.asList(str));
    System.out.println(valSet.toString());

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do I convert a String[] object to a Set?

    This thread is three years old; the OP most likely has found a solution to his/her problem already (and your solution isn't correct). I'm closing this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Convert object to String
    By vinnie in forum New To Java
    Replies: 10
    Last Post: 12-12-2010, 05:45 AM
  2. Convert object to String
    By innspiron in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 01:26 PM
  3. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 12:33 PM
  4. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 09:12 AM
  5. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •