gpt4 book ai didi

java - 如何通过使用子句 GROUP BY 两列查询来将值存储在数据结构(集合)中

转载 作者:行者123 更新时间:2023-11-29 07:36:32 25 4
gpt4 key购买 nike

我正在寻找在数据结构中存储值的最佳方法,其中值来自查询三列 xxxxxx GROUP BY status, height; (即两列)。结果看起来像。

status |  height | count |
--------------------------
InUse | 90 | 5 |
InUSe | 80 | 3 |
stock | 80 | 1 |
stock | 120 | 3 |
scrap | 90 | 1 |

现在我想存储在一些数据结构或 MultiMap 或任何最好的方式中,以便我可以获得计数的值。

或者

无论我能以何种最佳方式操纵这些值。

我想到的一件事是对于每组唯一的(状态、高度)--> count 我将获得 count 的值,以便我必须如何存储它们。

我可以做类似 Map< Map<someENUM, List<Long>>, Long> 的事情吗? 这对我有帮助吗?或任何其他方式来存储和使用此值而不会造成混淆。

status of type ENUM
height of type Long
count of type Long

EDIT: Thanks for your answers @Andy Turner, @OAD and @burhancerit

这些答案在 Java 中运行良好。但很抱歉没有具体说明我使用的上下文。

The Context where I'm using this is I want to populate a HTML table with this Guava Table suggested by @Andy Turner or ArrayList<myObject> suggested by @OAD and @ burhancerit in jstl/EL.

像这样

status |  height | count |                  Height | stock | Scrap | InUSe  
-------------------------- ---------------------------------
InUse | 90 | 5 | HTML 90 | 0 | 1 | 5
InUSe | 80 | 3 | ------> Table 80 | 1 | 0 | 3
stock | 80 | 1 | using EL 120 | 3 | 0 | 0
stock | 120 | 3 |
scrap | 90 | 1 |

那么,现在在这种情况下哪种方法最好,以及如何在 EL 中使用它们。

最佳答案

因为你标记了 Guava:将其存储在 Guava Table 中,其中行是状态,列是高度:

Table<String, Long, Long> table;

例如:

// Construction:
ImmutableTable.Builder<String, Long, Long> builder =
ImmutableTable.builder();
for (RowType row : rows) {
builder.put(row.getStatus(), row.getHeight(), row.getCount());
}
ImmutableTable<StatusType, HeightType, CountType> table = builder.build();

// Retrieval:
Long count = table.get("InUse", 90L);

要构建您在问题中描述的表格,您可以使用此答案中建议的表格结构,或者您可以转置表格,使其成为表格(交换行和列)。然后(作为普通控制台输出给出的示例,因为我不熟悉 el):

Set<String> statuses = table.columnKeySet();
System.out.print("Height");
for (String status : statuses) {
System.out.print("|" + status);
}
System.out.println();
for (Long height : table.rowKeySet()) {
System.out.print(height);
for (String status : statuses) {
Long count = Objects.firstNotNull(table.get(height, status), 0L);
System.out.print("|" + count);
}
System.out.println();
}

关于java - 如何通过使用子句 GROUP BY 两列查询来将值存储在数据结构(集合)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35084333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com