kotlin - 如何使用 ArrayList 对象 Kotlin 中的 2 个条件进行计数
问题描述
假设我有这个模型课。
data class Bike(
val bikeId: String? = null,
val type: String? = null,
val color: String? = null,
)
如果我想得到 的计数color
,我可以使用这个:-
val freqs = list.groupingBy { it.color }.eachCount()
结果将是:-
{GREEN=3, YELLOW=12}
如何计算type
还包括在内的地方?
示例结果如下: -
{MOUNTAIN and GREEN=2}
{FOLDING and GREEN=1}
{MOUNTAIN and YELLOW=5}
{FOLDING and YELLOW=1}
{BMX and YELLOW=6}
等等..
解决方案
对于每个项目,您需要创建一个包含这两个字段的数据对象。简单Pair
就足够了:
list.groupingBy { it.type to it.color }.eachCount()
只是注意it.type to it.color
基本相同Pair(it.type, it.color)
。
推荐阅读
- visual-studio-code - 在 VS Code 中禁用架构建议
- angular - switchMap 没有取消之前的 HTTP 调用
- javascript - 我如何获得过去的 cookie 同意页面
- python - Django Rest Framework,如何在另一个外键中获取外键对象的数量
- javascript - 根据 MaterialUI 滑块的值更改图像
- python - 为什么我的 Python Tkinter GUI 运行缓慢并且没有响应?
- mysql - 如何从关系表中查询相关标签?
- python - 在 Pycharm 中,如何逐个单元格移动光标?
- nginx - syslog-ng 未过滤远程服务器上的标签
- java - 如何使用 @BeforeEach 方法在 Java 中进行测试?