17 April 2013

How to copy all values from enum (enumeration) to to arraylist in java? (or anything from array to arralist)?

So you have some bunch of values of enum and you want them put to ArrayList.
What to do ?

Action plan:
  1. Get all values from enum and put them to array  MyEnums.values()
  2. Convert them to ArrayList  new ArrayList<MyEnums>(Arrays.asList(MyEnums.values()));
Solution:
Problem require 1 line of code, so go prepare coffee and enjoy reading:

ArrayList<MyEnums> enumlist = new ArrayList<MyEnums>(Arrays.asList(MyEnums.values()));

Simple :)