简体   繁体   中英

How do I draw specific values within an ArrayList embedded into the same Hashmap key?

I'm working on a RFID reader application in java, and I'm having difficulties extracting the reader results.

As the values returned are of different datatypes, the method that reads the RFID tags inputs its values into an ArrayList which is embedded into a LinkedHashMap under keys of "String" and "Integers".

LinkedHashMap lhm = new LinkedHashMap();
List<String> listOfStringVals = new ArrayList<String>();
List<Integer> listOfIntVals = new ArrayList<Integer>();
String tagID = null;
String TimeStamp = null;
String Date = null;
String Time = null;
int tagAntenna = 0;

tagReads = r.read(1000);
// Print tag reads
for (TagReadData tr : tagReads) 
{
    tagID = tr.epcString();
    Date = df.format(new Date());
    Time = tf.format(new Date());
    TimeStamp = Date + " " + Time;
    tagAntenna = tr.getAntenna();

    listOfStringVals.add(tagID);
    listOfStringVals.add(TimeStamp);
    listOfIntVals.add(tagAntenna);

    lhm.put("strings", listOfStringVals);
    lhm.put("integers", listOfIntVals);

    // Shut down reader
    r.destroy();
}
connect.insertData(lhm);

The LinkedHashMap is transferred to a seperate method for extracting.

System.out.println("Fetching Keys and corresponding [Multiple] Values ");

for (Map.Entry<String, List<String>> entry : readMap.entrySet()) { 

    String key = entry.getKey();
    List<String> values = entry.getValue();

    System.out.println("Key = " + key);
    System.out.println("Values = " + values);
}
System.out.println(" Insert Complete! ");

With this I get the following in console:

Fetching Keys and corresponding [Multiple] Values   
Key = strings  
Values = [tagid1, timestamp1, tagid2, timestamp3, tagid3, timestamp3]  
Key = integers  
Values = [antenna1, antenna2, antenna3]  
Insert Complete!   

What code should I add to be able to get the tagID, timestamp, and antenna of the same number group (separate the collection of values into the tags that each one belongs originally)?

EDIT: Example of input in JSON

var tagReads = [{
    "epc" : "0048029C130143700278303F",
    "timestamp" : "2016-01-02 10:20:45",
    "antenna" : "1"
},
{
    "epc" : "0048029C1301437002783031",
    "timestamp" : "2016-01-02 10:20:45",
    "antenna" : "1"
}
{
    "epc" : "0048029C130143700278305F",
    "timestamp" : "2016-01-03 12:12:00",
    "antenna" : "2"
}];

Why don't you write a class that stores all the attributes for one tag?

public class RfidTag
{
  private String tagId;
  private LocalDateTime readTimestamp;
  private Integer antenna;

  // Getters and setters
}

The collection you store this in will depend on your requirements. If you need to remember the sequence the tags were read then an ArrayList would be best. If you need to retrieve a tag by it's ID then you could store in a map and use the tag ID as the key and the RfidTag as the value.

For example:

List<RfidTag> tags = new ArrayList<RfidTag>();

for (TagReadData tr : tagReads) 
{
    RfidTag tag = new RfidTag();

    tag.setTagId(tr.epcString());
    tag.setReadTimestamp(LocalDateTime.now());
    tag.setAntenna(tr.getAntenna());

    tags.add(tag);
}

Then if you wanted to print the information you could do something like:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
for(RfidTag tag : tags)
{
    System.out.println("Tag: " + tag.getTagId() + " "
                      + "Timestamp: " + tag.getReadTimestamp().format(formatter) + " "
                      + "Antenna: " + tag.getAntenna() ); 
}

Or if you override toString in RfidTag it would be even simpler.

for(RfidTag tag : tags)
{
    System.out.println(tag); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM