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:Main.java

public static void main(String args[]) {
    // create an array of string objs
    String init[] = { "One", "Two", "Three", "One", "Two", "Three", "from java2s.com" };

    // create one list
    List<String> list = new ArrayList<String>(Arrays.asList(init));

    System.out.println("List value before: " + list);

    // create singleton list
    list = Collections.singletonList("One");

    System.out.println("List value after: " + list);
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new HashSet(Arrays.asList(elements));

    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);/*w  w w  .j  a  va2 s . c  o m*/
    oos.close();

    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set anotherSet = (Set) ois.readObject();
    ois.close();
    System.out.println(anotherSet);
}

From source file:MainClass.java

public static void main(String args[]) {
    String orig[] = { "1st", "2nd", "3rd", "4th", "5th", "1st", "2nd", "3rd", "4th", "5th" };
    String act[] = { "2nd", "3rd", "6th" };
    List origList = new ArrayList(Arrays.asList(orig));
    List actList = Arrays.asList(act);

    System.out.println(origList.retainAll(actList));
    System.out.println(origList);
}

From source file:Main.java

public static void main(String[] args) {
    // A string array with duplicate values
    String[] data = { "A", "C", "B", "D", "A", "B", "E", "D", "B", "C" };
    System.out.println("Original array         : " + Arrays.toString(data));

    List<String> list = Arrays.asList(data);
    Set<String> set = new HashSet<String>(list);

    System.out.print("Remove duplicate result: ");

    String[] result = new String[set.size()];
    set.toArray(result);// w w w.j a  v  a2s. co m
    for (String s : result) {
        System.out.print(s + ", ");
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String simpsons[] = { "B", "H", "L", "M", "H", "M", "R" };

    List list = new ArrayList(Arrays.asList(simpsons));

    // Ensure list sorted
    Collections.sort(list);/*from  w  w w.j ava 2 s.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, "M");
    System.out.println("Found M @ " + index);

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

}

From source file:Main.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

    List<Date> otherDates = Arrays.asList(new Date[] { simpleDateFormat.parse("01.01.2015 01:00:00"),
            simpleDateFormat.parse("01.01.2015 01:00:02") });

    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:01")));
    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:03")));
    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:00")));
}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    int howManyWords = 2;
    List<String> listOfWords = new ArrayList<>();
    Random random = new Random();
    listOfWords.addAll(Arrays.asList(randomMessages));
    List<String> selectedRandomMessages = new ArrayList<>();
    for (int i = 0; i < howManyWords; i++) {
        int randomNumber = random.nextInt(listOfWords.size());
        String randomItem = listOfWords.get(randomNumber);
        selectedRandomMessages.add(randomItem);
        listOfWords.remove(randomItem);/*from www .ja  v  a2  s .com*/
    }
    System.out.println(selectedRandomMessages);
}

From source file:Main.java

public static void main(String[] args) {
    List<Runnable> runnables = Arrays.asList(new Runnable[] { new Runnable() {
        public void run() {
            out.println("I'm the one");
        }/*w  w  w .  j  a va  2s .  c o m*/
    }, new Runnable() {
        public void run() {
            out.println("I'm the two");
        }
    }, new Runnable() {
        public void run() {
            out.println("I'm the three");
        }
    }, new Runnable() {
        public void run() {
            out.println("I'm the four");
        }
    }, });
    for (Runnable run : runnables) {
        new ExecuteTask(run).start();
    }

}

From source file:SaveVector.java

public static void main(String args[]) throws Exception {
    String data[] = { "Java", "Source", "and", "Support", "." };
    Vector v = new Vector(Arrays.asList(data));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(v);//from  w  ww.  j a  v a 2s . c o m
    o.close();
    ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray());
    ObjectInputStream oo = new ObjectInputStream(bb);
    Vector v2 = (Vector) oo.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    List wineMakers = Arrays.asList(new String[] { "U", "C" });
    List barFlies = Arrays.asList(new String[] { "U", "C", "L" });
    Collections.copy(barFlies, wineMakers);
    System.out.println(barFlies);
    Collections.copy(wineMakers, barFlies);
}