Example usage for java.util List add

List of usage examples for java.util List add

Introduction

In this page you can find the example usage for java.util List add.

Prototype

boolean add(E e);

Source Link

Document

Appends the specified element to the end of this list (optional operation).

Usage

From source file:Main.java

public static void main(String[] args) {
    // setup//from   ww  w .j  av  a2 s. c om
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way, the shortest way
    list.sort((a, b) -> a - b);
    System.out.println(list);

}

From source file:Card.java

public static void main(String args[]) {
    List<Card> lc = new ArrayList<Card>();
    lc.add(new Card(Card.Rank.SIX, Card.Suit.CLUBS));
    lc.add(new Card(Card.Rank.TEN, Card.Suit.CLUBS));
    lc.add(new Card(Card.Rank.SIX, Card.Suit.HEARTS));
    lc.add(new Card(Card.Rank.ACE, Card.Suit.HEARTS));

    System.out.println(lc);//from  w ww . j  av a2  s .  co  m
    Collections.sort(lc);
    System.out.println(lc);
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> people = new ArrayList<Person>();
    people.add(new Person("C", 21));
    people.add(new Person("T", 20));
    people.add(new Person("B", 35));
    people.add(new Person("A", 22));
    people.sort(Comparator.comparing(Person::getName));
    people.forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    // setup/*from  w  w w  .  java 2  s .  co m*/
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way
    Comparator<Integer> lambdaComparator = (Integer a, Integer b) -> b - a;
    list.sort(lambdaComparator);
    System.out.println(list);

}

From source file:ForeachDemo.java

public static void main(String args[]) {
    List<String> l = new ArrayList<String>();
    l.add("Toronto");
    l.add("Stockholm");
    iterate(l);//www  .  j  a  v a 2  s .  c  o  m
}

From source file:Main.java

public static void main(String args[]) {

    List<String[]> left = new ArrayList<String[]>();
    left.add(new String[] { "one", "two" });

    List<String[]> right = new ArrayList<String[]>();
    right.add(new String[] { "one", "two" });

    java.util.Iterator<String[]> leftIterator = left.iterator();
    java.util.Iterator<String[]> rightIterator = right.iterator();
    if (left.size() != right.size()) {
        System.out.println("not equal");
    }/* w w w  .jav a  2  s.c  o m*/
    while (leftIterator.hasNext()) {
        if (Arrays.equals(leftIterator.next(), rightIterator.next()))
            continue;
        else {
            System.out.print("not equal");
            break;
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    boolean result = task.call();
    if (result) {
        System.out.println("Compilation was successful");
    } else {// w  ww. j  a v a2s.c om
        System.out.println("Compilation failed");
    }
    fileManager.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    task.call();/*from   w ww  .j a  va2  s  .  com*/
    fileManager.close();
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

From source file:SafeCollectionIteration.java

public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Iterators");
    wordList.add("require");
    wordList.add("special");
    wordList.add("handling");

    synchronized (wordList) {
        Iterator iter = wordList.iterator();
        while (iter.hasNext()) {
            String s = (String) iter.next();
            System.out.println("found string: " + s + ", length=" + s.length());
        }//from  w  w  w.j  a v a2s.  c o m
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Phone> phones = new ArrayList<>();
    phones.add(new Phone("Galaxy", 12345));
    phones.add(new Phone("iPhone", 12345));

    JPanel panel = new JPanel(new GridLayout(0, 1));
    for (Phone phone : phones) {
        String html = "<html><body style='width:100px'>Phone: " + phone.getName() + "<br/>Model: "
                + phone.getModel() + "</body></html>";
        JLabel label = new JLabel(html);
        label.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(label);// w  w w .  j  av a2s  .com

    }
    JOptionPane.showMessageDialog(null, panel, "Phone List", JOptionPane.PLAIN_MESSAGE);
}