0

I have List with some users from google and now in stream I say for every google user make new HRVacationUser ( witch is model ) and give me ( their email, some random date, some random date ) which random date is for random holiday. But in that case i set each user is on vacantion. How can I take random user from google users and set random date for holiday, so I can make a request in the database - give me this user which is in holiday?

My List /GoogleUser> is with size for example 80, and I want to set only for random users vacantion date, and then to make request if(user is in vacantion return 'user') and in database to do request give me holiday users

public List<HRVacationUser> listVacationGoogleUsers(List<GoogleUser> allGoogleUsers){
        LocalDate date = LocalDate.now();
        List<HRVacationUser> collectHRUser = allGoogleUsers.stream()
                .map(user ->
                        new HRVacationUser(user.getPrimaryEmail(), date.minusDays(ThreadLocalRandom.current().nextInt(1,5)), date.plusDays(ThreadLocalRandom.current().nextInt(1, 5))))
                .collect(toList());
        return collectHRUser;
    }
BRRusev
  • 525
  • 2
  • 6
  • 12
  • 1
    You collect them to a ``List``. Generate a random number as list index, then modify the user behind ``collectHRUser.get(randomIndex)``. – f1sh Jul 24 '18 at 08:26
  • To solve the first part of your question have a look on this question: [How to get random objects from a stream](https://stackoverflow.com/questions/31703226/how-to-get-random-objects-from-a-stream) – LuCio Jul 24 '18 at 08:31
  • `Collections.shuffle(list)` before return of `collectHRUser` – ZhenyaM Jul 24 '18 at 08:42
  • Yes, but this is not like my problem. My allGoogleUsers list is with size for example 80, and I want to set only for random users vacantion date, and then to make request if(user is in vacantion return 'user') and in database to do request give me holiday users :) – BRRusev Jul 24 '18 at 08:42
  • You'd better provide a **minimal** demo to elaborate your idea? What **exactly** you wanna achieve? – Hearen Jul 24 '18 at 08:43

2 Answers2

0

You can take a random item from your list, based on your list size:

import java.util.Random;

List<?> yourList = new ArrayList<>();
yourList.get(new Random().nextInt(yourList.size()));
Leviand
  • 2,745
  • 4
  • 29
  • 43
-1

you can use the random library

import java.util.Random;

More possible solutions for random elements here =)

Marc
  • 199
  • 8