Monday 3 June 2019

How to create ArrayList from Array


Approach 1: Using Arrays.asList method
String[] hobbies = {"tennis", "football", "cricket"};
List<String> myHobbies = Arrays.asList(hobbies);

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

public class App {

 public static void main(String args[]) {
  String[] hobbies = {"tennis", "football", "cricket"};
  
  List<String> myHobbies = Arrays.asList(hobbies);
  
  System.out.println(myHobbies);
  
 }
}

Output
[tennis, football, cricket]

Since Arrays.asList method return an immutable list, you can’t add new elements or remove existing elements from the list.

For example, in the below example, I am trying to remove the element from the list created by Arrays.asList, but I end up in ‘UnsupportedOperationException’.


App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

public class App {

 public static void main(String args[]) {
  String[] hobbies = {"tennis", "football", "cricket"};
  
  List<String> myHobbies = Arrays.asList(hobbies);
  
  System.out.println(myHobbies);
  
  myHobbies.remove(1);
  
 }
}

Output
[tennis, football, cricket]
Exception in thread "main" java.lang.UnsupportedOperationException
         at java.util.AbstractList.remove(AbstractList.java:161)
         at com.sample.app.App.main(App.java:15)

If you want to update the list, use below statement while creating the list.
List<String> myHobbies = new ArrayList<>(Arrays.asList(hobbies));


App.java
package com.sample.app;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App {

 public static void main(String args[]) {
  String[] hobbies = { "tennis", "football", "cricket" };

  List<String> myHobbies = new ArrayList<>(Arrays.asList(hobbies));

  System.out.println(myHobbies);

  myHobbies.remove(1);

  System.out.println(myHobbies);

 }
}

Output
[tennis, football, cricket]
[tennis, cricket]

Approach 2: Iterate over the array and add elements to the list.
String[] hobbies = { "tennis", "football", "cricket" };

List<String> myHobbies = new ArrayList<>(hobbies.length);

for (String hobby : hobbies) {
         myHobbies.add(hobby);
}


App.java
package com.sample.app;

import java.util.ArrayList;
import java.util.List;

public class App {

 public static void main(String args[]) {
  String[] hobbies = { "tennis", "football", "cricket" };

  List<String> myHobbies = new ArrayList<>(hobbies.length);

  for (String hobby : hobbies) {
   myHobbies.add(hobby);
  }

  System.out.println(myHobbies);

  myHobbies.remove(1);

  System.out.println(myHobbies);

 }
}

Output
[tennis, football, cricket]
[tennis, cricket]

Approach 3: Using Collections.addAll method.
String[] hobbies = { "tennis", "football", "cricket" };
List<String> myHobbies = new ArrayList<>(hobbies.length);
Collections.addAll(myHobbies, hobbies);


App.java
package com.sample.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class App {

 public static void main(String args[]) {
  
  String[] hobbies = { "tennis", "football", "cricket" };
  List<String> myHobbies = new ArrayList<>(hobbies.length);
  Collections.addAll(myHobbies, hobbies);

  System.out.println(myHobbies);
 }
}

Approach 3: Using List.of method (Supported from Java9 onwards)

String[] hobbies = { "tennis", "football", "cricket" };
List<String> myHobbies = List.of(hobbies);

Approach 4: Using streams (Java8 onwards).
String[] hobbies = { "tennis", "football", "cricket" };
List<String> myHobbies = Stream.of(hobbies).collect(Collectors.toList());


App.java
package com.sample.app;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class App {

 public static void main(String args[]) {

  String[] hobbies = { "tennis", "football", "cricket" };
  List<String> myHobbies = Stream.of(hobbies).collect(Collectors.toList());


  System.out.println(myHobbies);
 }
}

Approach 5: Using Arrays.stream() method
String[] hobbies = { "tennis", "football", "cricket" };
List<String> myHobbies = Arrays.stream(hobbies).collect(Collectors.toList());


App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class App {

 public static void main(String args[]) {

  String[] hobbies = { "tennis", "football", "cricket" };
  List<String> myHobbies = Arrays.stream(hobbies).collect(Collectors.toList());


  System.out.println(myHobbies);
 }
}


You may like




No comments:

Post a Comment