英文:
How to merge two unmodifiable Sets in Java?
问题
如何合并两个“不可修改的静态最终”集合?
public static final Set<Long> ORG_SUBSCRIBER_ALLOWED_NUMBER_CD = Set.of(COMPANY_GST, GOVERNMENT_BODY_GST);
public static final Set<Long> INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD = Set.of(BUSINESS_PAN, INDIVIDUAL_PAN);
我想要将上述静态最终集合合并为一个集合(一次性初始化),因为它是一个“Class”变量
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = ?
英文:
How to merge two unmodifiable static final
sets?
public static final Set<Long> ORG_SUBSCRIBER_ALLOWED_NUMBER_CD = Set.of(COMPANY_GST, GOVERNMENT_BODY_GST);
public static final Set<Long> INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD = Set.of(BUSINESS_PAN, INDIVIDUAL_PAN);
I want to combine the above static final sets into one set (one-statement initialization), because it's a Class
variable
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = ?
答案1
得分: 4
Stream#concat也很有用
Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.concat(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD.stream(),
INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD.stream())
.collect(Collectors.toSet());
英文:
Stream#concat is useful too
Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.concat(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD.stream(),
INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD.stream())
.collect(Collectors.toSet());
答案2
得分: 2
If you want to use streams:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD, INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toSet());
Or, in your case, and as mentioned in the comment, you can use Collectors.toUnmodifiableSet():
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD, INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toUnmodifiableSet());
英文:
If you want to use streams:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD, INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toSet());
Or, in your case, and as mentioned in comment, you can use Collectors.toUnmodifiableSet() :
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD, INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toUnmodifiableSet());
答案3
得分: 2
仅提醒一下,相较于仅使用JDK的解决方案,Guava仍然能够更加清晰地处理这种情况(可能更具性能)。以下是示例代码:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = ImmutableSet.builder()
.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD)
.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.build();
英文:
Just to point out that Guava still handles this case more more readably (and probably much more performant) than the JDK-only solutions:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = ImmutableSet.builder()
.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD)
.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.build();
答案4
得分: 1
有什么理由你不能创建一个第三个集合,然后将这两个最终的集合都添加进去呢?
Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = new HashSet<>();
SUBSCRIBER_ALLOWED_NUMBER_CD.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD);
SUBSCRIBER_ALLOWED_NUMBER_CD.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD);
前两个集合被声明为 final
只意味着你不能修改那些集合。它并不阻止你将这些集合读取到另一个新的集合中。
英文:
Any reason why you can't create a third set and just add both of the two final sets?
Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD = new HashSet<>();
SUBSCRIBER_ALLOWED_NUMBER_CD.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD);
SUBSCRIBER_ALLOWED_NUMBER_CD.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD);
The first two sets' being final
just means that you can't modify those sets. It does not prevent you from reading the sets into another new collection.
答案5
得分: 1
以下是翻译好的代码部分:
一种声明初始化方式,如果更喜欢:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Collections.unmodifiableSet(
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD,
INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toSet()));
或者,也许更易读:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD;
static {
Set<Long> all = new HashSet<>();
all.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD);
all.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD);
SUBSCRIBER_ALLOWED_NUMBER_CD = Collections.unmodifiableSet(all);
}
如果不期望第三个集合不可修改,请忽略`Collections.unmodifiableSet`调用。
英文:
A one-statement initialization, if preferred:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD =
Collections.unmodifiableSet(
Stream.of(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD,
INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD)
.flatMap(Set::stream)
.collect(Collectors.toSet()));
Or, perhaps more readable:
public static final Set<Long> SUBSCRIBER_ALLOWED_NUMBER_CD;
static {
Set<Long> all = new HashSet<>();
all.addAll(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD);
all.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD);
SUBSCRIBER_ALLOWED_NUMBER_CD = Collections.unmodifiableSet(all);
}
Ignore the Collections.unmodifiableSet
call if the third set is not expected to be unmodifiable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论