如何在java中找到二维ArrayList的列的唯一值? [英] How to find unique value of a column of a 2D ArrayList in java?

查看:28
本文介绍了如何在java中找到二维ArrayList的列的唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Delete
{
    public static void main(String[] args)
    {
         List<List<String>> list = new ArrayList<>();
         list.add(List.of("A","B","C","R"));
         list.add(List.of("E","F","G","F"));
         list.add(List.of("A","B","C","D"));
         System.out.println(list.stream().distinct().count());
         Map<String, Long> countMapOfColumn = list.stream()
                                                  .filter(innerList -> innerList.size() >= 3)
                                                  .map(innerList -> innerList.get(3 - 1))
                         .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(countMapOfColumn.keySet());
    }
}

我想找出第 3 列的唯一元素 "C","G" 并且第 3 列有 2 个唯一元素.

I want to find out the unique element of column 3 which are "C","G" and there are 2 unique element in column 3.

这可以使用 loop 来完成,但我不能使用循环.使用 java stream 可能有一个解决方案.

This can be done using loop but I can't use loop. Using java stream there may be a solution.

此外,如何在 1,2 列中一次获得具有 "A","B" 的行数,通常对于 N 列?

In addition, how can I get count of rows having "A","B" in column 1,2 at a time and in general for N columns?

推荐答案

对于你给定的数据结构;

For your given datastructure;

Map<String, Long> countMapOfColumn = list.stream()
        .filter(innerList -> innerList.size() >= column)
        .map(innerList -> innerList.get(column - 1))
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

where column = 3, 会给你一个 {"C", "G"} 作为键集的地图,值会给你每个字符的计数在给定的列中.它也适用于非均匀矩阵,因为它只会跳过没有第 n 列的行.

where column = 3, will get you a map with {"C", "G"} as keyset, and values will give you the count of each character in the given column. It will also work for non uniform matrices, since it will just skip the rows that has no nth column.

任何列的唯一计数将决定 collect 方法的结果映射的大小;countMapOfColumn.size()

Unique count of any column will the the size of resulting map of collect method; countMapOfColumn.size()

要获取单个字符计数,请使用带有键的结果映射作为输入,例如要获取 column = 3C 的计数,请使用 countMapOfColumn.get("C"),返回2

To get individual char counts, use the resulting map with keys as input, for example to get count of C in column = 3, use countMapOfColumn.get("C"), which will return 2

但我更愿意使用 Guava 的 Table,你可以直接选择任何给定的行或列,没有任何麻烦,那么它只是,获取第 n 列,并过滤我们的重复项.

But I'd rather use a Guava's Table, you can directly select any given row or column without any hassle, then it is only, getting nth column, and filter our duplicates.

更新

获取以连续列表开头的行数 list = {"A", "B"};

To get the count of rows that start with consecutive list list = {"A", "B"};

List<String> startingList = Arrays.asList("A", "B");
Long count = list.stream()
        .filter(innerList -> innerList.size() >= startingList.size() 
                && innerList.subList(0, startingList.size()).equals(startingList))
        .count();

这篇关于如何在java中找到二维ArrayList的列的唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆