Java 中的通用表结构(如 ResultSet)

发布于 2024-09-02 03:41:16 字数 296 浏览 4 评论 0 原文

可能的重复:
类似java数据结构的表

有谁知道是否有一个好的通用的基于表的我可以用来操作数据的结构? ResultSet 是一个接口,所以如果我想要没有数据库的类似功能,我是否必须完全实现某些东西? Apache Commons Collections 似乎没有立即合适的东西。

Possible Duplicate:
Table like java data structure

Does anyone know if there's a good general-purpose Table-based structure that I can use for manipulating data? ResultSet is an interface, so am I stuck having to fully implement something if I want similar functionality without a database? Apache Commons Collections does not seem to have anything immediately suitable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迎风吟唱 2024-09-09 03:41:16

通常您使用 <代码>列表为此设置 Javabean。每个 Javabean 又代表一个现实世界的实体,就像数据库中的一行。例如User

public class User {
    private Long id;
    private String name;
    private Integer age;
    // Add/generate getters+setters.
}

List<User> users = new ArrayList<User>();
users.add(new User(1L, "foo", 30));
users.add(new User(2L, "bar", 20));
//...

List提供了通过索引快速访问的方法。

对于不确定数量的属性(列),您可以考虑使用 Map 而不是其中键表示属性(列)名称。

List<Map<String, Object>> table = new ArrayList<Map<String, Object>>();
Map<String, Object> row = new HashMap<String, Object>();
row.put("id", 1L);
row.put("name", "foo");
row.put("age", 30);
table.add(row);
// ...

它只比简单的 Javabean 增加了一点开销,因为这些字符串键也需要存储在堆中。

如果您不关心列名称,那么您也可以直接使用 List> ,甚至可能使用不太灵活的 Object[][].使用这两种方法,您可以轻松地通过 xy 访问元素。

Usually you use a List or Set of Javabeans for this. Each Javabean in turn represents one real world entity, like a row in a database. For example an User:

public class User {
    private Long id;
    private String name;
    private Integer age;
    // Add/generate getters+setters.
}

with

List<User> users = new ArrayList<User>();
users.add(new User(1L, "foo", 30));
users.add(new User(2L, "bar", 20));
//...

The List provides methods for fast access by index.

For an undetermined amount of properties (columns) you may consider to use a Map<String, Object> instead where the key represents the property (column) name.

List<Map<String, Object>> table = new ArrayList<Map<String, Object>>();
Map<String, Object> row = new HashMap<String, Object>();
row.put("id", 1L);
row.put("name", "foo");
row.put("age", 30);
table.add(row);
// ...

It only adds a bit more overhead than a simple Javabean since those string keys needs to be stored in the heap as well.

If you don't care about column names, then you can also just go ahead with a List<List<Object>> or maybe even a less flexible Object[][]. With both you can easily access elements by x and y.

请别遗忘我 2024-09-09 03:41:16

为什么不使用内存数据库,例如 HSQLDB 或 H2。它们非常轻量且快速,提供您似乎想要的 JDBC 接口,并且还提供您提到的 JOIN 功能。

Why not use an In-Memory Database, such as HSQLDB or H2. They are very lightweight and fast, provide the JDBC interface you seem to want and also provide the JOIN functionality you mention.

窝囊感情。 2024-09-09 03:41:16

Google Guava 库提供了多种通用Table 实现 - HashBasedTable,TreeBasedTable。这些类是通用的,因此任何类都可以充当行/列键。

文档中的示例:

Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
weightedGraph.put(v1, v2, 4.0);
weightedGraph.put(v1, v3, 20.0);
weightedGraph.put(v2, v3, 5.0);

weightedGraph.row(v1); // returns a Map mapping v2 to 4, v3 to 20
weightedGraph.column(v3); // returns a Map mapping v1 to 20, v2 to 5

Google Guava libraries offer several general purpose Table implementations - HashBasedTable, TreeBasedTable. The classes are generic so any class can act as a row/column key.

Example from the docs:

Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
weightedGraph.put(v1, v2, 4.0);
weightedGraph.put(v1, v3, 20.0);
weightedGraph.put(v2, v3, 5.0);

weightedGraph.row(v1); // returns a Map mapping v2 to 4, v3 to 20
weightedGraph.column(v3); // returns a Map mapping v1 to 20, v2 to 5
唯憾梦倾城 2024-09-09 03:41:16

Java 6 有一个 CachedRowSet 接口,并附带一个 Sun 类 CachedRowSetImpl。这可能是最接近您正在寻找的现成库存的东西。它被设计为由 ResultSet 填充。

如果这适合您,那么它可能正是您正在寻找的。

如果没有,您在正确填充它时可能会遇到问题。

Java 6 has a CachedRowSet interface and comes with a Sun class, CachedRowSetImpl. This probably is the closest thing stock out of the box to what you're looking for. It's designed to be populated by a ResultSet.

If that suits you it's probably exactly what you're looking for.

If not, you may have issues populating it properly.

流殇 2024-09-09 03:41:16

也许像 DefaultTableModel 这样的东西会满足您的要求。它通常与 JTable 一起使用来显示数据,但也可以单独使用。

或者基于 BeanTableModel RowTableModel 有一些更多的动态功能,可以让您更轻松地访问数据行。

Maybe something like the DefaultTableModel will meet your requirements. It is generally used with a JTable to display data but it can be used stand alone.

Or the BeanTableModel based on the RowTableModel has a few more dynamic features that allows you to access rows of data easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文