0

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 = ?
Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • create another set @Thirumal – Divyesh Kanzariya Aug 03 '20 at 10:06
  • `SUBSCRIBER_ALLOWED_NUMBER_CD = Stream.concat(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD.stream(),INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD.stream()) .collect(Collectors.toSet());` – Hadi J Aug 03 '20 at 10:10
  • 3
    *Why* is this getting downvoted? Just look at the weak answers - this trivial problem has no neat JDK-only solution. – drekbour Aug 03 '20 at 10:19
  • 3
    Does this answer your question? [Is there a better way to combine two string sets in java?](https://stackoverflow.com/a/30139228/4207306) – Eklavya Aug 03 '20 at 10:40

5 Answers5

4

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());
Hadi J
  • 16,989
  • 4
  • 36
  • 62
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 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());
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

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();
drekbour
  • 2,895
  • 18
  • 28
1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    `Set SUBSCRIBER_ALLOWED_NUMBER_CD = new HashSet<>(ORG_SUBSCRIBER_ALLOWED_NUMBER_CD);SUBSCRIBER_ALLOWED_NUMBER_CD.addAll(INDIVIDUAL_SUBSCRIBER_ALLOWED_NUMBER_CD);` – Hadi J Aug 03 '20 at 10:06
1

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.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 2
    Since this is joining two sets created via `Set.of(…)`, the OP might want to use the collector `Collectors.toUnmodifiableSet()` instead of `Collectors.toSet()`, to get a set with the same semantics (wrapping it in `Collections.unmodifiableSet(…)` becomes unnecessary then). Likewise, the alternative might use `Set.copyOf(…)` instead of `Collections.unmodifiableSet(…)`. It may cause a little higher initialization time, which is compensated with potentially higher performance in the subsequent runtime. – Holger Aug 03 '20 at 11:39