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
添加回答
举报