Initialize List the way Arrays are initialized

If you want a fixed size list, wherein you don’t want to add/remove elements into it then probably this trick will save some line of code:

Consider this:

List<Character> vowels = new ArrayList<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');


This could have also be written as:


 List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');

Trying to add/remove elements from this list will result in an UnsupportedOperationException exception

Advertisement

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.