Sort LongSparseArray values - android

I have a LongSparseArray variable, in which the objects stored implement the interface Comparable.
Is there a easy way to sort them, without do it "manually"?
I tried Collections.sort(myLongSparseArray), but it does not implements the List interface.
Another way could be convert it to a List, but still I have not found any method to do that.

SparseArray, or LongSparseArray, should be considered as an efficient hash table when the keys are integers or longs. As such, it is not the best class to use if ordering is important to you.
Usually, when using hash-table type data structures, then uniqueness of values & efficiency of get / set are important.
If this is the case, perhaps you should look into using LinkedHashSet? It provides a way of holding unique items (based on their hashCode & equals functions), but also preserves the order of items, and has high efficiency of get / set.
If sorting is important, then you could extract the value list from the LinkedHashSet, then place it in a List, and use Collections.Sort() on it.

Related

How to store child classes in a database?

I'm really new to databases, but I'm making a running tracker app in Android, and I have some classes that represent the type of goals that you can have on a single type of workout.
What every Goal class does actually is to store a value but together with a unit, like for example a DistanceGoal will represent its value in meters and provide methods to convert it to kilometers or miles. A PaceGoal value will represent minutes per kilometers, and so on.
Also, according to type of Goal I will then have to read all tracking stats to see if the user has achieved the objective or not.
So:
interface GoalĀ {
long getGoalValue();
}
class TimeGoal implements Goal{}
class DistanceGoal implements Goal{}
class PaceGoal implements Goal{}
/* ...... */
Now this works well, although there may be a better way to solve this. The problem is that I need to store this in a database.
I have been using the Android room library, and I couldn't find any other way than to store each type of goal in its own table, so when I need to retrieve them I know what stored value represents what kind of goal.
But when I have to store a new goal, it will probably be given as its abstraction (the Goal interface), so I don't know in which table to store it.
The only solution I thought of is the Visitor Pattern, do any of you have a better solution for this?
I may have to make changes to the whole class hierarchy, but what really matters is to distinguish the unit from one value from the others, in order to read the tracking statistics accordingly.
You could make the Goal classes serializable. Then you can serialize the Goal objects to a byte array that you can store in your database. When you retrieve the byte array from the database, you can deserialize it to get the Goal object back. When you Google serializable java you should get lots of information on how to do this.

Performance penalty of ObjectBox assignable ids

ObjectBox docs suggest to use auto-assignable long ids for elements and it even has some checks based on it:
By default object IDs are assigned by ObjectBox. For each new object, ObjectBox will assign an unused ID that is above the current highest ID value used in a box. For example, if there are two objects with ID 1 and ID 100 in a box the next object that is put will be assigned ID 101.
http://objectbox.io/documentation/introduction/#Object_ID_assignment
If we have a custom key, we can add #Id(assignable = true) and it will use given field as an id.
However, I read somewhere that it adds some performance overhead and it is better to use the standard auto incremented ones whenever possible. I can't find the source now, so does anybody know if it is ok to use assignable ids for often changed objects? In addition, does ObjectBox use equals() and hashCode() somehow?
The main reasoning for using assignable ids for us is to be able to put elements using their natural long ids without manual resolving the mapping.
As I understood according to official docs and comment of Marcus Junginger (CTO of ObjectBox), there is no performance penalty when you use assignable ids.

When you should use SparseArray setValueAt()?

Why is setValueAt(...) in the public interface of the SparseArray class? I was using it instead of put(...) obviously not getting the result I had in mind.
I came here trying to figure out the same thing. As Prekak Sola mentioned in the comments, setValueAt maps a value on a specific index, while put maps a value on a specific key.
Obviously, that can become confusing, but I think it is a very useful feature, because if you look at the official SparseArray documentation, it is mentioned that the SparseArray is generally slower than a HashMap, because lookups require a binary search and adds and removes require inserting and deleting entries in the array.
So, I guess that in certain cases, it would perform much faster if you iterate over the items in this container using keyAt(int), size(), and obviously all index-related functions, such as setValueAt, instead of using the keys.

"global" array with different data types - Android

I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.

Android LRUCache Retrieval

I have implemented a standard LRUCache in Android that stores Objects. Each key is a unique ObjectId associated with the Object stored. My problem is that the only way to retrieve an Object from cache is by the ObjectId (no iterator). What would be the best way to implement a getAll() method?
Another option would be to store all the ObjectIds in a list somewhere, so I can iterate over the lists and get all of the Objects - but what would be the best way of holding all of the ObjectIds?
Thanks!
If you're using (or extending) the LruCache that Android provides, it has a snapshot method that returns a map of keys (your ObjectIds) and values (your Objects). You can do something like this:
Map<ObjectIds, Object> snapshot = lruCache.snapshot();
for (ObjectIds id : snapshot.keySet()) {
Object myObject = lruCache.get(id);
}
If you're not using Android's LruCache, then I imagine it would depend on your implementation. (I'd also be curious what motivated you to implement your own instead of subclassing the provided one!)
Using snapshot to get current collection at the moment
lruCache.snapshot().values()
It does not make sense to iterate over the objects in a LRU cache. You can not know which object is still in the cache and which got evicted (you actually can, but that's another story). It sound like you'd probably better off with a different data structure like a Hashmap or so. Nothing will ever get evicted from there.
A common use-case is to have a List of all possible object keys in memory. If you need one, you check if it is in the cache. If not, receive it and add it to the cache.

Categories

Resources