Example usage for java.util ArrayList ArrayList

List of usage examples for java.util ArrayList ArrayList

Introduction

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

Prototype

public ArrayList() 

Source Link

Document

Constructs an empty list with an initial capacity of ten.

Usage

From source file:Main.java

public static void main(String[] argv) {
    List<Person> persons = new ArrayList<>();
    persons.add(new Person("Joe", 12));
    persons.add(new Person("Jim", 34));
    persons.add(new Person("John", 23));

    List<Person> list = persons.stream().filter(p -> p.getAge() > 18).collect(Collectors.toList());
    list.forEach(p -> System.out.println(p.getFirstName()));

}

From source file:Main.java

public static void main(String[] args) {
    List<String> fruits = new ArrayList<String>();
    fruits.add("A");
    fruits.add("");
    fruits.add("C");
    fruits.add("D");
    fruits.add("A");

    Collator collator = Collator.getInstance(Locale.US);

    Collections.sort(fruits, collator);

    for (int i = 0; i < fruits.size(); i++) {
        String fruit = fruits.get(i);

        System.out.println("Fruit = " + fruit);
    }/*from  ww w  . jav a2s .  c o m*/
}

From source file:Main.java

public static void main(String[] args) {
    // create array list
    List<Character> list = new ArrayList<Character>();

    // populate the list
    list.add('X');
    list.add('Y');

    System.out.println("Initial list: " + list);

    Collection<Character> immutablelist = Collections.unmodifiableCollection(list);

    // try to modify the list
    immutablelist.add('Z');
}

From source file:MainClass.java

public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("\u00e4pple");
    list.add("banan");
    list.add("p\u00e4ron");
    list.add("orange");

    // Obtain a Swedish collator
    Collator collate = Collator.getInstance(new Locale("sv", ""));
    Collections.sort(list, collate);

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }/*from www. ja va  2s.co m*/
}

From source file:Main.java

public static void main(String[] args) {

    // create a new list of arguments for our process
    List<String> list = new ArrayList<String>();
    list.add("notepad.exe");
    list.add("test.txt");

    // create the process builder
    ProcessBuilder pb = new ProcessBuilder(list);
    try {//w  w  w . j  a v a  2 s .  co m
        // start the subprocess
        System.out.println("Starting the process..");
        pb.start();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("Peter", 65, Person.Gender.MALE));
    persons.add(new Person("Kevin", 5, Person.Gender.MALE));
    persons.add(new Person("Mandy", 19, Person.Gender.FEMALE));

    // Method Reference
    Collection<Person> eighteen = list(persons, (Person p) -> p.getAge() >= 18);
    System.out.println(eighteen);

    // Method Reference static
    Collection<Person> malePersons = list(persons, Person::isMale);
    System.out.println(malePersons);

    // Method Reference non static
    Person a = new Person("a", 1, Person.Gender.MALE);
    Collection<Person> femalePersons = list(persons, a::isFemale);
    System.out.println(femalePersons);
}

From source file:Main.java

public static void main(String... args) {
    List<Apple> inventory = new ArrayList<>();
    inventory.addAll(Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")));

    inventory.sort(comparing(Apple::getWeight));
    System.out.println(inventory);
}

From source file:Main.java

public static void main(String[] argv) {
    List collection = new ArrayList();

    // For a set or list
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object element = it.next();
    }/*from   w w  w .  j a  v  a 2 s  . co  m*/
    Map map = new HashMap();
    // For keys of a map
    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
        Object key = it.next();
    }

    // For values of a map
    for (Iterator it = map.values().iterator(); it.hasNext();) {
        Object value = it.next();
    }

    // For both the keys and values of a map
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
    }
}

From source file:Main.java

public static void main(String args[]) {
    // create two array list objects       
    List<String> arrlistsrc = new ArrayList<String>();
    List<String> arrlisttarget = new ArrayList<String>();

    // populate two lists
    arrlistsrc.add("A");
    arrlistsrc.add("from java2s.com");
    arrlistsrc.add("C");
    arrlistsrc.add("D");
    arrlistsrc.add("E");

    arrlisttarget.add("C");
    arrlisttarget.add("D");
    arrlisttarget.add("E");

    // check starting position of the last occurrence
    int index = Collections.lastIndexOfSubList(arrlistsrc, arrlisttarget);

    System.out.println("Starting position is: " + index);
}

From source file:Main.java

public static void main(String args[]) {
    // create two array list objects       
    List<String> arrlistsrc = new ArrayList<String>();
    List<String> arrlisttarget = new ArrayList<String>();

    // populate two lists
    arrlistsrc.add("A");
    arrlistsrc.add("from java2s.com");
    arrlistsrc.add("C");
    arrlistsrc.add("D");
    arrlistsrc.add("E");

    arrlisttarget.add("C");
    arrlisttarget.add("D");
    arrlisttarget.add("E");

    // check target list in source list
    int index = Collections.indexOfSubList(arrlistsrc, arrlisttarget);

    System.out.println("Target list starts at index: " + index);
}