Converting an Array to a Collection - Java Language Basics

Java examples for Language Basics:Array

Description

Converting an Array to a Collection

Demo Code

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Main {
  public void main(String[] argv) {
    int[] array = null;
    // Fixed-size list
    List list = Arrays.asList(array);

    // Growable list
    list = new LinkedList(Arrays.asList(array));

    // Duplicate elements are discarded
    Set set = new HashSet(Arrays.asList(array));
  }//from w ww. ja  v  a2s .  c o  m

}

Related Tutorials