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

public static void main(String[] args) {
    SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
    System.out.println(sortedSet);
    Object low = sortedSet.first(), high = sortedSet.last();
    System.out.println(low);//w  w  w.j  av a2 s  .  c  o  m
    System.out.println(high);
    Iterator it = sortedSet.iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedSet.subSet(low, high));
    System.out.println(sortedSet.headSet(high));
    System.out.println(sortedSet.tailSet(low));
}

From source file:AlphabeticSorting.java

public static void main(String[] args) {
    String[] sa = new String[] { "a", "c", "b" };

    System.out.println("Before sorting: " + Arrays.asList(sa));
    Arrays.sort(sa, new AlphabeticComparator());
    System.out.println("After sorting: " + Arrays.asList(sa));
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);/*from  w  w w .  j av a2  s . c o  m*/
    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);
    oos.close();
    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set set3 = (Set) ois.readObject();
    ois.close();
    System.out.println(set3);
}

From source file:CompTypeComparator.java

public static void main(String[] args) {
    Integer[] a = new Integer[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = i;//from   ww  w.j av a2 s .com
    }

    System.out.println("before sorting, a = " + Arrays.asList(a));
    Arrays.sort(a, new CompTypeComparator());
    System.out.println("after sorting, a = " + Arrays.asList(a));
}

From source file:ReplacingStringTokenizer.java

public static void main(String[] args) {
    String input = "But I'm not dead yet! I feel happy!";
    StringTokenizer stoke = new StringTokenizer(input);
    while (stoke.hasMoreElements())
        System.out.println(stoke.nextToken());
    System.out.println(Arrays.asList(input.split(" ")));
}

From source file:Utilities.java

public static void main(String[] args) {
    List list = Arrays.asList("one Two three Four five six one".split(" "));
    System.out.println(list);/*from  w  w  w  . j av  a2  s.  c om*/
    System.out.println("max: " + Collections.max(list));
    System.out.println("min: " + Collections.min(list));
    AlphabeticComparator comp = new AlphabeticComparator();
    System.out.println("max w/ comparator: " + Collections.max(list, comp));
    System.out.println("min w/ comparator: " + Collections.min(list, comp));
    List sublist = Arrays.asList("Four five six".split(" "));
    System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
    System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
    Collections.replaceAll(list, "one", "Yo");
    System.out.println("replaceAll: " + list);
    Collections.reverse(list);
    System.out.println("reverse: " + list);
    Collections.rotate(list, 3);
    System.out.println("rotate: " + list);
    List source = Arrays.asList("in the matrix".split(" "));
    Collections.copy(list, source);
    System.out.println("copy: " + list);
    Collections.swap(list, 0, list.size() - 1);
    System.out.println("swap: " + list);
    Collections.fill(list, "pop");
    System.out.println("fill: " + list);
    List dups = Collections.nCopies(3, "snap");
    System.out.println("dups: " + dups);
    // Getting an old-style Enumeration:
    Enumeration e = Collections.enumeration(dups);
    Vector v = new Vector();
    while (e.hasMoreElements())
        v.addElement(e.nextElement());
    // Converting an old-style Vector
    // to a List via an Enumeration:
    ArrayList arrayList = Collections.list(v.elements());
    System.out.println("arrayList: " + arrayList);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;/*from   w  w w . ja  v a2 s .  co m*/
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<Vector<String>> rowData = new Vector<Vector<String>>();
    Vector<String> columnName = new Vector<String>(Arrays.asList("Column 1"));
    for (int i = 0; i < 2000; i++) {
        rowData.add(new Vector<String>(Arrays.asList(Integer.toString(i))));
    }/* ww w .j  a v  a2 s .c  om*/
    JTable table = new JTable(rowData, columnName);
    JScrollPane scrollPane = new JScrollPane(table);
    JScrollBar vertical = scrollPane.getVerticalScrollBar();
    vertical.setPreferredSize(new Dimension(0, 0));
    f.add(scrollPane);
    f.pack();
    f.setVisible(true);
    JViewport view = scrollPane.getViewport();
    Component[] components = view.getComponents();
    for (int i1 = 0; i1 < components.length; i1++) {
        if (components[i1] instanceof JTable) {
            System.out.println("got");
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;// w w  w .ja v a2s  .com
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:Main.java

public static void main(String[] args) {
    String[] array = { "Java", "C#", "Scala", "Basic", "C++", "Ruby", "Pyton", "Perl", "Haskell", "Jet" };
    System.out.println("\nStrings that contain ?e? first, everything else second: ");
    Arrays.sort(array, (s1, s2) -> {
        if (s1.contains("e") && s2.contains("e")) {
            return 0;
        } else if (s1.contains("e")) {
            return -1;
        } else {//from w ww .  j  a v a 2s .c  om
            return 1;
        }
    });
    Arrays.asList(array).forEach(System.out::println);
}