-2

I did this:

Set<String> mySet = new TreeSet<String>();
String list = ("word,another,word2"); // actually this is much bigger
String[] wordArr = list.split(",");
mySet.addAll(wordArr);

This gives error:

 The method addAll(Collection<? extends String>) in the type Set<String> is
 not applicable for the arguments (String[])

I think my intentions are clear.

Side ques: is there a better way to achieve my desired set? I know already there are no repeated value in my list.

kBisla
  • 590
  • 2
  • 8
  • 23
  • 4
    Check out `Arrays.asList` – Sotirios Delimanolis Jan 16 '14 at 01:24
  • @SotiriosDelimanolis A -1? come on man, you could've just given your comment as an answer!! At least that'd help. – kBisla Jan 16 '14 at 01:28
  • 3
    I didn't downvote, but there are tons of other resources here and around the web that would've given you the answer. – Sotirios Delimanolis Jan 16 '14 at 01:30
  • 2
    @BlueFlame Note that the question's already been asked and answered (with almost exactly the same title). The tooltip on the downvote button says "This question does not show any research effort." People might downvote because searching for your question's title would have found you an answer. – Joshua Taylor Jan 16 '14 at 01:31
  • and stackoverflow is one such resource right? – kBisla Jan 16 '14 at 01:31
  • 1
    @BlueFlame Yes, Stack Overflow is one of the places that you should search before asking a question on Stack Overflow. :) – Joshua Taylor Jan 16 '14 at 01:33

2 Answers2

4

The TreeSet constructor accepts a Collection, a String[] can be converted to a List which implements Collection using Arrays.asList().

public static void main(String[] args) {

    String list = "word,another,word2"; //No need for () here
    String[] wordArr = list.split(",");
    Set<String> mySet = new TreeSet<String>(Arrays.asList(wordArr));

    for(String s:mySet){
        System.out.println(s);
    }

}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    You don't even need `addAll`. The set constructors will accept a collection, so you could use `Set mySet = new TreeSet<>( Arrays.asList( wordArr ));`. – Joshua Taylor Jan 16 '14 at 01:28
  • It's not just `TreeSet`, though, that has a constructor like that. Collections generally have constructors that accept collections. E.g., in the documentation for [AbstractCollection](http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html), there's a note, "The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification." – Joshua Taylor Jan 16 '14 at 01:32
  • @JoshuaTaylor Thats good to know. So to summarize, you can instantiate most (or any?) Collection by providing a `Collection` to the constructor? – Kevin Bowersox Jan 16 '14 at 01:34
  • You can't guarantee that classes that implement Collection have such constructors, but all the standard ones provided in the standard libraries do. The javadoc for the [Collection](http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html) interface does say that: – Joshua Taylor Jan 16 '14 at 01:37
  • "All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument. … – Joshua Taylor Jan 16 '14 at 01:38
  • … In effect, the latter constructor allows the user to copy any collection, producing an equivalent collection of the desired implementation type. There is no way to enforce this convention (as interfaces cannot contain constructors) but all of the general-purpose Collection implementations in the Java platform libraries comply." – Joshua Taylor Jan 16 '14 at 01:38
1

Here's an example using Guava:

package com.sandbox;


import com.google.common.collect.Sets;

import java.util.Set;

public class Sandbox {

    public static void main(String[] args) {
        String list = ("word,another,word2"); // actually this is much bigger
        String[] wordArr = list.split(",");
        Set<String> mySet = Sets.newHashSet(wordArr);
    }        
}

If you want to do this without Arrays (I don't recommend, but you're in a class so maybe you can't use it):

package com.sandbox;


import java.util.Set;
import java.util.TreeSet;

public class Sandbox {

    public static void main(String[] args) {
        Set<String> mySet = new TreeSet<String>();
        String list = ("word,another,word2"); // actually this is much bigger
        String[] wordArr = list.split(",");
        for (String s : wordArr) {
            mySet.add(s);
        }
    }


}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • No google.commons please. My professor may not allow this. – kBisla Jan 16 '14 at 01:26
  • @BrianRoach Well I don't think I'd add it if I wanted to literally write this code. But for a big enough project I probably already have it in my classpath. – Daniel Kaplan Jan 16 '14 at 01:28
  • 1
    Why would use use a giant 3rd party dependency to do: `new TreeSet(Array.asList(wordArr));` (accidentally deleted this instead of editing) – Brian Roach Jan 16 '14 at 01:28