RE: Methods removeFirstIf in Collection and skipFirst in Stream

2021-07-02 Thread Alberto Otero Rodríguez
Hi,

In my case, I have a list of entities related to another entity. In that list 
obviously there weren't repeated entities (entities with the same id). And I 
have the id of an entity to be removed from that list.

So, I have the following alternatives which are pretty inefficient:

1) entityList.removeIf(entity -> entityIdToRemove.equals(entity.getId()));

2) entityList = entityList.stream()
.filter(entity -> !entityIdToRemove.equals(entity.getId()))
.collect(Collectors.toList());

And I have this other alternative which is too much code:

Iterator iterator = entityList.iterator();
while (iterator.hasNext()) {
Entity entity = iterator.next();
if(entityIdToRemove.equals(entity.getId())) {
itr.remove();
break;
}
}

So, what I propose would be something like:

1) entityList.removeFirstIf(entity -> entityIdToRemove.equals(entity.getId()));

2) entityList = entityList.stream()
.skipFirst(entity -> entityIdToRemove.equals(entity.getId()))
.collect(Collectors.toList());

Regards,

Alberto.

De: Tagir Valeev 
Enviado: viernes, 2 de julio de 2021 19:59
Para: Alberto Otero Rodríguez 
Cc: core-libs-dev 
Asunto: Re: Methods removeFirstIf in Collection and skipFirst in Stream

Hello!

It's unclear why one might need methods like this. Could you please provide any 
real life use cases when such methods could be useful? Thanks.

With best regards,
Tagir Valeev.

пт, 2 июл. 2021 г., 21:28 Alberto Otero Rodríguez 
mailto:albest...@hotmail.com>>:
Sorry for the links, the methods would be:

1) In Collection:
   default boolean removeFirstIf​(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
 filter)

2) In Stream:
   Stream skipFirst(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 predicate)

De: Alberto Otero Rodríguez
Enviado: viernes, 2 de julio de 2021 16:22
Para: core-libs-dev@openjdk.java.net 
mailto:core-libs-dev@openjdk.java.net>>
Asunto: Methods removeFirstIf in Collection and skipFirst in Stream

Hi,

I think it would be interesting adding the following methods:

1) In Collection:
   default boolean 
removeFirstIf​(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
 filter)

2) In Stream:
   
Streamhttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 
skipFirst(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 predicate)

The purpose of both methods would be the same. Deleting the first element in 
the collection that fulfills the predicate.

This could be useful in collections/streams with no duplicate elements where 
performance would be better than using removeIf from Collection of filter from 
Stream.

Regards,

Alberto.


Re: Methods removeFirstIf in Collection and skipFirst in Stream

2021-07-02 Thread Tagir Valeev
Hello!

It's unclear why one might need methods like this. Could you please provide
any real life use cases when such methods could be useful? Thanks.

With best regards,
Tagir Valeev.

пт, 2 июл. 2021 г., 21:28 Alberto Otero Rodríguez :

> Sorry for the links, the methods would be:
>
> 1) In Collection:
>default boolean removeFirstIf​(Predicate https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
> filter)
>
> 2) In Stream:
>Stream skipFirst(Predicate https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
> predicate)
> 
> De: Alberto Otero Rodríguez
> Enviado: viernes, 2 de julio de 2021 16:22
> Para: core-libs-dev@openjdk.java.net 
> Asunto: Methods removeFirstIf in Collection and skipFirst in Stream
>
> Hi,
>
> I think it would be interesting adding the following methods:
>
> 1) In Collection:
>default boolean removeFirstIf​(Predicate<
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/function/Predicate.html> super E<
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
> filter)
>
> 2) In Stream:
>Stream<
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html
> > https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
> skipFirst(Predicate<
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/function/Predicate.html> super T<
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
> predicate)
>
> The purpose of both methods would be the same. Deleting the first element
> in the collection that fulfills the predicate.
>
> This could be useful in collections/streams with no duplicate elements
> where performance would be better than using removeIf from Collection of
> filter from Stream.
>
> Regards,
>
> Alberto.
>


RE: Methods removeFirstIf in Collection and skipFirst in Stream

2021-07-02 Thread Alberto Otero Rodríguez
Sorry for the links, the methods would be:

1) In Collection:
   default boolean removeFirstIf​(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
 filter)

2) In Stream:
   Stream skipFirst(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 predicate)

De: Alberto Otero Rodríguez
Enviado: viernes, 2 de julio de 2021 16:22
Para: core-libs-dev@openjdk.java.net 
Asunto: Methods removeFirstIf in Collection and skipFirst in Stream

Hi,

I think it would be interesting adding the following methods:

1) In Collection:
   default boolean 
removeFirstIf​(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html>>
 filter)

2) In Stream:
   
Streamhttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 
skipFirst(Predicatehttps://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/Stream.html>>
 predicate)

The purpose of both methods would be the same. Deleting the first element in 
the collection that fulfills the predicate.

This could be useful in collections/streams with no duplicate elements where 
performance would be better than using removeIf from Collection of filter from 
Stream.

Regards,

Alberto.