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

How to store values in data structure(collections) where the values came by querying with clause GROUP BY two columns

我正在寻找在数据结构中存储值的最佳方法,其中值来自查询三列 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);

要构建您在问题中描述的 table,您可以使用此答案中建议的 table 结构,或者您可以转置 table,这样它就是 Table(交换行和列)。然后(作为普通控制台输出的示例,因为我不熟悉 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();
}

好吧,这取决于您要如何存储。比如arrayLists, maps等..

如果你有一个这样的对象到下面的值

class YourObject{
 status
 height
 count

//getters-setters etc some other stuff

}

您可以使用这样的 class 将您的值存储在 ArrayList<YourObject> list 中;

add,
remove,
getItem,
size,
.
.

还有list很灵活,可以根据自己的数据结构实现,还有一个是用Queue,Linklist,这些都已经实现了,可以用了。

对于小而快速变化的数据,我更喜欢Arraylist,它有很多方法,可以轻松访问数据。等..

也请看看这个

更详细的arraylist解释。

这只是一个简单的条目explanation.Hope它很有帮助。

编辑:OAD 已经提到了这一点。