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

发布于 2024-11-04 01:56:55 字数 909 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(4

最后的乘客 2024-11-11 01:56:55

方法一:
您可以使用哈希图来实现您的目标。

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

Approach 1:
You can use Hashmaps to acheive your goal.

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"

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

<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>

The classes may be defined like this :

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;
    }
}

Sample code for loading the classes :

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();
        }
    }
}

--

Here is the output :


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

Beacon 2
SSID   : north
BSSID  : 02:03:04:05:06:07
一个人的夜不怕黑 2024-11-11 01:56:55

Java 中可用的最佳构造是什么?

如果您的数据结构良好,为什么还要使用通用容器呢?

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

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

Which is the best construct available in Java?

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) and hash.put(key, value) style accessors.

∞梦里开花 2024-11-11 01:56:55

简单值对象:

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;
    }
}

Simple value objects:

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;
    }
}
留蓝 2024-11-11 01:56:55
private static final Map<String, String> BEACONS = new HashMap<String, String>();
beacons.put("north", "01:02:03:04:05:06");
...

private 在这里意味着,除了你的类之外,没有任何实体可以看到这个 Map。 static 意味着该类只创建一次,而不是每次创建对象时。另外,Java 命名约定假定您以大写形式命名 constatns。

此处了解有关 HashMap 的更多信息。

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

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.

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