Java 8 lambda get and remove element from list

JavaLambdaJava 8Java Stream

Java Problem Overview


Given a list of elements, I want to get the element with a given property and remove it from the list. The best solution I found is:

ProducerDTO p = producersProcedureActive
				.stream()
				.filter(producer -> producer.getPod().equals(pod))
				.findFirst()
				.get();
producersProcedureActive.remove(p);

Is it possible to combine get and remove in a lambda expression?

Java Solutions


Solution 1 - Java

To Remove element from the list

objectA.removeIf(x -> conditions);

eg:

objectA.removeIf(x -> blockedWorkerIds.contains(x));

List<String> str1 = new ArrayList<String>();
str1.add("A");
str1.add("B");
str1.add("C");
str1.add("D");

List<String> str2 = new ArrayList<String>();
str2.add("D");
str2.add("E");

str1.removeIf(x -> str2.contains(x)); 
	
str1.forEach(System.out::println);

OUTPUT: A B C

Solution 2 - Java

Although the thread is quite old, still thought to provide solution - using Java8.

Make the use of removeIf function. Time complexity is O(n)

producersProcedureActive.removeIf(producer -> producer.getPod().equals(pod));

API reference: removeIf docs

Assumption: producersProcedureActive is a List

NOTE: With this approach you won't be able to get the hold of the deleted item.

Solution 3 - Java

Consider using vanilla java iterators to perform the task:

public static <T> T findAndRemoveFirst(Iterable<? extends T> collection, Predicate<? super T> test) {
	T value = null;
	for (Iterator<? extends T> it = collection.iterator(); it.hasNext();)
		if (test.test(value = it.next())) {
			it.remove();
			return value;
		}
	return null;
}

Advantages:

  1. It is plain and obvious.
  2. It traverses only once and only up to the matching element.
  3. You can do it on any Iterable even without stream() support (at least those implementing remove() on their iterator).

Disadvantages:

  1. You cannot do it in place as a single expression (auxiliary method or variable required)

As for the

> Is it possible to combine get and remove in a lambda expression?

other answers clearly show that it is possible, but you should be aware of

  1. Search and removal may traverse the list twice
  2. ConcurrentModificationException may be thrown when removing element from the list being iterated

Solution 4 - Java

The direct solution would be to invoke ifPresent(consumer) on the Optional returned by findFirst(). This consumer will be invoked when the optional is not empty. The benefit also is that it won't throw an exception if the find operation returned an empty optional, like your current code would do; instead, nothing will happen.

If you want to return the removed value, you can map the Optional to the result of calling remove:

producersProcedureActive.stream()
						.filter(producer -> producer.getPod().equals(pod))
						.findFirst()
						.map(p -> {
                            producersProcedureActive.remove(p);
                            return p;
                        });

But note that the remove(Object) operation will again traverse the list to find the element to remove. If you have a list with random access, like an ArrayList, it would be better to make a Stream over the indexes of the list and find the first index matching the predicate:

IntStream.range(0, producersProcedureActive.size())
		 .filter(i -> producersProcedureActive.get(i).getPod().equals(pod))
         .boxed()
         .findFirst()
		 .map(i -> producersProcedureActive.remove((int) i));

With this solution, the remove(int) operation operates directly on the index.

Solution 5 - Java

Use can use filter of Java 8, and create another list if you don't want to change the old list:

List<ProducerDTO> result = producersProcedureActive
                            .stream()
                            .filter(producer -> producer.getPod().equals(pod))
                            .collect(Collectors.toList());

Solution 6 - Java

I'm sure this will be an unpopular answer, but it works...

ProducerDTO[] p = new ProducerDTO[1];
producersProcedureActive
            .stream()
            .filter(producer -> producer.getPod().equals(pod))
            .findFirst()
            .ifPresent(producer -> {producersProcedureActive.remove(producer); p[0] = producer;}

p[0] will either hold the found element or be null.

The "trick" here is circumventing the "effectively final" problem by using an array reference that is effectively final, but setting its first element.

Solution 7 - Java

With Eclipse Collections you can use detectIndex along with remove(int) on any java.util.List.

List<Integer> integers = Lists.mutable.with(1, 2, 3, 4, 5);
int index = Iterate.detectIndex(integers, i -> i > 2);
if (index > -1) {
    integers.remove(index);
}
        
Assert.assertEquals(Lists.mutable.with(1, 2, 4, 5), integers);

If you use the MutableList type from Eclipse Collections, you can call the detectIndex method directly on the list.

MutableList<Integer> integers = Lists.mutable.with(1, 2, 3, 4, 5);
int index = integers.detectIndex(i -> i > 2);
if (index > -1) {
    integers.remove(index);
}

Assert.assertEquals(Lists.mutable.with(1, 2, 4, 5), integers);

Note: I am a committer for Eclipse Collections

Solution 8 - Java

The below logic is the solution without modifying the original list

List<String> str1 = new ArrayList<String>();
str1.add("A");
str1.add("B");
str1.add("C");
str1.add("D");

List<String> str2 = new ArrayList<String>();
str2.add("D");
str2.add("E");

List<String> str3 = str1.stream()
                        .filter(item -> !str2.contains(item))
                        .collect(Collectors.toList());

str1 // ["A", "B", "C", "D"]
str2 // ["D", "E"]
str3 // ["A", "B", "C"]

Solution 9 - Java

When we want to get multiple elements from a List into a new list (filter using a predicate) and remove them from the existing list, I could not find a proper answer anywhere.

Here is how we can do it using Java Streaming API partitioning.

Map<Boolean, List<ProducerDTO>> classifiedElements = producersProcedureActive
    .stream()
    .collect(Collectors.partitioningBy(producer -> producer.getPod().equals(pod)));

// get two new lists 
List<ProducerDTO> matching = classifiedElements.get(true);
List<ProducerDTO> nonMatching = classifiedElements.get(false);

// OR get non-matching elements to the existing list
producersProcedureActive = classifiedElements.get(false);

This way you effectively remove the filtered elements from the original list and add them to a new list.

Refer the 5.2. Collectors.partitioningBy section of this article.

Solution 10 - Java

As others have suggested, this might be a use case for loops and iterables. In my opinion, this is the simplest approach. If you want to modify the list in-place, it cannot be considered "real" functional programming anyway. But you could use Collectors.partitioningBy() in order to get a new list with elements which satisfy your condition, and a new list of those which don't. Of course with this approach, if you have multiple elements satisfying the condition, all of those will be in that list and not only the first.

Solution 11 - Java

Combining my initial idea and your answers I reached what seems to be the solution to my own question:

public ProducerDTO findAndRemove(String pod) {
	ProducerDTO p = null;
	try {
		p = IntStream.range(0, producersProcedureActive.size())
		     .filter(i -> producersProcedureActive.get(i).getPod().equals(pod))
		     .boxed()
		     .findFirst()
		     .map(i -> producersProcedureActive.remove((int)i))
		     .get();
		logger.debug(p);
	} catch (NoSuchElementException e) {
		logger.error("No producer found with POD [" + pod + "]");
	}
	return p;
}

It lets remove the object using remove(int) that do not traverse again the list (as suggested by @Tunaki) and it lets return the removed object to the function caller.

I read your answers that suggest me to choose safe methods like ifPresent instead of get but I do not find a way to use them in this scenario.

Are there any important drawback in this kind of solution?

Edit following @Holger advice

This should be the function I needed

public ProducerDTO findAndRemove(String pod) {
	return IntStream.range(0, producersProcedureActive.size())
			.filter(i -> producersProcedureActive.get(i).getPod().equals(pod))		
	        .boxed()																
	        .findFirst()
	        .map(i -> producersProcedureActive.remove((int)i))
	        .orElseGet(() -> {
	        	logger.error("No producer found with POD [" + pod + "]"); 
	        	return null; 
	        });
}

Solution 12 - Java

the task is: get ✶_and_✶ remove element from list

p.stream().collect( Collectors.collectingAndThen( Collector.of(
    ArrayDeque::new,
    (a, producer) -> {
      if( producer.getPod().equals( pod ) )
        a.addLast( producer );
    },
    (a1, a2) -> {
      return( a1 );
    },
    rslt -> rslt.pollFirst()
  ),
  (e) -> {
    if( e != null )
      p.remove( e );  // remove
    return( e );    // get
  } ) );

Solution 13 - Java

resumoRemessaPorInstrucoes.removeIf(item -> 
			item.getTipoOcorrenciaRegistro() == TipoOcorrenciaRegistroRemessa.PEDIDO_PROTESTO.getNome() ||
			item.getTipoOcorrenciaRegistro() == TipoOcorrenciaRegistroRemessa.SUSTAR_PROTESTO_BAIXAR_TITULO.getNome());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarco StramezziView Question on Stackoverflow
Solution 1 - Javauma shankarView Answer on Stackoverflow
Solution 2 - Javaasifsid88View Answer on Stackoverflow
Solution 3 - JavaVasily LiaskovskyView Answer on Stackoverflow
Solution 4 - JavaTunakiView Answer on Stackoverflow
Solution 5 - JavaToi NguyenView Answer on Stackoverflow
Solution 6 - JavaBohemianView Answer on Stackoverflow
Solution 7 - JavaDonald RaabView Answer on Stackoverflow
Solution 8 - JavaKimchiManView Answer on Stackoverflow
Solution 9 - JavaShanika EdiriweeraView Answer on Stackoverflow
Solution 10 - Javauser140547View Answer on Stackoverflow
Solution 11 - JavaMarco StramezziView Answer on Stackoverflow
Solution 12 - JavaKaplanView Answer on Stackoverflow
Solution 13 - JavaRicardo WojciechowskiView Answer on Stackoverflow