2

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?

4 Answers 4

3

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
4
  • Yes, but are HashMaps useful for the slightly more complex "points" structure? I suppose they are not...
    – MarcoS
    Apr 29, 2011 at 9:17
  • @Marcos I have added a second approach by using the JAXB API (Java Xml Binding)
    – Stephan
    Apr 29, 2011 at 9:38
  • Sorry, I can't resolve "import javax.xml.bind.JAXBContext" in Eclipse: I did copy the "com.viewstreet.java.eclipse.jaxbplugin_0.1.0" directory to Eclipse's plugins directory, restarted Eclipse, but I keep getting "The import javax.xml.bind cannot be resolved" ...
    – MarcoS
    May 1, 2011 at 15:48
  • @Marcos which version of java do you use ? Check that the JRE System Library is in your buildpath(added by default in every new Java project in Eclipse). javax.xml.bind.JAXBContext should be located in rt.jar with java 6.
    – Stephan
    May 1, 2011 at 21:38
1

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.

1
  • Can you make a simple example on how to implement the "points" class?
    – MarcoS
    Apr 29, 2011 at 9:17
1

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;
    }
}
2
  • I like this approach best. Can You make an example to perform a comparison: i.e.: having 4 rssi values, find the closest point id?
    – MarcoS
    Apr 29, 2011 at 15:50
  • 1
    Can you tell me what algorithm to use or do i need to invent one? Seems there needs to be a large margin for error. What if one or two of the beacons or wifi reception in general are impaired? Apr 30, 2011 at 0:35
0
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.

2
  • Yes, but are HashMaps useful for the slightly more complex "points" structure? I suppose they are not...
    – MarcoS
    Apr 29, 2011 at 9:15
  • 1
    Yes, they are. Just use Point instead of String in value place. Apr 29, 2011 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.