Open In App

How to add selected items from a collection to an ArrayList in Java?

Last Updated : 02 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Collection with some values, the task is to add selected the items of this Collection to an ArrayList in Java.

Examples:

Input: Collection = [1, 2, 3], condition = (item != 2)
Output: ArrayList = [1, 3]

Input: Collection = [GFG, Geek, GeeksForGeeks], condition = (item != GFG)
Output: ArrayList = [Geek, GeeksForGeeks]

Approach:

  • Get the Collection whose selected items are to be added into the ArrayList
  • Create an ArrayList
  • Add selected items of Collection into this ArrayList using Stream
  • Generate the stream with all the items of the collection using stream() method
  • Select the required items from the Stream using filter() method
  • Collect the selected items of the stream as an ArrayList using forEachOrdered() method
  • ArrayList with selected items of Collections have been created.

Below is the implementation of the above approach:




// Java program to add selected items
// from a collection to an ArrayList
  
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to add selected items
    // from a collection to an ArrayList
    public static <T> ArrayList<T>
    createArrayList(List<T> collection, T N)
    {
  
        // Create an ArrayList
        ArrayList<T> list = new ArrayList<T>();
  
        // Add selected items of Collection
        // into this ArrayList
        // Here select items if
        // they are not equal to N
        collection.stream()
            .filter(item -> !item.equals(N))
            .forEachOrdered(list::add);
  
        return list;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        List<Integer> collection1
            = Arrays.asList(1, 2, 3);
        System.out.println(
            "ArrayList with selected "
            + "elements of collection "
            + collection1 + ": "
            + createArrayList(collection1, 2));
  
        List<String> collection2
            = Arrays.asList("GFG",
                            "Geeks",
                            "GeeksForGeeks");
        System.out.println(
            "ArrayList with selected "
            + "elements of collection "
            + collection2 + ": "
            + createArrayList(collection2, "GFG"));
    }
}


Output:

ArrayList with selected elements of collection [1, 2, 3]: [1, 3]
ArrayList with selected elements of collection [GFG, Geeks, GeeksForGeeks]: [Geeks, GeeksForGeeks]



Similar Reads

How to Add All Items From a Collection to an ArrayList in Java?
Given a Collection with some values, the task is to add all the items of this Collection to an ArrayList in Java. Illustrations: Input: Collection = [1, 2, 3] Output: ArrayList = [1, 2, 3]Input: Collection = [GFG, Geek, GeeksForGeeks] Output: ArrayList = [GFG, Geek, GeeksForGeeks] Approach: Get the Collection whose items are to be added into the Ar
2 min read
Java Program to Add the Data from the Specified Collection in the Current Collection
The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a true value if the adding of the collection is succ
3 min read
How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java Code // Java pr
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
How to Insert all the Collection Elements to the Specified Position in Java ArrayList?
The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this method accepts two arguments the first argument is the
3 min read
Insert all Elements of Other Collection to Specified Index of Java ArrayList
ArrayList is part of the collection framework. It is a List and implements the java.util.list interface. ArrayList is a better alternative to Arrays, especially if you are not sure about the array size. Unlike array which has a fixed size, ArrayList can grow in size when needed. Internally ArrayList also uses arrays to store data. When it reaches t
2 min read
How to Get Substring Items Within Arraylist in Java?
In Java, ArrayList is the pre-defined class of the Java collection framework, and it is part of java.util package. ArrayList can be used to add or remove an element dynamically in a Java program. It can be dynamically based on the elements added or removed from the ArrayList. In this article, we will be discussing how to get substring items within
2 min read
Java Program to Get the Size of Collection and Verify that Collection is Empty
The size() and isEmpty() of java.util.Collection interface is used to check the size of collections and if the Collection is empty or not. isEmpty() method does not take any parameter and does not return any value. Example: Input:[99, 54, 112, 184, 2] Output:size = 5 and Collection is not empty Input:[] Output: size = 0 and Collection is emptysize(
2 min read
Difference Between Collection.stream().forEach() and Collection.forEach() in Java
Collection.forEach() and Collection.stream().forEach() are used for iterating over the collections, there is no such major difference between the two, as both of them give the same result, though there are some differences in their internal working. Collection.stream().forEach() is basically used for iteration in a group of objects by converting a
3 min read
Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o: The element to be appended to this list. Exception: NA // Java code to illustrate add(Object o) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static vo
2 min read
Practice Tags :