

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you copy an element from one list to another in Java?
An element can be copied to another List using streams easily.
Use Streams to copy selective elements.
List<String> copyOfList = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
Example
Following is the example to copy only even numbers from a list −
package com.tutorialspoint; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = Arrays.asList(11, 22, 3, 48, 57); System.out.println("Source: " + list); List<Integer> evenNumberList = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("Even numbers in the list: " + evenNumberList); } }
Output
This will produce the following result −
Source: [11, 22, 3, 48, 57] Even numbers in the list: [22, 48]
- Related Questions & Answers
- Java Program to copy value from one list to another list
- How do you copy a list in Java?
- How do you add an element to a list in Java?
- How can we copy one array from another in Java
- How to copy a list to another list in Java?
- How do I insert all elements from one list into another in Java?
- Copy all the elements from one set to another in Java
- How to move an array element from one array position to another in Java?
- Copy values from one array to another in Numpy
- How do you make a shallow copy of a list in Java?
- How to copy rows from one table to another in MySQL?
- How to copy files from one server to another using Python?
- How to copy files from one folder to another using Python?
- How to copy a table from one MySQL database to another?
- How do you create an empty list in Java?
Advertisements