Lynn
Lynn

Reputation: 131

How to remove one instance of an object using Java ArrayList

If I have two ArrayList list and toremove , and I want to delete all the objects on list that are equals to objects on toremove. But only once (one instance of the object). How can I achieve that in Java ? Do I have to manually loop through all toremove objects and delete it if it in list?

I have tried .removeAll(toremove); but it deletes every instance if objects.

for example, if i have book1 book1 book2 book3, and my toRemove list is book1 book2. the output will be book1 book3.


    private List<Book>  removeList(List<Book> initial,List<Book> toremove) {
        List<Book> list=initial;

        for (Book book : List) {

        }

        return List;
    }

Upvotes: 1

Views: 1672

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79425

You can do it as follows:

private static List<Book> removeList(List<Book> initial, List<Book> toRemove) {
    List<Book> toBeReturned = new ArrayList<Book>(initial);
    List<Book> deleted = new ArrayList<Book>();
    Iterator<Book> itr = toBeReturned.iterator();
    while (itr.hasNext()) {
        Book a = itr.next();
        for (Book b : toRemove) {
            if (b.equals(a) && !deleted.contains(b)) {
                deleted.add(b);
                itr.remove();
            }
        }
    }
    return toBeReturned;
}

Demo:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

class Book {
    int id;

    public Book(int id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public boolean equals(Object obj) {
        Book other = (Book) obj;
        return this.id == other.id;
    }

    @Override
    public String toString() {
        return "Book [id=" + id + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        List<Book> initial = new ArrayList<Book>();
        initial.add(new Book(1));
        initial.add(new Book(1));
        initial.add(new Book(1));
        initial.add(new Book(2));
        initial.add(new Book(2));
        initial.add(new Book(3));

        List<Book> toRemove = new ArrayList<Book>();

        toRemove.add(new Book(1));
        toRemove.add(new Book(2));

        List<Book> list = removeList(initial, toRemove);

        // Display list
        list.stream().forEach(System.out::println);
    }

    private static List<Book> removeList(List<Book> initial, List<Book> toRemove) {
        List<Book> toBeReturned = new ArrayList<Book>(initial);
        List<Book> deleted = new ArrayList<Book>();
        Iterator<Book> itr = toBeReturned.iterator();
        while (itr.hasNext()) {
            Book a = itr.next();
            for (Book b : toRemove) {
                if (b.equals(a) && !deleted.contains(b)) {
                    deleted.add(b);
                    itr.remove();
                }
            }
        }
        return toBeReturned;
    }
}

Output:

Book [id=1]
Book [id=1]
Book [id=2]
Book [id=3]

Upvotes: 1

0xh3xa
0xh3xa

Reputation: 4857

Override equals() in book class

static class Book {
        int id;
        String title;

        public Book(int id, String title) {
            this.id = id;
            this.title = title;
        }

        @Override
        public boolean equals(Object obj) {
            Book other = (Book) obj;
            return this.title.equals(other.title);
        }

        @Override
        public String toString() {
            return id + ": " + title;
        }
    }

And remove the first occurrence of the object in the list using remove()


    public static void main(String[] a) {
        Book b1 = new Book(1, "book1");
        Book b2 = new Book(1, "book1");
        Book b3 = new Book(3, "book2");
        Book b4 = new Book(4, "book3");
        List<Book> list = new ArrayList<>();
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        removeFirstOccurance(list, Arrays.asList(b1, b3));
        System.out.println(list);
    }

    private static void removeFirstOccurance(List<Book> list, List<Book> toRemove) {
        for (Book b : toRemove) {
            list.remove(b);
        }
    }

, output

[1: book1, 4: book3]

Upvotes: 1

user
user

Reputation: 7604

Just use this:

for (Book b : toremove) {
  int index = list.indexOf(b);
  if (index != -1) list.remove(index);
}

This finds the index of your object, and then removes it using that index as long as that book is present in list.

Of course, as sc0der said, you'll have to override the equals method for the Book class, but I suspect you've already done that, since removeAll does work in that it removes all objects.

Example input:

list = new ArrayList<>();
list.add(book1);
list.add(book1);
list.add(book2);
list.add(book1);
list.add(book3);
toremove = Arrays.asList(new Book[]{book1, book2});

Example output:

list = [book1, book1, book3]

Upvotes: 2

Related Questions