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[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {/*from ww w .j a v  a  2s  .  c  o m*/
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {
        System.out.println(e.getMessage());
    }

}

From source file:MainClass.java

public static void main(String args[]) {
    String str[] = { "B", "H", "L", "M", "I", "N", "R" };
    // Convert to list
    List list = new ArrayList(Arrays.asList(str));
    // Ensure list sorted
    Collections.sort(list);//  www.j  a va  2  s.  co 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);
    // Insert
    int newIndex = -index - 1;
    list.add(newIndex, "J");
    System.out.println("With J added: [length: " + list.size() + "]");
    System.out.println(list);
}

From source file:Main.java

public static void main(String[] args) {
    String[] k1 = { "0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1" };
    Arrays.sort(k1, new VersionNumberComparator());
    System.out.println(Arrays.asList(k1));
}

From source file:Main.java

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

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//from  ww w. ja v  a2s .  c  om
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {

    }

    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] arr = { 1, 2, 3, 1, 4, 4, 1, 5 };
    System.out.println(Arrays.toString(arr));
    List<Integer> l = Arrays.asList(arr);

    System.out.println(l);/*from w  ww . j av a  2  s .c o  m*/

    Set<Integer> set = new HashSet<Integer>();
    for (int j = 0; j < l.size(); j++) {
        if (Collections.frequency(l, l.get(j)) > 1) {
            set.add(l.get(j));
        }
    }
    System.out.println("dups are:" + set);
}

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("Shortest to longest: ");
    Arrays.sort(array, (s1, s2) -> s1.length() - s2.length());
    Arrays.asList(array).forEach(System.out::println);

    System.out.println("\nLongest to shortest: ");
    Arrays.sort(array, (s1, s2) -> (s2.length() - s1.length()));
    Arrays.asList(array).forEach(System.out::println);

    System.out.println("\nAlphabetically by the first character only: ");
    Arrays.sort(array, (s1, s2) -> s1.charAt(0) - s2.charAt(0));
    Arrays.asList(array).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] argv) {
    Vector rowData = new Vector();
    for (int i = 0; i < 1; i++) {
        Vector colData = new Vector(Arrays.asList("qq"));
        rowData.add(colData);//  ww w  . j  a  v  a 2 s .com
    }

    String[] columnNames = { "a" };

    Vector columnNamesV = new Vector(Arrays.asList(columnNames));

    JTable table = new JTable(rowData, columnNamesV);
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String init[] = { "One", "Two", "Three", "One", "Two", "Three" };

    // create two lists
    List<String> list1 = new ArrayList<String>(Arrays.asList(init));
    List<String> list2 = new ArrayList<String>(Arrays.asList(init));

    // remove from list1
    list1.remove("One");
    System.out.println("List1 value: " + list1);

    // remove from list2 using singleton
    list2.removeAll(Collections.singleton("One"));
    System.out.println("The SingletonList is :" + list2);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
}

From source file:Main.java

public static void main(String[] args) {

    String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";

    List<String> list = Arrays.asList(text.split(" "));

    Set<String> uniqueWords = new HashSet<String>(list);
    for (String word : uniqueWords) {
        System.out.println(word + ": " + Collections.frequency(list, word));
    }//  w  w w . ja v a 2  s .  com
}