番石榴表与二维数组 [英] Guava Table vs. 2D array

查看:157
本文介绍了番石榴表与二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么用Guava表实施和一个二维数组,如果我事先知道数组的大小区别?

What is the difference between using a Guava Table implementation and a 2D array if I know the size of the array beforehand?

是其中一个比另一个更有效?怎么样?
难道让运行中的时间差?

Is one more efficient than the other? How? Would it make a difference in running time?

推荐答案

最明显和最关键的区别是,一个数组总是索引,以 INT S,而一个表可与任意对象进行索引。

The most obvious and crucial difference is that an array is always indexed, with ints, whereas a Table can be indexed with arbitrary objects.

从番石榴网站表实例>:

Consider the Table Example from the Guava site:

Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
weightedGraph.put(v1, v2, 4);
...

索引这里通过顶点对象发生。如果你想要做同样的一个数组,你将不得不一些 getIndex 方法添加到顶点类,和accesss阵列像这样

The indexing here happens via Vertex objects. If you wanted to do the same with an array, you would have to add some getIndex method to the Vertex class, and accesss the array like this

double array[][] = new double[4][5];
array[v1.getIndex()][v2.getIndex()] = 4;

这是不方便的,特别是难以维持 - 尤其是,当指数可能会改变(或顶点时必须添加/删除,虽然你提到​​这是不适合你的情况下)

This is inconvenient and particularly hard to maintain - especially, when the indices may change (or when vertices have to be added/removed, although you mentioned that this is not the case for you).

此外,该番石榴表允许获得的的或的的作为独立的实体。在一个二维数组,你可以随时访问任何一个的的或者一个的的数组的 - 这取决于你如何跨preT的2维数组的。该表允许的形式,访问这两个,每个地图

Additionally, the Guava table allows obtaining rows or columns as separate entities. In a 2D array, you can always access either one row or one column of the array - depending on how you interpret the 2 dimensions of the array. The table allows accessing both, each in form of a Map.

关于性能:会有,你就会有性能上的 noticable 的差异情况。特别是当你有一个的的二维数组的原始的类型( INT 双击等)和替代将是一个大表与相应的参考的类型(整数等)。但同样,这只会是noticable当阵列/表是真正的大。

Concerning the performance: There will be cases where you'll have a noticable difference in performance. Particularly when you have a large 2D array of primitive types (int, double etc), and the alternative would be a large table with the corresponding reference types (Integer, Double etc). But again, this will only be noticable when the array/table is really large.

这篇关于番石榴表与二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆