Java,如何实现perl的哈希数组?

时间:2011-04-29 08:55:36

标签: android data-structures

我对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中最好的构造是哪种?我应该使用HashMaps吗?你能举个例子吗?

4 个答案:

答案 0 :(得分:3)

方法1:
您可以使用Hashmaps来实现目标。

Map<String,String> beacons = new HashMap<String,String>();
beacons.put("ssid","north");
beacons.put("bssid","01:02:03:04:05:06");
System.out.println(beacons.get("ssid")); // Outputs "north"

方法2:
如果您的数据来自xml文件,则可以使用JAXB API(Java XML Binding)来构建对象。

假设我们有以下XML文件: beacons.xml

<beacons>
   <beacon ssid="north">
      <bssid>01:02:03:04:05:06</bssid>
   </beacon>
   <beacon ssid="north">
      <bssid>02:03:04:05:06:07</bssid>
   </beacon>
</beacons>

这些类可以这样定义:

<强> beacons.java

package com.mycompany;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Beacons {
    @XmlElement(name = "beacon")
    public List<Beacon> beaconsList = new ArrayList<Beacon>();
}

<强> beacon.java

package com.mycompany;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Beacon {
    @XmlAttribute
    private String ssid;
    @XmlElement
    private String bssid;

    public String getSsid() {
        return ssid;
    }

    public String getBssid() {
        return bssid;
    }
}

加载类的示例代码:

package com.mycompany;
import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class Main {

    public static void main(String args[]) {
        try {
            JAXBContext jc = JAXBContext.newInstance(Beacons.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();

            Beacons myBeacons = (Beacons) unmarshaller.unmarshal(new File("beacons.xml"));

            List<Beacon> beaconsList = myBeacons.beaconsList;
            for (int i = 0; i < beaconsList.size(); i++) {
                Beacon a_beacon = beaconsList.get(i);
                System.out.println("Beacon " + (i+1));
                System.out.println("SSID   : " + a_beacon.getSsid());
                System.out.println("BSSID  : " + a_beacon.getBssid());
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

-

这是输出:


Beacon 1
SSID   : north
BSSID  : 01:02:03:04:05:06

Beacon 2
SSID   : north
BSSID  : 02:03:04:05:06:07

答案 1 :(得分:1)

  

Java中最好的构造是什么?

如果您的数据结构合理,为什么要使用通用容器?

如果将这些定义良好的结构包装到自己的Java类中,您的代码将更有效。

这些类的实现当然需要代码,但是如果不使用hash.get(key)hash.put(key, value)样式访问器,后续使用结构将会容易得多。

答案 2 :(得分:1)

简单值对象:

public class Beacon {
    private String ssid;
    private String bssid;

    public Beacon(String ssid, String bssid) {
        this.ssid = ssid;
        this.bssid = bssid;
    }

    public String getSsid() {
        return ssid;
    }

    public String getBssid() {
        return bssid;
    }
}

public class Point {
    private String id;
    private Rssi rssi;

    public Point(String id, Rssi rssi) {
        this.id = id;
        this.rssi = rssi;
    }

    public String getId() {
        return id;
    }

    public Rssi getRssi() {
        return rssi;
    }
}

public class Rssi {
    private int north;
    private int east;
    private int south;
    private int west;

    public Rssi(int north, int east, int south, int west) {
        this.north = north;
        this.east = east;
        this.south = south;
        this.west = west;
    }

    public int getNorth() {
        return north;
    }

    public int getEast() {
        return east;
    }

    public int getSouth() {
        return south;
    }

    public int getWest() {
        return west;
    }
}

答案 3 :(得分:0)

private static final Map<String, String> BEACONS = new HashMap<String, String>();
beacons.put("north", "01:02:03:04:05:06");
...

private在这里意味着,没有实体而不是您的班级可以看到此地图。 static表示为此类创建一次,而不是每次创建对象。此外,Java命名约定假定您以大写字母命名constatns。

阅读更多关于HashMaps的here