我想從 a 中獲取唯一值List<String[]>并將它們存儲在一個新列表中(或者唯一值在HashMap<String, Integer>哪里以及它在 中出現(xiàn)的次數(shù)。如何提取唯一值?StringIntegerList<String[]>
2 回答

長風(fēng)秋雁
TA貢獻1757條經(jīng)驗 獲得超7個贊
您可以使用Collectors.groupingBy
Map<String, Long> map = abc.stream() .flatMap(Arrays::stream) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

RISEBY
TA貢獻1856條經(jīng)驗 獲得超5個贊
如果您使用 Java 8 或更高版本,這非常簡單。(使用其他答案稍作修正)
List<String[]> abc = new ArrayList<>();
String[] string1 = {"123", "567"};
String[] string2 = {"123", "456"};
abc.add(string1);
abc.add(string2);
List<String> newList = abc.stream()
.flatMap(Arrays::stream)
.distinct()
.collect(Collectors.toList());
Map<String, Long> hashMap = abc.stream()
.flatMap(Arrays::stream)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
添加回答
舉報
0/150
提交
取消