How do I convert collection to ArrayList?

package org.kodejava.example.util;

import java.util.ArrayList;
import java.util.LinkedList;

public class CollectionToArrayList {
    public static void main(String[] args) {
        // We create LinkedList collection type at put some values
        // in it. Here we put A, B, C and D letter into it.
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.push("A");
        linkedList.push("B");
        linkedList.push("C");
        linkedList.push("D");

        // Let say you want to convert it to other type of collection,
        // for instance here we convert it into ArrayList. To do it
        // we can pass the collection created above as a parameter to
        // ArrayList constructor.
        ArrayList<String> arrayList = new ArrayList<String>(linkedList);

        // Now we have converted the collection into ArrayList and
        // printed what is inside.
        for (String s : arrayList) {
            System.out.println("s = " + s);
        }
    }
}

How do I convert a collection object into an array?

To convert collection-based object into array we can use toArray() or toArray(T[] a) method provided by the implementation of Collection interface such as java.util.ArrayList.

package org.kodejava.example.util;

import java.util.List;
import java.util.ArrayList;

public class CollectionToArrayExample {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        words.add("Kode");
        words.add("Java");
        words.add("-");
        words.add("Learn");
        words.add("Java");
        words.add("by");
        words.add("Examples");

        String[] array = words.toArray(new String[words.size()]);
        for (String word : array) {
            System.out.println(word);
        }
    }
}

Our sample code result is shown below:

Kode
Java
-
Learn
Java
by
Examples

How do I know if an ArrayList contains a specified item?

In this example you are going to learn how to find out if a List or ArrayList object has a specified element. To check if an ArrayList object contains a specified element we can use the contains() method. This method returns a boolean true when the specified element is found in the ArrayList, if not it will returns false.

package org.kodejava.example.util;

import java.util.ArrayList;
import java.util.List;

public class ArrayListContainsExample {
    private List<String> list = new ArrayList<>();

    public ArrayListContainsExample() {
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");
    }

    public static void main(String[] args) {
        ArrayListContainsExample demo = new ArrayListContainsExample();

        // Check to see if the list contains "Item 1".
        String itemToFind = "Item 1";
        demo.findItemInTheList(itemToFind);

        // Check to see if the list contains "Item 20".
        itemToFind = "Item 20";
        demo.findItemInTheList(itemToFind);
    }

    private void findItemInTheList(String itemToFind) {
        if (list.contains(itemToFind)) {
            System.out.println(itemToFind + " was found in the list");
        } else {
            System.out.println(itemToFind + " was not found in the list");
        }
    }
}

The output of the code snippet above are:

Item 1 was found in the list
Item 20 was not found in the list