简体   繁体   English

变换集 <Keyword> 放入String []

[英]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. 我有一个对象Keyword,它存储一个带有关键字文本的String和一个我需要转换为String数组的set o关键字( Set<Keyword> )。 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

}

Try this: 尝试这个:

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

... is what I would have said, if you had a Set<Object> . 如果您有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 . 不,由于KeywordString之间没有直接关系,因此无法直接将Set<Keyword>转换为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();

Every class that implements Collection intefrace (and that includes Set ) has toArray() method: 每个实现Collection intefrace(包括Set )的类都具有toArray()方法:

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

In case of a set that is parametrized with some other type, eg Set<Keyword> you would have to do something like: 如果参数集使用其他类型的参数化,例如Set<Keyword>您必须执行以下操作:

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();
}

If you use Guava , you may use this: 如果您使用Guava ,则可以使用以下命令:

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

And this only scratches the surface of what Guava can actually do. 这只是刮擦番石榴实际功能的表面。

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 您可以使用set.toArray将Set转换为Object [],然后遍历数组或直接遍历set

You may need to add toString() method to your Keyword class as shown below. 您可能需要向关键字类添加toString()方法,如下所示。 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. 这里唯一的问题是,关键字值不能包含逗号( , ),因为split()方法会使用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM