Slinidy
Slinidy

Reputation: 387

How to create arraylist without one element from another arraylist

I needed to create an arraylist without an element of another arraylist, but I need this new arraylist to keep updating. For example, an element of the old arraylist is removed, also remove in the new one.

But I did not want to remove the element of the two arraylist, only the old one, so as not to have much code

(My method "showPeople" is updated every 1 second)

My code:

ArrayList<Person> personList = new ArrayList<>();

private void method(){
   personList.add(new People("Name"))
}

private void showPeople(){
    ArrayList<Person> newPersonList = 
              new ArrayList<>(personList.stream()
                                        .filter(person -> !person.getName().equals("Test"))
                                        .collect(Collectors.toList()))

    for (int i = 0; i < newPersonList.size(); i++){
        gui.show(newPersonList.get(i).getName());
    }
}

The problem is that when I create the new arraylist and remove an item from the old one, the new one does not update

Upvotes: 1

Views: 1498

Answers (4)

Adam Wise
Adam Wise

Reputation: 2290

Here's a handy method:

private static <T> List<T> CopyListWithoutItem(List<T> listToCopy, T itemToNotCopy) {
    return listToCopy.stream().filter(item -> !item.equals(itemToNotCopy)).collect(Collectors.toList());
}

Upvotes: 0

Jan Nielsen
Jan Nielsen

Reputation: 11829

You're making multiple copies of your list; instead, do something like:

List<Person> filterPeople(List<Person> people, @NotNull String name) {
    return people.stream()
        .filter(person -> !name.equals(person.getName()))
        .collect(Collectors.toList());
}

If you're uncomfortable with the lack of guarantees on the the shape of the List, you can be explicit:

        .collect(Collectors.toCollection(ArrayList::new));

It's still unclear what you're asking, however. I suggest you provide a minimal, complete, and verifiable example.

Upvotes: 2

Andy Turner
Andy Turner

Reputation: 140494

If you want the list without the element to keep updating, you can create a view of the list by extending AbstractList.

The API documentation contains instructions as to the methods you would need to override. If you don't want the list to be modifiable through the view, all you need to do is to override the get and size methods:

class ListView extends AbstractList<String> {
  private final List<String> delegate;  // Initialize in constructor.

  public T get(int i) {
    int pos = delegate.indexOf("Test");
    if (pos < 0 || i < pos) return delegate.get(i);
    return delegate.get(i + 1);
  }

  public int size() {
    return delegate.size() - (delegate.contains("Test") ? 1 : 0);
  }
}

This will repeatedly search for the "Test" element, because there is no way for the view to know if the delegate list has been updated underneath it.

Upvotes: 0

Reddi
Reddi

Reputation: 743

You can use that: List<String> elements = list.stream().distinct().collect(Collectors.toList()); That will remove duplicates.

Upvotes: -1

Related Questions