Example usage for java.util Arrays asList

List of usage examples for java.util Arrays asList

Introduction

In this page you can find the example usage for java.util Arrays asList.

Prototype

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) 

Source Link

Document

Returns a fixed-size list backed by the specified array.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    String elements[] = { "A", "C", "D", "G", "F" };
    Set set = new TreeSet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }/*  w ww. jav  a2  s.com*/
}

From source file:SearchTest.java

public static void main(String args[]) {
    String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };

    // Convert to list
    ArrayList list = new ArrayList(Arrays.asList(simpsons));

    // Ensure list sorted
    Collections.sort(list);//from  w  w w. ja va 2s.c  o  m
    System.out.println("Sorted list: [length: " + list.size() + "]");
    System.out.println(list);

    // Search for element in list
    int index = Collections.binarySearch(list, "Maggie");
    System.out.println("Found Maggie @ " + index);

    // Search for element not in list
    index = Collections.binarySearch(list, "Jimbo Jones");
    System.out.println("Didn't find Jimbo Jones @ " + index);

    // Insert
    int newIndex = -index - 1;
    list.add(newIndex, "Jimbo Jones");
    System.out.println("With Jimbo Jones added: [length: " + list.size() + "]");
    System.out.println(list);
}

From source file:ShuffleTest.java

public static void main(String args[]) {
    String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };
    List list1 = Arrays.asList(simpsons);
    List list2 = Arrays.asList(simpsons);
    Random rand = new Random(100);
    Collections.shuffle(list1, rand);
    Collections.shuffle(list2, rand);
    System.out.println(list1);/* ww  w  . jav  a 2  s. c om*/
    System.out.println(list2);
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] numbers = { 7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4 };

    List<Integer> list = Arrays.asList(numbers);
    Set<Integer> set = new HashSet<Integer>(list);

    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        Object o = iterator.next();
        System.out.print(o + ", ");
    }//w ww .jav  a  2 s  .  co m
}

From source file:Main.java

public static void main(String[] a) throws Exception {
    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });

    FileOutputStream fos = new FileOutputStream("list.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(list);//from  w w  w.  ja v  a 2 s .c  o  m
    oos.close();

    FileInputStream fis = new FileInputStream("list.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    List anotherList = (List) ois.readObject();
    ois.close();

    System.out.println(anotherList);
}

From source file:Tail.java

public static void main(String args[]) throws Exception {
    String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter\0"));
    System.out.println(set.tailSet("Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Poodle\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter"));
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {

    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
    FileOutputStream fos = new FileOutputStream("list.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(list);//  ww  w.  j  a v  a 2 s .  co  m
    oos.close();

    FileInputStream fis = new FileInputStream("list.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    List anotherList = (List) ois.readObject();
    ois.close();

    System.out.println(anotherList);
}

From source file:Main.java

public static void main(String[] args) {
    // collect as typed array
    Stream<String> words = Stream.of("All", "men", "are", "created", "equal");
    final String[] wordsArray = words.toArray(String[]::new);
    Arrays.asList(wordsArray).forEach(n -> System.out.println(n));
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {

    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });

    FileOutputStream fos = new FileOutputStream("list.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(list);/* w  ww.j a  v a  2  s . c  o  m*/
    oos.close();

    FileInputStream fis = new FileInputStream("list.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    List anotherList = (List) ois.readObject();
    ois.close();

    System.out.println(anotherList);
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

    Comparator<Integer> normal = Integer::compare;
    Comparator<Integer> reversed = normal.reversed();
    Collections.sort(listOfIntegers, reversed);

    listOfIntegers.stream().forEach(e -> System.out.print(e + " "));
    System.out.println("");

}