1

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

3
  • Couldn't you just keep one arraylist, and filter out the object you don't want everytime you need to grab the entries? You should explain the reasoning for this, I assume there's definitely a better solution to your problem. Commented Dec 14, 2018 at 15:51
  • I already did this and it did not work Commented Dec 14, 2018 at 15:58
  • What is the order of the methods you invoke? Because here it looks like every time you invoke showPeople, the current elements of personList are used. Thus, if you remove or add some items from it, when you later invoke showPeople, such a change should also appear. Btw, instead of collecting the stream in a newPersonList, you can also replace .collect with .forEach(p -> gui.show(p.getName())) Commented Dec 14, 2018 at 16:02

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to mention but my list is inside a HashMap <T, MyList>, even removing the element from the list inside the hashmap, it did not seem like it creates a new arraylist that I have to delete the element from it too. Only updates if I exit and enter the GUI, even updating every 1 second
@Slinidy -- help us help you by providing an appropriate minimal, complete and verifiable example.
0

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.

1 Comment

I need filter specific element in the case: delegate.indexOf("Test");
0

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.