Jeff Lamb
Jeff Lamb

Reputation: 5865

Enumeration lookup in Java

I'm not too familiar with Java and need some help with an enumeration lookup (I think that's what it's called?) I have many years of C experience, so I know what I'm trying to accomplish using C, but I don't know the equivalent syntax or construct in Java.

I don't have a C environment on this computer, so the syntax might be off, but this is what I'm trying to accomplish:

typedef enum regions_t {
     REGION_CALIFORNIA,
     REGION_HAWAII,
     REGION_LOUISIANA,
     REGION_NUM_REGIONS
} regions_t;

typedef struct regionData_t {
    regions_t regionName;
    char_t[50] url;
    int32_t population;
 } regionData_t;

 regionData_t myRegions[REGION_NUM_REGIONS] {
     {REGION_CALIFORNIA, "http://http://california.gov/", 10123321},
     {REGION_HAWAII, "http://hawaii.gov", 5123321},
     {REGION_LOUISIANA, "http://louisiana.gov/", 8123321}
 }

This way, I can access, for example, the url data in this fashion, which is easy to read and easy to expand upon:

myRegions[REGION_HAWAII].url

How do I accomplish the same idea in Java?

Upvotes: 0

Views: 527

Answers (4)

Renato
Renato

Reputation: 13720

I suspect you need something like this:

public enum Region {
    CALIFORNIA("california-url", 1),
    HAWAII("hawaii-url", 2),
    LOUISIANA("louisiana-url", 3);

    private String url;
    private int population;

    private Region(String url, int population) {
        this.url = url;
        this.population = population;
    }

    public int getPopulation() {
        return population;
    }

    public String url() {
        return url;
    }
}

Notice that the String url should probably come from an external resource.

Upvotes: 2

user1270627
user1270627

Reputation:

Enums in Java are actually Classes. So you can do whatever you like with them except a few things like object instantiation at runtime.

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61550

Java has enums also with slightly different syntax, but instead of a struct you'll want to encapsulate all of this information in a class. I am listing as direct of a translation as I can:

public enum Region
{
    REGION_CALIFORNIA,
    REGION_HAWAII,
    REGION_LOUISIANA;
}

public class RegionData
{
   Region region;
   String url;
   int    population;

   public RegionData(Region region, String url, int population)
   {
      this.region     = region;
      this.url        = url;
      this.population = population;
   }
}

Then you would want to look into the HashMap of HashTable classes to get the lookup functionality you want:

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

http://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html

Upvotes: 0

Bombe
Bombe

Reputation: 83993

Quick and dirty:

public enum Region {
    CALIFORNIA, HAWAII, LOUISIANA
}

public class RegionData {
    public final Region region;
    public final String url;
    public final int population;
    public RegionData(Region region, String url, int population) {
        this.region = region;
        this.url = url;
        this.popuplation = population;
    }
}

…

Map<Region, RegionData> regionData = new HashMap<Region, RegionData>();
regionData.put(Region.CALIFORNIA, new RegionData(Region.CALIFORNIA, "http://http://california.gov/", 10123321));
…

System.out.println(regionData.get(Region.CALIFORNIA).url);

But you can also use the fact that enums in Java are normal classes, too.

public enum Region {
    CALIFORNIA("http://http://california.gov/", 10123321),
    HAWAII("http://hawaii.gov", 5123321),
    LOUISIANA("http://louisiana.gov/", 8123321);
    public final String url;
    public final int population;
    private Region(String url, int population) {
        this.url = url;
        this.population;
    }
}

…

System.out.println(Region.CALIFORNIA.url);

Upvotes: 1

Related Questions