Skip to content

Instantly share code, notes, and snippets.

@vincenthauser
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincenthauser/417331e1228bd8ea1bf4 to your computer and use it in GitHub Desktop.
Save vincenthauser/417331e1228bd8ea1bf4 to your computer and use it in GitHub Desktop.
Immutable list
// Immutable -- returns an unmodifiable List instead
public class SafeStates {
private final String[] states = new String[] { "Alabama", "Alaska", ... };
private final List statesAsList
= new AbstractList() {
public Object get(int n) {
return states[n];
}
public int size() {
return states.length;
}
};
public List getStates() {
return statesAsList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment