PDF download Download Article
Learn how to initialize your lists and arrays in Java easily
PDF download Download Article

Are you trying to figure out how to initialize a list in Java? In Java , a List is a child interface of Collection that displays elements in their insertion order, and duplicates are permitted. [1] Initializing is the process of giving a variable an initial value. If you don't initialize, you'll likely get errors in your code, but thankfully, each list initialization method is straightforward. In this article, we will cover 7 ways to initialize a list (or an array that looks like a list) in Java.

Things You Should Know

  • You cannot add to a List, but you can create an array that looks like a List that can be added to later.
  • Lists can hold various data types, such as strings, integers, floats, doubles, and more.
  • The "add()" and "ArrayList" methods can be used with some other methods to create mutable lists.
Method 1
Method 1 of 7:

add() to an ArrayList

PDF download Download Article
  1. If you already have an ArrayList you can skip this, but if not, create one. Use the following code snippet to create your ArrayList where <Object> is your data type and capacity is how many elements will be in the list.
    • Strings can hold a range of characters (including numbers). Integers are numbers that you can do integer operations on (i.e. addition, subtraction).
    • If you don't know how many elements will be in the list, you can leave capacity blank. The array will then resize itself if it goes over the default capacity of 10.
    •  ArrayList 
       < 
       Object 
       > 
       listName 
       = 
       new 
       ArrayList 
       < 
       Object 
       >( 
       capacity 
       ); 
      
  2. The add() method can be used to add elements to a list (or various other types of collections). The add() method allows you to add one element at a time and is an easy way to initialize your list with some values.
    • Below is an example of how to use the add() method to add some string values to a list. Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
    •  import 
       java.util.ArrayList 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       [] 
       args 
       ) 
       { 
       // create a new ArrayList 
       ArrayList 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       < 
       String 
       >(); 
       // elements being added to the list 
       listDogs 
       . 
       add 
       ( 
       "poodle" 
       ); 
       listDogs 
       . 
       add 
       ( 
       "golden retriever" 
       ); 
       listDogs 
       . 
       add 
       ( 
       "chihuahua" 
       ); 
       // print the contents of the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  3. Advertisement
Method 2
Method 2 of 7:

Arrays.asList()

PDF download Download Article
  1. The Array.asList() method will convert an array into a List collection. This List is immutable, which means you cannot add or remove elements from it.
       String 
       [] 
       dogs 
       = 
       { 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       }; 
      
  2. The array you created will be used as the argument for this method.
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Arrays 
       . 
       asList 
       ( 
       dogs 
       ); 
      
  3. You can use this object to print the list, for example. The complete snippet to print this list using the Arrays.asList() method is below. Replace dogs , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
       import 
       java.util.Arrays 
       ; 
       import 
       java.util.List 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       [] 
       args 
       ) 
       { 
       // create a new array 
       String 
       [] 
       dogs 
       = 
       { 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       }; 
       // pass the array as an argument 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Arrays 
       . 
       asList 
       ( 
       dogs 
       ); 
       // print the contents of the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  4. By using the Arrays.asList() method as an argument for a new ArrayList , you can combine methods to make a list that can be changed (a mutable list).
    • Use the following code snippet to make your mutable list:
    •  List 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       <>( 
       Arrays 
       . 
       asList 
       ( 
       dogs 
       )); 
      
  5. The following code will print your initial list and your modified list. Remember to add each new element with a separate add() method.
       import 
       java.util.ArrayList 
       ; 
       import 
       java.util.Arrays 
       ; 
       import 
       java.util.List 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // create a new array 
       String 
       [] 
       dogs 
       = 
       { 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       }; 
       // create a mutable list with the initial array 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       <>( 
       Arrays 
       . 
       asList 
       ( 
       dogs 
       )); 
       // print the contents of the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       "List : " 
       + 
       listDogs 
       . 
       toString 
       ()); 
       // add something to the list 
       listDogs 
       . 
       add 
       ( 
       "german shepherd" 
       ); 
       // print the contents of the updated list 
       System 
       . 
       out 
       . 
       println 
       ( 
       "Modified list : " 
       + 
       listDogs 
       . 
       toString 
       ()); 
       } 
       } 
      
  6. Advertisement
Method 3
Method 3 of 7:

Collections.addAll()

PDF download Download Article
  1. You can use either ArrayList or List , though we've used ArrayList for this example. Using either one will still allow this list to be mutable.
       ArrayList 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       < 
       String 
       >(); 
      
  2. This method first defines the collection (ArrayList or List) and then includes the elements that should be included in the list.
       Collections 
       . 
       addAll 
       ( 
       listDogs 
       , 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ); 
      
  3. You can print the list you've created like the methods before. Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
    • Note that if you used List instead of ArrayList , add import java.util.List; to the top of your code. You must keep the ArrayList package, as it's utilized in creating your list.
    •  import 
       java.util.Collections 
       ; 
       import 
       java.util.ArrayList 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // create a new list 
       ArrayList 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       < 
       String 
       >(); 
       // initialize the list 
       Collections 
       . 
       addAll 
       ( 
       listDogs 
       , 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ); 
       // print the list contents 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  4. The following code will print your initial list as well as your modified list. Remember to add each new element with a separate add() method.
       import 
       java.util.Collections 
       ; 
       import 
       java.util.ArrayList 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // create a new list 
       ArrayList 
       < 
       String 
       > 
       listDogs 
       = 
       new 
       ArrayList 
       < 
       String 
       >(); 
       // initialize the list 
       Collections 
       . 
       addAll 
       ( 
       listDogs 
       , 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ); 
       // print the list contents 
       System 
       . 
       out 
       . 
       println 
       ( 
       "List : " 
       + 
       listDogs 
       . 
       toString 
       ()); 
       // add something to the list 
       listDogs 
       . 
       add 
       ( 
       "german shepherd" 
       ); 
       // print the contents of the updated list 
       System 
       . 
       out 
       . 
       println 
       ( 
       "Modified list : " 
       + 
       listDogs 
       . 
       toString 
       ()); 
       } 
       } 
      
  5. Advertisement
Method 4
Method 4 of 7:

Collections.unmodifiableList()

PDF download Download Article
  1. If you want a list that cannot be altered, you can use the unmodifiableList() method with the Collections class.
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Collections 
       . 
       unmodifiableList 
       ( 
       Arrays 
       . 
       asList 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       )); 
      
  2. Once you've set this list, you cannot change it using add() or any other method. Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
       import 
       java.util.List 
       ; 
       import 
       java.util.Arrays 
       ; 
       import 
       java.util.Collections 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // create the list 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Collections 
       . 
       unmodifiableList 
       ( 
       Arrays 
       . 
       asList 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       )); 
       // print the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  3. Advertisement
Method 5
Method 5 of 7:

Collections.singletonList()

PDF download Download Article
  1. If you want a list that holds only one element, you can use the singletonList() method with the Collections class. This list is immutable.
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Collections 
       . 
       singletonList 
       ( 
       "poodle" 
       ); 
      
  2. Once you've set this list, you cannot change it using add() or any other method. Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
       import 
       java.util.List 
       ; 
       import 
       java.util.Collections 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // create the list 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Collections 
       . 
       singletonList 
       ( 
       "poodle" 
       ); 
       // print the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  3. Advertisement
Method 6
Method 6 of 7:

Stream.of() (Java 8)

PDF download Download Article
  1. There are a few syntaxes you can use with Stream.of() , but all of them start with the same code snippet:
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Stream 
       . 
       of 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ) 
      
  2. You can end the method in three ways using the {{kbd|collect()} method. These methods will all return similar-looking lists, though each one works a bit differently: [2]
    • .collect(Collectors.toList()); collects the elements and displays them as a list.
    • .collect(Collectors.toCollection(ArrayList::new)); collects the elements and displays them as an array.
    • .collect(Collectors.collectingAndThen(Collectors.toList(),Collections::unmodifiableList)); collects the elements and displays them as an immutable list.
  3. The example below completes the stream with .collect(Collectors.toList()); ; if you'd like to use a different method, replace .collect(Collectors.toList()); with the method of your choice from the list above.
    • Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
    •  import 
       java.util.List 
       ; 
       import 
       java.util.stream.Collectors 
       ; 
       import 
       java.util.stream.Stream 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // creating a list using the toList syntax 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       Stream 
       . 
       of 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ) 
       . 
       collect 
       ( 
       Collectors 
       . 
       toList 
       ()); 
       // printing the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  4. Advertisement
Method 7
Method 7 of 7:

List.of() (Java 9)

PDF download Download Article
  1. With Java 9, you can use the List.of() method to create a simple, unmodifiable list. [3]
       List 
       < 
       String 
       > 
       listDogs 
       = 
       List 
       . 
       of 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ); 
      
  2. You can print your immutable list by using the following code. Replace ExampleList , listDogs , and the arguments "poodle", "golden retriever", and "chihuahua" with your own data.
       import 
       java.util.List 
       ; 
       public 
       class 
       ExampleList 
       { 
       public 
       static 
       void 
       main 
       ( 
       String 
       args 
       []) 
       { 
       // creating an unmodifiable List.of list 
       List 
       < 
       String 
       > 
       listDogs 
       = 
       List 
       . 
       of 
       ( 
       "poodle" 
       , 
       "golden retriever" 
       , 
       "chihuahua" 
       ); 
       // printing the list 
       System 
       . 
       out 
       . 
       println 
       ( 
       listDogs 
       ); 
       } 
       } 
      
  3. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Thanks to all authors for creating a page that has been read 8,726 times.

      Is this article up to date?

      Advertisement