gpt4 book ai didi

Filling a List with all enum values in Java(用Java中的所有枚举值填充列表)

转载 作者:bug小助手 更新时间:2023-10-25 12:51:11 29 4
gpt4 key购买 nike



I would like to fill a list with all possible values of an enum

Since I recently fell in love with EnumSet, I leveraged allOf()

我想用一个枚举的所有可能值填充一个列表,因为我最近爱上了EnumSet,我利用Allof()



EnumSet<Something> all = EnumSet.allOf( Something.class);
List<Something> list = new ArrayList<>( all.size());
for (Something s : all) {
list.add( s);
}
return list;


Is there a better way (as in non obfuscated one liner) to achieve the same result?

有没有更好的方法(就像在非模糊处理的情况下)来达到同样的效果?


更多回答

ArrayList<Something> list = new ArrayList<Something>(Arrays.asList(Something.values()))

ArrayList列表=新ArrayList(Arrays.asList(Something.values()))

优秀答案推荐

I wouldn't use a List in the first places as an EnumSet is more approriate but you can do

首先,我不会使用列表,因为EnumSet更合适,但您可以这样做



List<Something> somethingList = Arrays.asList(Something.values());


or



List<Something> somethingList =
new ArrayList<Something>(EnumSet.allOf(Something.class));


Class.getEnumConstants()

Class.getEnumConstants()



List<SOME_ENUM> enumList = Arrays.asList(SOME_ENUM.class.getEnumConstants());


There is a constructor for ArrayList which is

ArrayList有一个构造函数,它是



ArrayList(Collection<? extends E> c) 


Now, EnumSet extends AbstractCollection so you can just do

现在,EnumSet扩展了AbstractCollection,因此您可以只做



ArrayList<Something> all = new ArrayList<Something>(enumSet)


try

试试看



enum E {
E1, E2, E3
}

public static void main(String[] args) throws Exception {
List<E> list = Arrays.asList(E.values());
System.out.println(list);
}


List<Something> result = new ArrayList<Something>(all);


EnumSet is a Java Collection, as it implements the Set interface:

EnumSet是一个Java集合,因为它实现了Set接口:



public interface Set<E> extends Collection<E> 


So anything you can do with a Collection you can do with an EnumSet.

因此,您可以使用Collection执行的任何操作都可以使用EnumSet执行。



This is a bit more readable:

下面的代码更具可读性:



Object[] allValues = all.getDeclaringClass().getEnumConstants();


Try this:

试试这个:



... = new ArrayList<Something>(EnumSet.allOf(Something.class));


as ArrayList has a constructor with Collection<? extends E>. But use this method only if you really want to use EnumSet.

因为ArrayList有一个集合<?扩展E>。但只有当您真的想要使用EnumSet时才使用此方法。



All enums have access to the method values(). It returns an array of all enum values:

所有枚举都可以访问方法值()。它返回一个包含所有枚举值的数组:



... = Arrays.asList(Something.values());


You can use also:

您还可以使用:



Collections.singletonList(Something.values())


I know the question is asking about List but I think I have possible cleaner solutions as well as one solution using List that wasn't added yet.

我知道问题是关于List的,但我想我有可能更干净的解决方案,以及一个使用List的解决方案,但还没有添加。


I'm using Java 17

我使用的是Java 17


Simply use a array of enum


public class Main {
static public enum Something {
One,
Two,
Three,
Four,
Five,
;

}

public static void main(String[] args) {
Something[] somethings = Something.values();
for (Something something: somethings) {
System.out.printf("- %s\n", something);
}

}

}


Simple get all values of the enum with enum.values()

简单地用enum.values()获取枚举的所有值


Something[] somethings = Something.values();

Something[]Something=Something.Values();


Use list.of



import java.util.List;

public class Main {
static public enum Something {
One,
Two,
Three,
Four,
Five,
;

}

public static void main(String[] args) {
List<Something> somethings2 = List.of(Something.values());
for (Something something: somethings2) {
System.out.printf("- %s\n", something);
}
}

}


I've seen solutions using Arrays.asList( which as described in What is the difference between List.of and Arrays.asList?

我见过使用Arrays.asList的解决方案(如List.of和Arrays.asList之间的区别所述?



Arrays.asList returns a mutable list while the list returned by List.of is immutable



or



Arrays.asList allows null elements while List.of doesn't



And few other reasons.
Because theoretically, from an Array of enum you should't be able to mutate it (adding / removing elements) I think it makes more sense to use List.of
Anyways, it depends by use cases and both ways are probably valid

几乎没有其他原因。因为从理论上讲,您不应该能够从枚举数组中改变它(添加/删除元素)。我认为使用List.of Anyway更有意义,这取决于用例,两种方法都可能是有效的



private ComboBox gender;
private enum Selgender{Male,Famle};
ObservableList<Object> observableList =FXCollections.observableArrayList(Selgender.values());

更多回答

Sometimes EnumSet don't cut it. In particular when order matters

有时EnumSet不会把它砍掉。尤其是当秩序很重要的时候

@MonoThreaded What order did you have in mind? An EnumSet is in the natural order for an enum. The list will be in the same order as the set, so using a List doesn't change anything here.

@MonoThreaded你想要什么顺序?枚举集是按照枚举的自然顺序排列的。列表的顺序将与集合相同,因此使用列表不会更改此处的任何内容。

Sorry, ordering is out of scope for this question. I believe you meant values() in the first example.

对不起,订购超出了这个问题的范围。我相信您在第一个示例中指的是值()。

@PeterLawrey we use Enums all the time. When we design a workflow the approval process may not move in the Natural Order of the Enum, this is why a List<Enum> comes in handy vs an EnumSet.

@PeterLawrey我们一直在使用Enums。当我们设计工作流时,审批流程可能不会按照Enum的自然顺序移动,这就是为什么List比EnumSet更方便的原因。

EnumSet does not have a get() method (easy random access) while List does. This may be important for whatever the List/EnumSet is intended to be used for

EnumSet没有get()方法(轻松随机访问),而List有。无论列表/枚举集的用途是什么,这都可能很重要

worth clarifying that you do not get a java.util.ArrayList out of this, as stated in the question

值得澄清的是,正如问题中所述,您并没有从中得到一个java.util.ArrayList

Then you could simply do Something.class.getEnumConstants() right?

然后,您可以简单地执行Something.class.getEnumConstants(),对吗?

... or Something.values();

..。或Something.Values();

I am getting hexCode

我收到了十六进制码

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com