I'm working on a project where I have to determine when a driving event ends. A function is given an HashMap<Date, Integer>
containing confidence levels of in vehicle/on foot in form of a percentage alongside a unix timestamp.
I'm attempting to iterate over this HashMap, determine if a driving event has ended.
I'm a PHP developer, and its a real challenge using Java logic like this, and I'm struggling to achieve something that should be simple. Any help appreciated.
Heres the logic I'm trying to implement:
If
we have at least 1 minute worth of data with 10 items in our HashMap
then
loop HashMap
if past 30 seconds of time, from 30 seconds ago contain driving data of 60% confidence adverage or above then
AND past 30 seconds of time from now contains working data with average 60% confidence or above
then
mark isDriving as true
if isDriving == true
then
doSomething()`
My HashMap looks something like this:
private HashMap mActivityData = new HashMap<String, Long>();
mActivityData.putExtra("in_vehicle",80); // % confidence
mActivityData.putExtra("on_foot",10); // % confidence
mActivityData.putExtra("time",1461684458); // unix time stamp
This is only a partial answer.
You have mentioned "loop HashMap { ... } " in your question, but you can only loop over the keys of the hashmap. (or over the values with .values()
)
To get the keys of a HashMap<Date, Integer>
:
Set<Date> dates_unordered = my_hashmap.keySet ();
A Set is unordered, to order it use a function like this one to create an ordered list. (the "Date" class implements the "Comparable" interface which is needed for that sorting to work)
List<Date> dates_ordered = asSortedList (dates_unordered);
Iterate over the list.
Iterator<Date> it = dates_ordered.iterator (); // create an iterator object
while (it.hasNext ())
{
Date d = it.next ();
Integer i = my_hashmap.get (d); // access value in hashmap with this key
// do something with "i" here
// ...
}
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.