Java Enum: Indian States and Union Territories with Codes
To manage Indian states and union territories in Java applications, an enum
is a type-safe and maintainable approach. Here's a Java enum listing all states and union territories with their corresponding ISO 3166-2:IN two-letter codes:
java
public enum IndianState {
ANDHRA_PRADESH("AP"),
ARUNACHAL_PRADESH("AR"),
ASSAM("AS"),
BIHAR("BR"),
CHHATTISGARH("CG"),
GOA("GA"),
GUJARAT("GJ"),
HARYANA("HR"),
HIMACHAL_PRADESH("HP"),
JHARKHAND("JH"),
KARNATAKA("KA"),
KERALA("KL"),
MADHYA_PRADESH("MP"),
MAHARASHTRA("MH"),
MANIPUR("MN"),
MEGHALAYA("ML"),
MIZORAM("MZ"),
NAGALAND("NL"),
ODISHA("OR"),
PUNJAB("PB"),
RAJASTHAN("RJ"),
SIKKIM("SK"),
TAMIL_NADU("TN"),
TELANGANA("TG"),
TRIPURA("TR"),
UTTAR_PRADESH("UP"),
UTTARAKHAND("UK"),
WEST_BENGAL("WB"),
ANDAMAN_AND_NICOBAR_ISLANDS("AN"),
CHANDIGARH("CH"),
DADRA_AND_NAGAR_HAVELI_AND_DAMAN_AND_DIU("DN"),
DELHI("DL"),
JAMMU_AND_KASHMIR("JK"),
LADAKH("LA"),
LAKSHADWEEP("LD"),
PUDUCHERRY("PY");
private final String code;
IndianState(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
Usage Example:
java
IndianState state = IndianState.KARNATAKA;
System.out.println(state.getCode()); // Outputs: KA
SQL Version: Indian States and Union Territories Table
You can store Indian states and UTs in a relational database with the following SQL schema and seed data.
sql
CREATE TABLE indian_states (
id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL,
code CHAR(2) NOT NULL UNIQUE
);
Insert Statements:
sql
INSERT INTO indian_states (name, code) VALUES
('Andhra Pradesh', 'AP'),
('Arunachal Pradesh', 'AR'),
('Assam', 'AS'),
('Bihar', 'BR'),
('Chhattisgarh', 'CG'),
('Goa', 'GA'),
('Gujarat', 'GJ'),
('Haryana', 'HR'),
('Himachal Pradesh', 'HP'),
('Jharkhand', 'JH'),
('Karnataka', 'KA'),
('Kerala', 'KL'),
('Madhya Pradesh', 'MP'),
('Maharashtra', 'MH'),
('Manipur', 'MN'),
('Meghalaya', 'ML'),
('Mizoram', 'MZ'),
('Nagaland', 'NL'),
('Odisha', 'OR'),
('Punjab', 'PB'),
('Rajasthan', 'RJ'),
('Sikkim', 'SK'),
('Tamil Nadu', 'TN'),
('Telangana', 'TG'),
('Tripura', 'TR'),
('Uttar Pradesh', 'UP'),
('Uttarakhand', 'UK'),
('West Bengal', 'WB'),
('Andaman and Nicobar Islands', 'AN'),
('Chandigarh', 'CH'),
('Dadra and Nagar Haveli and Daman and Diu', 'DN'),
('Delhi', 'DL'),
('Jammu and Kashmir', 'JK'),
('Ladakh', 'LA'),
('Lakshadweep', 'LD'),
('Puducherry', 'PY');
Summary
Using enums and database tables to manage Indian states ensures data consistency, type safety, and easier maintenance in Java and Spring Boot applications. This approach is ideal for address forms, filters, and regional validations.