
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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 to copy a list to another list in Java?
- How can we copy one array from another in Java
- How do I insert all elements from one list into another in Java?
- How to move an array element from one array position to another in Java?
- Copy all the elements from one set to another in Java
- How do you make a shallow copy of a list in Java?
- How do you check if an element is present in a list in Java?
- How do you get the index of an element in a list in Java?
- How to write a program to copy characters from one file to another in Java?
- How to copy rows from one table to another in MySQL?
- Copy values from one array to another in Numpy
- How do you create an empty list in Java?

Advertisements