3
Good morning guys, sorry my ignorance for the simple doubt. I’ll get right to the point.
I have a list of alerts
List<Alertas> alertas;
This list there are several messages sent to some users, in it I have the following methods that I can use:
getUsers_receive;
getMsg;
The idea is that I can only show the message if the person’s user is in "usrs_receive" so for this I need to scan the list I already have of "user_receive" and try to find the person’s user.
All users are separated by comma, so soon I know I can use Split to do this.
My difficulty lies in: Separate the user_received with the split and after that locate if in any of these users I find certain user.
Currently what I have is just this:
for (Alertas a : lista_alertas) {
String[] usrs = a.getUsers_receive().split(",");
}
Vinicius, are you using Java version 8? I ask this because there are some cool tricks that can be done with
Stream
. Otherwise you are on the right path. You can try to scan the arrayusrs
and see if any of the positions contain the current user. Alternatively, if you’re going to do the same thing over and over again for different users it might be worth building aHashSet
with the content ofusrs
, so you can use the methodcontains
which has constant amortized performance (it is faster than scanning the array).– Anthony Accioly
I am using the 8 yes @Anthonyaccioly, I will search on Stream. Thank you!
– Vinicius Leonardo