存储 3 列 oracle 表的最佳 Java 数据结构?3列数组?还是双地图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1612430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:16:14  来源:igfitidea点击:

Best Java data structure to store a 3 column oracle table? 3 column array? or double map?

javadatabaseoracledata-structureshashtable

提问by Ayrad

What is the best data structure to store an oracle table that's about 140 rows by 3 columns. I was thinking about a multi dimensional array.

存储大约 140 行 x 3 列的 oracle 表的最佳数据结构是什么。我在考虑一个多维数组。

By best I do not necessarily mean most efficient (but i'd be curious to know your opinions) since the program will run as a job with plenty of time to run but I do have some restrictions:

最好的我并不一定意味着最有效(但我很想知道您的意见),因为该程序将作为一项有足够时间运行的工作运行,但我确实有一些限制:

It is possible for multiple keys to be "null" at first. so the first column might have multiple null values. I also need to be able to access elements from the other columns. Anything better than a linear search to access the data?

多个键一开始可能为“空”。所以第一列可能有多个空值。我还需要能够从其他列访问元素。有什么比访问数据的线性搜索更好的吗?

So again, something like [][][] would work.. but is there something like a 3 column map where I can access by the key or the second column ? I know maps have only two values.

再说一次,像 [][][] 这样的东西会起作用..但是有没有像 3 列地图这样我可以通过键或第二列访问的东西?我知道地图只有两个值。

All data will probably be strings or cast as strings.

所有数据都可能是字符串或转换为字符串。

Thanks

谢谢

回答by skaffman

A custom class with 3 fields, and a java.util.Listof that class.

具有 3 个字段的自定义类,以及java.util.List该类的一个。

There's no benefit in shoe-horning data into arrays in this case, you get no improvement in performance, and certainly no improvement in code maintainability.

在这种情况下,将数据硬塞到数组中没有任何好处,性能没有提高,代码可维护性当然也没有提高。

回答by duffymo

This is another example of people writing FORTRAN in an object-oriented language.

这是人们用面向对象语言编写 FORTRAN 的另一个例子。

Java's about objects. You'd be much better off if you started using objects to abstract your problem, hide details away from clients, and reduce coupling.

Java 是关于对象的。如果您开始使用对象来抽象您的问题、对客户端隐藏细节并减少耦合,那么您的情况会好得多。

What sensible object, with meaningful behavior, do those three items represent? I'd start with that, and worry about the data structures and persistence later.

这三个项目代表什么有意义的对象,具有有意义的行为?我会从那个开始,然后担心数据结构和持久性。

All data will probably be strings or cast as strings.

所有数据都可能是字符串或转换为字符串。

This is fine if they really are strings, but I'd encourage you to look deeper and see if you can do better.

如果它们真的是字符串,这很好,但我鼓励您更深入地研究,看看您是否可以做得更好。

For example, if you write an application that uses credit scores you might be tempted to persist it as a number column in a database. But you can benefit from looking at the problem harder and encapsulating that value into a CreditScore object. When you have that, you realize that you can add something like units ("FICO" versus "TransUnion"), scale (range from 0 to 850), and maybe some rich behavior (e.g., rules governing when to reorder the score). You encapsulate everything into a single object instead of scattering the logic for operating on credit scores all over your code base.

例如,如果您编写一个使用信用评分的应用程序,您可能想将它作为一个数字列保存在数据库中。但是您可以通过更深入地研究问题并将该值封装到 CreditScore 对象中而受益。有了这些之后,您就会意识到可以添加诸如单位(“FICO”与“TransUnion”)、比例(范围从 0 到 850)之类的内容,还可以添加一些丰富的行为(例如,控制何时对分数重新排序的规则)。您将所有内容都封装到单个对象中,而不是将用于操作信用评分的逻辑分散到整个代码库中。

Start thinking less in terms of tables and columns and more about objects. Or switch languages. Python has the notion of tuples built in. Maybe that will work better for you.

开始少考虑表和列,多考虑对象。或者切换语言。Python 内置了元组的概念。也许这对你更有效。

回答by Andrey Adamovich

If you need to access your data by key and by another key, then I would just use 2 maps for that and define a separate class to hold your record.

如果您需要通过键和另一个键访问您的数据,那么我将只使用 2 个映射并定义一个单独的类来保存您的记录。

class Record {
   String field1;
   String field2;
   String field3;
}

and

   Map<String, Record> firstKeyMap = new HashMap<String, Record>();
   Map<String, Record> secondKeyMap = new HashMap<String, Record>();

回答by Valentin Jacquemin

I'd create an object which map your record and then create a collection of this object.

我会创建一个映射您的记录的对象,然后创建该对象的集合。