David Moreno García
David Moreno García

Reputation: 4523

Transform Set<Keyword> into String[]

I have an object Keyword that stores a String with the text of the keyword and a set o keywords (Set<Keyword>) that I need to transform into a String array. Is there a quick/easy way to do this or I need to iterate the set and add each keyword one by one?

EDIT:

For those asking for Keyword class:

@Entity
public class Keyword {

    // ...

    @Basic
    private String value;

    // ...

    // Getters & Setters

}

Upvotes: 1

Views: 163

Answers (5)

Gladwin Burboz
Gladwin Burboz

Reputation: 3549

You may need to add toString() method to your Keyword class as shown below. Or you can use a separate transformer class/method.

class Keyword {
    private String value;

    Keyword(String v) {
        this.value = v;
    }

    public String toString() {
        return value;
    }
}

.

I would say iterate the set and add each keyword one by one is your best possible strategy.

System.out.println(toStringArray(set));

.

private static String[] toStringArray(Collection<?> set) {
    String[] arr = null;
    if (set != null) {
        arr = new String[set.size()];
        int i = 0;
        for (Object o : set) {
            arr[i++] = o.toString();
        }
    }
    return arr;
}

.

However if you really want, you can have a dirty workaround as shown below. Only issue here is that your keyword value cannot contain comma (,) as it is used by split() method.

String str = set.toString();
str = str.substring(1, str.length() - 1);
String[] asStringArray = str.split(",");
System.out.println(asStringArray);

Upvotes: 1

fge
fge

Reputation: 121720

If you use Guava, you may use this:

Lists.transform(Lists.newArrayList(theSet), Functions.usingToString())
    .toArray(new String[theSet.size()])

And this only scratches the surface of what Guava can actually do.

Upvotes: 3

darijan
darijan

Reputation: 9775

Every class that implements Collection intefrace (and that includes Set) has toArray() method:

String[] array= set.toArray(new String[0]);

In case of a set that is parametrized with some other type, e.g. Set<Keyword> you would have to do something like:

Keyword[] array= set.toArray(new Keyword[0]);
String[] stringArray= new String[array.length];

for (int i=0; i<array.length; i++) {
    stringArray[i]= array[i].getThatString();
}

Upvotes: 3

arshajii
arshajii

Reputation: 129507

Try this:

String[] arr = set.toArray(new String[set.size()]);

... is what I would have said, if you had a Set<Object>.


No, there is no way to directly convert a Set<Keyword> to a String[] since there is no direct relationship between Keyword and String. You will have to iterate over the set:

String[] arr = new String[set.size()];
int i = 0;
for (Keyword word : set)
    arr[i++] = word.toString();

Upvotes: 3

sidshu
sidshu

Reputation: 333

There is no specific way to do this . You can either convert Set to Object[] using set.toArray and then iterate over the array or iterate over the set directly

Upvotes: 1

Related Questions