Issue
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 = ?
Solution
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.
Answered By - ernest_k
Answer Checked By - Willingham (JavaFixing Volunteer)