Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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:

Java
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  );}
        }



    }

}
Posted
Updated 3-May-18 7:52am
v3
Comments
Richard MacCutchan 3-May-18 13:48pm    
What is the question?
HaniAgel 3-May-18 13:50pm    
sorry ,, question updated

1 solution

You can do this:
Java
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

How this works:

  • cars.stream() creates a Stream from the ArrayList. On this object, there is the filter function you can use.
  • .filter(x -> x.id == 1) will take all Cars that have 1 as id (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 parameter x and returns x.id == 1, a Boolean.
  • .findFirst() returns an Optional<Car>[^] - an Optional is a container which may, or may not, contain a non-null object. The Optional would be empty if filter turned out to filter out all objects and not returning a single one (but that is not the case in this example).
  • .get() extracts the Car from the Optional. Note that, if no value is present in the Optional, this will throw a NoSuchElementException. 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, use isPresent.
 
Share this answer
 
Comments
HaniAgel 3-May-18 15:01pm    
worked great Thanks, you are a treasure of information :)
Maciej Los 4-May-18 4:36am    
Nice answer ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900