将特定元素从arraylist复制到java中的另一个arraylist [英] Copying specific element from arraylist to another arraylist in java
本文介绍了将特定元素从arraylist复制到java中的另一个arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试将具有特定值的一个元素(对象)从对象的ArrayList复制到另一个ArrayList
例如:
如何复制具有id的对象1从汽车到bestcars arraylist
trying to copy one element(object) with a specific value from ArrayList of objects to another ArrayList
for example:
How can i copy object that has the id of 1 from cars to bestcars arraylist
Car c1 = new Car("Toyota",1);
Car c2 = new Car("Mercedes",2);
ArrayList<Car>cars = new ArrayList<>();
ArrayList<Car>bestCars = new ArrayList<>();
cars.add(c1);
cars.add(c2);
我的尝试:
What I have tried:
import java.util.ArrayList;
public class Car {
public String name;
public int id;
public Car(String name, int id) {
this.name = name;
this.id = id;
}
public static void main(String[] args) {
Car c1 = new Car("Toyota",1);
Car c2 = new Car("Mercedes",2);
ArrayList<Car>cars = new ArrayList<>();
cars.add(c1);
cars.add(c2);
ArrayList<Car>bestCars = new ArrayList<>();
Car p = cars.get(1);
bestCars.add(p);
System.out.println("------ Cars -------");
for(Car test: cars){
if(test instanceof Car)
{ System.out.println("---------------------------------------");
System.out.println("\t\tCar Name:" + test.name +
"\nCar ID:"+
test.id );}
}
System.out.println("------ bestCars -------");
for(Car test: cars){
if(test instanceof Car)
{ System.out.println("---------------------------------------");
System.out.println("Car Name:" + test.name +
"\nCar ID:"+
test.id );}
}
}
}
推荐答案
你可以这样做:
You can do this:
Car c1 = new Car("Toyota",1);
Car c2 = new Car("Mercedes",2);
ArrayList<Car>cars = new ArrayList<>();
ArrayList<Car>bestCars = new ArrayList<>();
cars.add(c1);
cars.add(c2);
Car c = cars.stream()
.filter(x -> x.id == 1)
.findFirst()
.get();
bestCars.add(c);
System.out.println(bestCars.get(0).name); // Toyota
如何运作:
-
cars.stream()
创建一个Stream
来自ArrayList
。在这个对象上,你可以使用过滤器
函数。 -
.filter(x - > x.id = = 1)
将1
的所有Car
作为> id
(在这个例子中有一个这样的汽车)。x - > x.id == 1
是一个 lambda表达式(这是一个可以用这样的简单语句创建的函数,不必属于某个类)。表达式有一个参数x
并返回x.id == 1
,一个布尔值。 -
.findFirst()
返回可选< Car> [ ^ ] -可选
是一个容器,可能包含也可能不包含非空对象。如果filter
结果是过滤掉所有对象而没有返回单个对象,则Optional将为空(但在此示例中不是这种情况)。 -
.get()
从Optional
Car >。请注意,如果Optional中没有值,则会抛出NoSuchElementException
。在此示例中,很明显存在匹配值。在您不是100%确定具有值的Optional的应用程序中,请使用isPresent
。 cars.stream()
creates aStream
from theArrayList
. On this object, there is thefilter
function you can use..filter(x -> x.id == 1)
will take allCar
s that have1
asid
(there is one Car like that, in this example).x -> x.id == 1
is a lambda expression (which is a function that can be created with a simple statement like this, without having to belong to a class). The expression has one parameterx
and returnsx.id == 1
, a Boolean..findFirst()
returns an Optional<Car>[^] - anOptional
is a container which may, or may not, contain a non-null object. The Optional would be empty iffilter
turned out to filter out all objects and not returning a single one (but that is not the case in this example)..get()
extracts theCar
from theOptional
. Note that, if no value is present in the Optional, this will throw aNoSuchElementException
. In this example, it's clear that there is a matching value. In applications where you are not 100% certain of an Optional to have a value, useisPresent
.
How this works:
这篇关于将特定元素从arraylist复制到java中的另一个arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文