Arrays.aslist immutable
Learn different and useful ways to convert array to list in Java. In this example, we will use Java 8 classes and Google guava library for creating an arraylist from elements of given array. Show 1. Convert array to list Immutable arraylistIf you want to create an immutable arraylist instance backed by array elements then follow any given method below. 1.1. Collections.unmodifiableList()Use Collections.unmodifiableList() to get a immutable list Program output. [alex, brian, charles, david]1.2. ImmutableList.copyOf()If you have guava library in project then you can use this method as well to get immutable list out of string array. String[] namesArray = new String[] {"alex", "brian", "charles", "david"}; ListProgram output. 2. Convert array to list Mutable arraylistIf you want to create an mutable list instance backed by array elements then follow any given method below. 2.1. Arrays.asList()Use Arrays.asList() to get a mutable list from a array of elements. String[] namesArray = new String[] {"alex", "brian", "charles", "david"}; ListProgram output. 2.2. Lists.newArrayList()Again, if you have guava library in project then you can use this method as well to get mutable arraylist from array. String[] namesArray = new String[] {"alex", "brian", "charles", "david"}; ArrayListProgram output. [alex, brian, charles, david]Happy Learning !! Read More: A Guide to Java ArrayList Was this post helpful?Let us know if you liked the post. Thats the only way we can improve. Yes No |