DEV Community

Edem Agbenyo
Edem Agbenyo

Posted on

100 Days of Java: Day 4 List manipulation

Welcome to Day 4 of the 100 Days of Code challenge! Today, we have an interesting Java code snippet that demonstrates how to remove items from a list using different ways. Let's break down the code and understand how it works.

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Day004 {

    public static void main(String[] args){
        List<Person> barcelonaTeam = new ArrayList<>();
        barcelonaTeam.add(new Person("001","Messi",LocalDate.of(1990,9,21)));
        barcelonaTeam.add(new Person("002","Pique",LocalDate.of(1989,8,1)));

        barcelonaTeam.add(new Person("003","Jordi",LocalDate.of(1990,9,21)));

        barcelonaTeam.add(new Person("004","Busquets",LocalDate.of(1990,9,21)));

        removeItemUsingEquals(barcelonaTeam);

        removeItemUsingAnSpecificFilter(barcelonaTeam);

        System.out.println(barcelonaTeam);

    }

    private static void removeItemUsingAnSpecificFilter(List<Person> team){
        team.removeIf(person -> "Jordi".equals(person.getName()));
    }
    private static void removeItemUsingEquals(List<Person> barcelonaTeam) {
        var messi = new Person("1", "Messi", LocalDate.of(1940, 10, 9));
        barcelonaTeam.remove(lennon);
    }

    static class Person{
        private String id;
        private String name;
        private LocalDate dateOfBrith;

        public Person(String id, String name, LocalDate dateOfBrith) {
            this.id = id;
            this.name = name;
            this.dateOfBrith = dateOfBrith;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public LocalDate getDateOfBrith() {
            return dateOfBrith;
        }

        @Override
        public boolean equals(Object o){
            if(this==o) {
                return true;
            }
            if(o==null || getClass() != o.getClass()) return false;
            var person = (Person)o;
            return Objects.equals(id, person.id) && Objects.equals(name, person.name) && Objects.equals(dateOfBrith, person.dateOfBrith);
        }

        @Override
        public int hashCode(){
            return Objects.hash(id,name, dateOfBrith);
        }
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

To start, we import necessary classes and define a Person class within the Day004 class. The Person class represents an individual with properties such as id, name, and dateOfBirth. It also overrides methods like equals(), hashCode(), and toString().

Inside the main method, we create a List called barcelonaTeam(yes I am a Barca Fan, sorry this is an old team) to store instances of the Person class.

We populate the barcelonaTeam list with four Person objects representing the members of the Barcelona Team 2019-2020. Each Person object is created using the new Person(...) syntax and initialized with specific values for id, name, and dateOfBirth.

Next, we call two methods to remove items from the barcelonaTeam list: removeItemUsingEquals(barcelonaTeam) and removeItemUsingAnSpecificFilter(barcelonaTeam).

In the removeItemUsingEquals method, we create a new Person object named messi with the same values as the first member of the Barca team. We then use the remove() method of the list to remove the messi object from the list. This works because we have overridden the equals() method in the Person class to compare objects based on their properties.

In the removeItemUsingAnSpecificFilter method, we use the removeIf() method of the list with a lambda expression as a filter. The lambda expression checks if the person's name is equal to "Jordi" and removes the matching person from the list.

Finally, we print the barcelonaTeam list using System.out.println() to see the resulting list after the removal operations.

In summary, this code snippet demonstrates two different approaches to remove items from a list in Java. The first approach uses the remove() method by creating an object that matches the item to be removed. The second approach utilizes the removeIf() method with a specific filter condition to remove items that meet the criteria.

That's it for Day 4! Stay tuned for the next code snippet and explanation. Happy coding!

Top comments (0)