We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 May 2015

How to convert Array into List

Following example shows how to convert Array into List using Arrays.asList(nameArray) method in Java.

<String> List<String> java.util.Arrays.asList(String... a)

@SafeVarargs

asList
@SafeVarargs
public static <T> List<T> asList(T... a) 
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:
     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
Parameters:
    a - the array by which the list will be backed
Returns:
    a list view of the specified array

Sample Program
ArrayToCollection.java
 
package com.javatutorialcorner.collection;

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

public class ArrayToCollection {

 public static void main(String[] args) {
  String[] nameArray = new String[] { "Spring", "Struts", "Hibernate",
    "Webservice", "SOAP Webservice", "RESTful Webservice" };
  List<String> nameList = Arrays.asList(nameArray);

  for (String name : nameList) {
   System.out.println(name);
  }

 }

}

Result
The above code will produce the following output.
Spring
Struts
Hibernate
Webservice
SOAP Webservice
RESTful Webservice

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to convert Array into List Rating: 5 Reviewed By: eHowToNow