Java,如何实现perl的哈希数组?
我对 Perl 的数据结构有很好的经验,并且我从 java(在 Android 上)开始。我需要实现类似 perl 数据结构的东西:
$beacons = (
{
ssid => "north",
bssid => "01:02:03:04:05:06",
},
{
ssid => "east",
bssid => "02:03:04:05:06:07",
},
{
ssid => "south",
bssid => "03:04:05:06:07:08",
},
{
ssid => "west",
bssid => "04:05:06:07:08:09",
},
);
$points = (
{
id => "La Gioconda",
rssi => {
north => -55,
east => -76,
south => -64,
west => -92,
},
},
{
id => "La Pietà",
rssi => {
north => -51,
east => -60,
south => -88,
west => -59,
},
},
...
);
这些是静态结构(最终从磁盘上的 xml 文件读取)。
如果它有任何用处,我正在实现一个本地(室内)定位系统,利用位于博物馆每个房间 4 个角落的 4 个 wifi 信标的 rssi(信号强度)值的变化来找到智能手机位置。在运行时,我必须将 4 个 rssi 值(每个值来自具有特定 ssid 和 bssid 的信标)与我的所有点结构进行比较,并找到最接近我的值的一个。
Java 中最好的可用结构是什么?我应该使用 HashMap 吗?你能举个例子吗?
I have a good experience with Perl's data structures, and I am beginning with java (on Android). I need to implement something like these perl data structures:
$beacons = (
{
ssid => "north",
bssid => "01:02:03:04:05:06",
},
{
ssid => "east",
bssid => "02:03:04:05:06:07",
},
{
ssid => "south",
bssid => "03:04:05:06:07:08",
},
{
ssid => "west",
bssid => "04:05:06:07:08:09",
},
);
$points = (
{
id => "La Gioconda",
rssi => {
north => -55,
east => -76,
south => -64,
west => -92,
},
},
{
id => "La Pietà",
rssi => {
north => -51,
east => -60,
south => -88,
west => -59,
},
},
...
);
These are static structures (eventually read from an xml file on disk).
If it can be of any use, I'm implementing a local (indoor) positioning system using the change in the rssi (signal strength) values from 4 wifi beacons positioned in the 4 corners of each room of a museum, to find the smartphone position. At runtime, I'll have to compare 4 rssi values (each from a beacon with a specific ssid and bssid) to all of my points structure, and find the one closest to my values.
Which is the best construct available in Java? Should I use HashMaps? Can you make some example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法一:
您可以使用哈希图来实现您的目标。
方法2:
如果您的数据来自 xml 文件,您可以使用 JAXB API(Java XML Binding)来构建您的对象。
假设我们有以下 XML 文件: beacons.xml
这些类可以这样定义:
beacons.java
beacon.java
示例代码加载类:
-
这是输出:
Approach 1:
You can use Hashmaps to acheive your goal.
Approach 2:
If your data comes from xml file, you can use JAXB API (Java XML Binding) for building your objects.
Let's say we have the following XML file : beacons.xml
The classes may be defined like this :
beacons.java
beacon.java
Sample code for loading the classes :
--
Here is the output :
如果您的数据结构良好,为什么还要使用通用容器呢?
如果将这些定义良好的结构包装到它们自己的 Java 类中,您的代码将会更加高效。
这些类的实现当然需要代码,但后续使用这些结构会容易得多,而无需使用
hash.get(key)
和hash.put(key, value)
样式访问器。Why use generic containers at all if your data is well structured?
Your code will be more efficient if you wrap those well defined structures into their own Java classes.
The implementation of those classes will of course require code, but subsequent use of the structures will be a lot easier without having to use
hash.get(key)
andhash.put(key, value)
style accessors.简单值对象:
Simple value objects:
private
在这里意味着,除了你的类之外,没有任何实体可以看到这个 Map。 static 意味着该类只创建一次,而不是每次创建对象时。另外,Java 命名约定假定您以大写形式命名 constatns。在此处了解有关 HashMap 的更多信息。
private
means here, that no entity instead of your class can see this Map. static means it is created once for this class, not each time the object is created. Also, Java Naming Conventions assumes you name constatns in Upper case.Read more here about HashMaps.