I have a list of words: List<Word> words
The bean Word
is:
public class Word {
private String name;
private String meaning;
...
}
I would like to shuffle the elements to have the following:
From:
{ name: "day", meaning: "giorno"},
{ name: "year", meaning: "anno"},
{ name: "hour", meaning: "ora"}
To:
{ name: "day", meaning: "ora"},
{ name: "year", meaning: "giorno"},
{ name: "hour", meaning: "anno"}
I've tried this but I think there is a more elegant solution:
private List<Word> shuffle(List<Word> words) {
List<String> names = words.stream()
.map(word -> word.getName())
.collect(Collectors.toList());
List<String> meanings = words.stream()
.map(word -> word.getMeaning())
.collect(Collectors.toList());
Collections.shuffle(meanings);
words = new ArrayList<Word>();
for(int i = 0; i<names.size(); i++) {
words.add(new Word(names.get(i), meanings.get(i)));
}
return words;
}