How do I find a subset of an enum in Java?

I have an enum as shown below:

public enum DatacenterEnum {
    ABC, PQR, LTR;

    // this tells my current local datacenter enum
    private static final DatacenterEnum ourLocation = findCurrentLocation();

}

      

Now I want to find a list of remote data centers. As an example, suppose if ourLocation

is ABC, then the list REMOTE_DATACENTER, or it can be an EnumSet (I will repeat this later) should have PQR and LTR, similarly for other combinations. What's the best way to do this?

+3


source to share


1 answer


Sounds like you want an add-on.



// contains all DatacenterEnum except ourLocation
Set<DatacenterEnum> remoteSet =
    EnumSet.complementOf(EnumSet.of(ourLocation));
// as a List, if you want
List<DataceterEnum> remoteList =
    new ArrayList<>(remoteSet);

      

+4


source







All Articles