在 java 中创建包含单个 T 值的 Enumeration<T> 对象的最简单方法是什么?
What is the easiest way to create an Enumeration<T> object containing a single T value in java?
一般来说,我想知道从 java.
中给定的单个 T
对象生成 Enumeration<T>
对象的最方便和可读的方法是什么
在我的例子中,我有一个 String
对象,我需要将其包装到 Enumeration<T>
中。我需要与需要枚举作为输入的遗留 API 进行互操作。
这就是我正在做的事情:
public Enumeration<String> getEnumeration(String value) {
Vector<String> result = new Vector<>();
result.add(value);
return result.elements();
}
有没有更简洁的方法?
谢谢
使用Collections
.
import java.util.Collections;
...
return Collections.enumeration( Collections.singleton(value) );
一般来说,我想知道从 java.
中给定的单个T
对象生成 Enumeration<T>
对象的最方便和可读的方法是什么
在我的例子中,我有一个 String
对象,我需要将其包装到 Enumeration<T>
中。我需要与需要枚举作为输入的遗留 API 进行互操作。
这就是我正在做的事情:
public Enumeration<String> getEnumeration(String value) {
Vector<String> result = new Vector<>();
result.add(value);
return result.elements();
}
有没有更简洁的方法? 谢谢
使用Collections
.
import java.util.Collections;
...
return Collections.enumeration( Collections.singleton(value) );