2 回答

TA貢獻1155條經驗 獲得超0個贊
如果它必須是數組,你去:
Collections.sort(al);
String[] wordsResult = new String[al.size()];
int i = 0;
for (Tuple tuple : al) {
wordsResult[i++] = tuple.word;
}
Stream.of(wordsResult).forEach(System.out::println);
System.out.println(al);

TA貢獻1752條經驗 獲得超4個贊
您還可以使用 Stream-API 對這個 ArrayList 進行排序
Object[] arr = al.stream()
.sorted(Comparator.comparing(Tuple::getCount).reversed())//sort counts as per count
.map(Tuple::getWord) //mapping to each count to word
.toArray(); //collect all words to array
System.out.println(Arrays.toString(arr));
你也可以把這些話收藏起來List<String>
List<String> arr =al.stream()
.sorted(Comparator.comparing(Tuple::getCount).reversed())//sort counts as per count
.map(Tuple::getWord) //mapping to each count to word
.collect(Collectors.toList())//Collect to List
- 2 回答
- 0 關注
- 186 瀏覽
添加回答
舉報