java中的通用枚举参数。这可能吗?
我正在尝试编写一个通用函数,它将接受任何枚举,并将值放入映射中以在下拉列表中使用。
这是我到目前为止所拥有的(对于特定枚举),我的函数 enumToMap 通常可以重写以接受任何枚举类型吗?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
private enum testenum{
RED,BLUE,GREEN
}
private static Map enumToMap(testenum e){
HashMap result = new HashMap();
List<testenum> orderReceiptKeys = Arrays.asList(e.values());
List<String> orderReceiptValues = unCapsCase(orderReceiptKeys);
for(int i=0;i<orderReceiptKeys.size();i++){
result.put(orderReceiptKeys.get(i), orderReceiptValues.get(i));
}
return result;
}
/**
* Converts a string in the form of 'TEST_CASE' to 'Test case'
* @param s
* @return
*/
private static String unCapsCase(String s){
StringBuilder builder = new StringBuilder();
boolean first=true;
for(char c:s.toCharArray()){
if(!first)
c=Character.toLowerCase(c);
if(c=='_')
c=' ';
builder.append(c);
first=false;
}
return builder.toString();
}
/**
* Converts a list of strings in the form of 'TEST_CASE' to 'Test case'
* @param l
* @return
*/
private static List<String> unCapsCase(List l){
List<String> list = new ArrayList();
for(Object o:l){
list.add(unCapsCase(o.toString()));
}
return list;
}
public static void main(String[] args) throws Exception {
try{
testenum e=testenum.BLUE;
Map map = enumToMap(e);
for(Object k:map.keySet()){
System.out.println(k.toString()+"=>"+map.get(k));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
感谢您的任何建议。
I am trying to write a generic function that will accept any enum, and put the values into a map for use in a drop down.
This is what I have so far, (for a specific enum), can my function enumToMap be rewritten generally to accept any enum type?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
private enum testenum{
RED,BLUE,GREEN
}
private static Map enumToMap(testenum e){
HashMap result = new HashMap();
List<testenum> orderReceiptKeys = Arrays.asList(e.values());
List<String> orderReceiptValues = unCapsCase(orderReceiptKeys);
for(int i=0;i<orderReceiptKeys.size();i++){
result.put(orderReceiptKeys.get(i), orderReceiptValues.get(i));
}
return result;
}
/**
* Converts a string in the form of 'TEST_CASE' to 'Test case'
* @param s
* @return
*/
private static String unCapsCase(String s){
StringBuilder builder = new StringBuilder();
boolean first=true;
for(char c:s.toCharArray()){
if(!first)
c=Character.toLowerCase(c);
if(c=='_')
c=' ';
builder.append(c);
first=false;
}
return builder.toString();
}
/**
* Converts a list of strings in the form of 'TEST_CASE' to 'Test case'
* @param l
* @return
*/
private static List<String> unCapsCase(List l){
List<String> list = new ArrayList();
for(Object o:l){
list.add(unCapsCase(o.toString()));
}
return list;
}
public static void main(String[] args) throws Exception {
try{
testenum e=testenum.BLUE;
Map map = enumToMap(e);
for(Object k:map.keySet()){
System.out.println(k.toString()+"=>"+map.get(k));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Thanks for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
enumToMap
方法的签名更改为如下所示:然后在调用
e.values()
的地方使用enumType.getEnumConstants()
代替。在您的 main 方法中,您可以像这样调用此方法:
正如 Seanizer 提到的,您还应该使用
EnumMap
作为Map
实现,而不是HashMap
。Change the signature of your
enumToMap
method to something like this:Then where you call
e.values()
useenumType.getEnumConstants()
instead.In your main method you can then call this method like:
As seanizer mentions, you should also be using
EnumMap
as theMap
implementation rather thanHashMap
.不要将 HashMap 与枚举一起使用,这就是 EnumMap 的设计目的是。
正如 Sun 中Collection Trail 的地图部分中所写Java教程:
Don't use a HashMap with enums, this is what an EnumMap was designed for.
As written in the Map section of the Collection Trail in the Sun Java Tutorial: