Filtrando uma list de Objetos em java

Salve turma estou tentando filtrar uma lista de objetos em java.
Tenho o seguinte lista:

  public List<country> lista(){
        List<country> pacotes = new ArrayList<>(Arrays.asList(
                new country("Brazil"),
                new country("França"),
                new country("Holanda"),
                new country("Canada"),
                new country("USA"),
                new country("Polonia"),
                new country("China"),
                new country("Japao"),
                new country("Suiça"),
                new country("Mexico"),
                new country("Bolivia"),
                new country("ARgentina")
                ));
        return pacotes;
    }

Pego ela e salvo com List lista = new CountryDAO().lista();

Porem quero fazer um sorteio e pegar 4 objetos aleatorios.
Tentei da seguinte maneira criando uma class sorteio:

public static List<country> sortearPaises(List<country> countriesList){
        List<country> newList = new ArrayList<country>();

        Random random = new Random();

        int count = 0;
        while(count < 4){
            int w = random.nextInt(countriesList.size());
            country newcountry = countriesList.get(w);

            country jaEstaEmUso = countriesList.stream().filter(x -> Objects.equals(x.getPais(), newcountry.getPais())).findFirst().orElse(null);
            if (jaEstaEmUso == null){

                newList.add(countriesList.get(w));
                count++;
            }
        }

        return newList;

    }

App quebra falando que não tem nada na lista.

Uma forma de fazer seria combinar os métodos Collections.shuffle e List.subList. Olha um exemplo:

class Main {
  public static void main(String... args) {
    class Country {
      private String name;

      Country(String name) {
        this.name = name;
      }

      @Override
      public String toString() {
        return this.name;
      }
    }

    List<Country> pacotes = Arrays.asList(
      new Country("Brazil"),
      new Country("França"),
      new Country("Holanda"),
      new Country("Canada"),
      new Country("USA"),
      new Country("Polonia"),
      new Country("China"),
      new Country("Japao"),
      new Country("Suiça"),
      new Country("Mexico"),
      new Country("Bolivia"),
      new Country("Argentina")
    );

    Collections.shuffle(pacotes);

    List<Country> sorteados = pacotes.subList(0, 4);

    System.out.println(sorteados);
  }
}
1 curtida

Caraio, eu dando maior volta e era so isso kkkkkkkkkkkkkkkkkkk Vlw De mais mano, novamente vc me salvando!
Tenho que estudar essas paradas de lista e filtros melhor.

1 curtida