Changing map key using enum on java

Posted: January 16, 2013 in web

enum:

public enum CarType {
	SUV("Suv"),SEDAN("Sedan"),TRUCK("Truck");

	private final String type;
	
	CarType(String type){
		this.type = type;
	}
	public String getType(){
		return type;
	}
	
	public static Map<String, CarType> getMap{
		Map<String, CarType> hashMap = new LinkedHashMap<String, CarType>();
		for(CarType carType : CarType.values()){		
			hasMap.put(carType.type, carType);

		}
		return hashMap;
	}

}

bean:

...
public Map<String, CarType> getCarTypes(){
	Map<String, CarType> map = CarType.getMap();
	//need to change Truck label to Double Cab
	if("FL".equals(someService.getState())){
		map.remove(CarType.TRUCK.getType());
		map.put("Double Cab", CarType.TRUCK);
	}
	return map;

}

...

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s