如何在Java中合并两个不可修改的Set?

huangapple go评论71阅读模式
英文:

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&lt;Long&gt; ORG_SUBSCRIBER_ALLOWED_NUMBER_CD = Set.of(COMPANY_GST, GOVERNMENT_BODY_GST);

public static final Set&lt;Long&gt; 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&lt;Long&gt; 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&lt;Long&gt; 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&lt;Long&gt; 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&lt;Long&gt; 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&lt;Long&gt; 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&lt;Long&gt; SUBSCRIBER_ALLOWED_NUMBER_CD = new HashSet&lt;&gt;();
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&lt;Long&gt; 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&lt;Long&gt; SUBSCRIBER_ALLOWED_NUMBER_CD;
static {
	Set&lt;Long&gt; all = new HashSet&lt;&gt;();
	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.

huangapple
  • 本文由 发表于 2020年8月3日 18:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63227389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定