Arrays.aslist in Java
Hello. In this tutorial, we will make understand and implement java.util.Arrays.asList(…) method.
1. Introduction
Arrays.asList()
method in java is used to create a fixed-size list in java. It is a static method that returns a list of homogeneous elements and does not throw any exceptions. The method is represented by the below syntax and below reports compilation errors if heterogeneous elements are passed.
Method syntax
1 | public static <T> List<T> asList(T... a) |
2. Practice
Let us dive into some practice stuff from here and I am assuming that you already have Java 1.8 or greater installed on your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.
2.1 Creating an implementation class
Create a java file that will show the implementation of the asList(…)
method. example1()
and example2()
will be responsible to show the usage of the asList(…)
method via code snippets and example3()
will throw UnsupportedOperationException
if something unexpected happens.
Implementation class
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.practice; import java.util.Arrays; import java.util.List; // pojo class class User { private int id; private String name; public User( int id, String name) { this .id = id; this .name = name; } public int getId() { return id; } public String getName() { return name; } @Override public String toString() { return "[id=" + id + ", name=" + name + "]" ; } public static List<User> createUsers() { return Arrays.asList( new User( 101 , "abc" ), new User( 102 , "pqr" )); } } // implementation class public class ArraysAsListExample { // syntax - static <T> List<T> asList(T... a) private static void example1() { List<String> elements = Arrays.asList( "apple" , "banana" , "kiwi" , "mango" ); System.out.println(elements); } private static void example2() { List<User> users = User.createUsers(); users.forEach(user -> System.out.println(user)); } private static void example3() { List<User> users = User.createUsers(); users.forEach(user -> System.out.println(user)); // adding new element to the list // will throw java.lang.UnsupportedOperationException users.add( new User( 103 , "rst" )); } // driver code public static void main(String[] args) { example1(); System.out.println( "\n" ); example2(); System.out.println( "\n" ); example3(); } } |
3. Demo
Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
4. Summary
In this tutorial, we discussed the practical implementation of Arrays.asList()
method. You can download the source code from the Downloads section.
5. Download the Project
This was a tutorial to understand the Arrays.asList()
method in java.
You can download the full source code of this example here: Arrays.aslist in Java