Summary of ArrayList ordering in Kotlin (Android) - arrays

I am trying to provide a summary of items within an ArrayList (where order matters). Basically, I am setting up an exercise plan with two different types of activities (Training and Assessment). I then will provide a summary of the plan after adding each training/assessment to it.
The structure I have is something along the lines of:
exercisePlan: [
{TRAINING OBJECT},
{TRAINING OBJECT},
{ASSESSMENT OBJECT},
{TRAINING OBJECT}
]
What I want to be able to do is summarise this in a format of:
2 x Training, 1 x Assessment, 1 x Training, which will be displayed in a TextView in a Fragment. So I will have an arbitrarily long string that details the structure and order of the exercise plan.
I have tried to investigate using a HashMap or a plain ArrayList, but it seems pretty messy so I'm looking for a much cleaner way (perhaps a MutableList). Thanks in advance!

ArrayList is just a specific type of MutableList. It's usually preferable to use a plain List, because mutability can make code a little more complex to work with and keep robust.
I'd create a list of some class that wraps an action and the number of consecutive times to do it.
enum class Activity {
Training, Assessment
}
data class SummaryPlanStep(val activity: Activity, val consecutiveTimes: Int) {
override fun toString() = "$consecutiveTimes x $activity"
}
If you want to start with your summary, you can create it and later convert it to a plain list of activities like this:
val summary: List<SummaryPlanStep> = listOf(
SummaryPlanStep(Activity.Training, 2),
SummaryPlanStep(Activity.Assessment, 1),
SummaryPlanStep(Activity.Training, 1),
)
val plan: List<Activity> = summary.flatMap { List(it.consecutiveTimes) { _ -> it.activity } }
If you want to do it the other way around, it's more involved because I don't think there's a built-in way to group consecutive duplicate elements. You could a write a function for that.
fun <T> List<T>.groupConsecutiveDuplicates(): List<Pair<T, Int>> {
if (isEmpty()) return emptyList()
val outList = mutableListOf<Pair<T, Int>>()
var current = first() to 1
for (i in 1 until size) {
val item = this[i]
current = if (item == current.first)
current.first to (current.second + 1)
else {
outList.add(current)
item to 1
}
}
outList.add(current)
return outList
}
val plan: List<Activity> = listOf(
Activity.Training,
Activity.Training,
Activity.Assessment,
Activity.Training
)
val summary: List<SummaryPlanStep> = plan.groupConsecutiveDuplicates().map { SummaryPlanStep(it.first, it.second) }

This is what I have set up to work for me at the moment:
if (exercisePlanSummary.isNotEmpty() && exercisePlanSummary[exercisePlanSummary.size - 1].containsKey(trainingAssessment)) {
exercisePlanSummary[exercisePlanSummary.size - 1][trainingAssessment] = exercisePlanSummary[exercisePlanSummary.size - 1][trainingAssessment]!! + 1
} else {
exercisePlanSummary.add(hashMapOf(trainingAssessment to 1))
}
var textToDisplay = ""
exercisePlanSummary.forEach {
textToDisplay = if (textToDisplay.isNotEmpty()) {
textToDisplay.plus(", ${it.values.toList()[0]} x ${it.keys.toList()[0].capitalize()}")
} else {
textToDisplay.plus("${it.values.toList()[0]} x ${it.keys.toList()[0].capitalize()}")
}
}
where trainingAssessment is a String of "training" or "assessment". exercisePlanSummary is a ArrayList<HashMap<String, Int>>.
What #Tenfour04 has written above is perhaps more appropriate, and a cleaner way of implementing this. But my method is quite simple.

Related

Kotlin distinct() from 2 Dimensional array

fun main() {
val data = ArrayList<List<String>>()
data.add(listOf("32701", "First"))
data.add(listOf("32702", "Second"))
data.add(listOf("32702", "Second"))
data.add(listOf("32701", "First True"))
println(data.distinct())
}
Result :
[[32701, First], [32702, Second], [32701, First True]]
Question How about removing data [32701, First] and get new data with the same value ?
Expected :
[32702, Second], [32701, First True]]
The problem is that distinct() uses the equals methods and comparing the entirety of the list.
You could use distinctyBy { it.first() } if you can ensure lists wont be empty
Edit
In order to get latest value you can:
a) Reverse the list and then call distinctBy
yourList
.reversed() // Now latest values are first in the list
.distinctBy { it.first() } // first element of list holds the id
b) Associate the values into a map of Map<String, List<String>> by calling associateBy { it.first()} and getting the last value of the map by calling
val correctResults = map.values.map { valueList -> valueList.last() }
As a whole would look like:
yourList
.associateBy { it.first() }
.values
.map { valueList -> valueList.last() }
Be aware that any of these approaches IS NOT dealing with empty lists.
In order to deal with empty lists you could filter them out by just doing
val listsThatAreNotEmpty = yourList.filter { it.isNotEmpty() }
Use a combination of reversed and disinctBy:
fun main() {
val Data = ArrayList<List<String>>()
Data.add(listOf("32701", "First"))
Data.add(listOf("32702", "Second"))
Data.add(listOf("32702", "Second"))
Data.add(listOf("32701", "First True"))
println(Data.reversed().distinctBy{it[0]} )
// prints [[32701, First True], [32702, Second]]
}
You can reverse the result again to get the original relative order.
As mentioned by others, the use of listOf is sub-optimal, here is a cleaner version:
data class Item(val id: String, val text: String)
fun distinct(data : List<Item>) = data.reversed().distinctBy{it.id}
fun main() {
val data = listOf(
Item("32701", "First"),
Item("32702", "Second"),
Item("32702", "Second"),
Item("32701", "First True")
)
println(distinct(data) )
// [Item(id=32701, text=First True), Item(id=32702, text=Second)]
}

Kotlin - Find matching objects in array

Let's say I have an array of strings and I want to get a list with objects that match, such as:
var locales=Locale.getAvailableLocales()
val filtered = locales.filter { l-> l.language=="en" }
except, instead of a single value I want to compare it with another list, like:
val lang = listOf("en", "fr", "es")
How do I do that? I'm looking for a one-liner solution without any loops. Thanks!
Like this
var locales = Locale.getAvailableLocales()
val filtered = locales.filter { l -> lang.contains(l.language)}
As pointed out in comments, you can skip naming the parameter to the lambda, and use it keyword to have either of the following:
val filtered1 = locales.filter{ lang.contains(it.language) }
val filtered2 = locales.filter{ it.language in lang }
Just remember to have a suitable data structure for the languages, so that the contains() method has low time complexity like a Set.

Sorting a dictionary by key and converting it into an array

I have a dictionary of prices and quantities. I am getting updates on the price and values multiple times in a second so I don't want to store them in an array because dictionary are much faster.
let mainPriceValDict = [Double:Double]()
The data is coming in as an array of JSON so I am using codable to parse the JSON and put it into a dictionary. When I use the data, it needs to be sorted in ascending and/or descending order because I am looping through each price in order to get to a certain total quantity. The format that the array is in that I am looping through is as follows:
let loopingArray = [PriceQuantityEntry]()
struct PriceQuantityEntry {
let price : Double
let size : Double
}
I want to sort the prices which are the keys in the dictionary above and convert them into an array of PriceQuantityEntry. What is the best way to do this? In ascending and deciding order. I have tried first getting all the keys sorted and then grabbing associated values and putting them into the array in order but this seems like more processing than this task actually requires.
I think the best way to do this would be to put a custom initializer in the struct to convert the dictionary value to a value of type PriceQuantityEntry but I am not exactly sure how that would work with the sorting.
This is what I am currently doing to get it to work. I just feel like there is a more efficient way for it to be done. If you feel like I should keep the structure as an array instead of converting it to a dict, let me know.
loopingArray = self.mainPriceValDict.sorted { $0.0 < $1.0 }.map { PriceQuantityEntry(price: $0.0, size: $0.1) }
If you are getting a lot of updates to individual entries, both a dictionary and an array may cause memory copies of the whole memory structure every time an entry is changed.
I would suggest using objects (classes) instead of structures. This will allow you to use both an array and a dictionary to reference the object instances. The dictionary will provide direct access for updates and the array will allow sequential processing in forward or backward order.
[EDIT] Example:
class PriceQuantityEntry
{
static var all:[PriceQuantityEntry] = []
static var prices:[Double:PriceQuantityEntry] = [:]
var price : Double
var size : Double
init(price:Double, size:Double)
{
self.price = price
self.size = size
PriceQuantityEntry.all.append(self)
// PriceQuantityEntry.all.resort() // on demand or when new prices added
PriceQuantityEntry.prices[price] = self
}
class func update(price:Double, with size:Double)
{
if let instance = PriceQuantityEntry.prices[price]
{ instance.size = size }
else
{
let _ = PriceQuantityEntry(price:price, size:size)
PriceQuantityEntry.resort()
}
}
class func resort()
{
PriceQuantityEntry.all.sort{$0.price < $1.price}
}
}
// if adding multiple initial entries before updates ...
let _ = PriceQuantityEntry(price:1, size:3)
let _ = PriceQuantityEntry(price:1.25, size:2)
let _ = PriceQuantityEntry(price:0.95, size:1)
PriceQuantityEntry.resort()
// for updates ...
PriceQuantityEntry.update(price:1, with: 2)
// going throug list ...
var count:Double = 0
var total:Double = 0
var quantity:Double = 5
for entry in PriceQuantityEntry.all
{
total += min(entry.size,quantity-count) * entry.price
count = min(quantity,count + entry.size)
if count == quantity {break}
}

backpressure is not properly handled in akka-streams

I wrote a simple stream using akka-streams api assuming it will handle my source but unfortunately it doesn't. I am sure I am doing something wrong in my source. I simply created an iterator which generate very large number of elements assuming it won't matter because akka-streams api will take care of backpressure. What am I doing wrong, this is my iterator.
def createData(args: Array[String]): Iterator[TimeSeriesValue] = {
var data = new ListBuffer[TimeSeriesValue]()
for (i <- 1 to range) {
sessionId = UUID.randomUUID()
for (j <- 1 to countersPerSession) {
time = DateTime.now()
keyName = s"Encoder-${sessionId.toString}-Controller.CaptureFrameCount.$j"
for (k <- 1 to snapShotCount) {
time = time.plusSeconds(2)
fValue = new Random().nextLong()
data += TimeSeriesValue(sessionId, keyName, time, fValue)
totalRows += 1
}
}
}
data.iterator
}
The problem is primarily in the line
data += TimeSeriesValue(sessionId, keyName, time, fValue)
You are continuously adding to the ListBuffer with a "very large number of elements". This is chewing up all of your RAM. The data.iterator line is simply wrapping the massive ListBuffer blob inside of an iterator to provide each element one at a time, it's basically just a cast.
Your assumption that "it won't matter because ... of backpressure" is partially true that the akka Stream will process the TimeSeriesValue values reactively, but you are creating a large number of them even before you get to the Source constructor.
If you want this iterator to be "lazy", i.e. only produce values when needed and not consume memory, then make the following modifications (note: I broke apart the code to make it more readable):
def createTimeSeries(startTime: Time, snapShotCount : Int, sessionId : UUID, keyName : String) =
Iterator.range(1, snapShotCount)
.map(_ * 2)
.map(startTime plusSeconds _)
.map(t => TimeSeriesValue(sessionId, keyName, t, ThreadLocalRandom.current().nextLong()))
def sessionGenerator(countersPerSession : Int, sessionID : UUID) =
Iterator.range(1, countersPerSession)
.map(j => s"Encoder-${sessionId.toString}-Controller.CaptureFrameCount.$j")
.flatMap { keyName =>
createTimeSeries(DateTime.now(), snapShotCount, sessionID, keyName)
}
object UUIDIterator extends Iterator[UUID] {
def hasNext : Boolean = true
def next() : UUID = UUID.randomUUID()
}
def iterateOverIDs(range : Int) =
UUIDIterator.take(range)
.flatMap(sessionID => sessionGenerator(countersPerSession, sessionID))
Each one of the above functions returns an Iterator. Therefore, calling iterateOverIDs should be instantaneous because no work is immediately being done and de mimimis memory is being consumed. This iterator can then be passed into your Stream...

LINQ - Select the elements from array A depending on values on Array B

Let say we have two arrays:
DateTime[] wDates = new DateTime[20000];
double[] wValues = new double[20000];
Those two arrays are both sequentially ordered, that is given an int i, wValues[i] is for the date wDates[i].
Let us say we need to get the average value of wValues where the month of the date is January
using a standard loop this would be:
double wAvg = 0.0;
int wDataCount = 0;
for (int i=0; i < 20000; i++)
if (wDates[i].Month == 1)
{
wAvg += wValues[i];
wDataCount++;
}
if (wDataCount > 0)
wAvg /= wDataCount;
I am wondering how to do this in LINQ?
I could create a struct/class DateDouble that contains both values and then do something like:
List<DateDouble> wListData = new List<DateDouble>();
Add the items...
double wAvg = (from d in wListData
where d.Date.Month == 1
select d.Value).Average();
but creating thousands of DateDouble object would be a big memory overhead when done dozens of millions time a day. The same would happen with temporary objects and trying to use 'index' and joining the index on the arrays would create awful performances.
Is there a better way to achieve this in LINQ?
Thanks,
MM
Well, you can use the Zip operator to make things simpler:
var average = wDates.Zip(wValues, (date, value) => new { date, value })
.Where(pair => pair.date.Month == 1)
.Average(pair => pair.value);
That will still create one instance of the anonymous type per pair, but I would personally let that go and measure the performance before you assume it will be too expensive. Note that this will operate in a streaming fashion - so although it will generate a lot of garbage, the total memory required at any one time is small.
You can make it more efficient by creating your own pair struct... that would avoid creating extra objects, but it would be a bit more of a pain. Not too bad, though:
// The normal Tuple types are classes.
public struct TupleValue<T1, T2>
{
private readonly T1 item1;
private readonly T2 item2;
public T1 Item1 { get { return item1; } }
public T2 Item2 { get { return item2; } }
public TupleValue(T1 item1, T2 item2)
{
this.item1 = item1;
this.item2 = item2;
}
}
var average = wDates.Zip(wValues, (date, value) =>
new TupleValue<DateTime, double>(date, value))
.Where(pair => pair.Item1.Month == 1)
.Average(pair => pair.Item2);
I would only do this after proving that the first approach was two expensive though.
There is an overloaded version of IEnumerable.Where() extension method which also considers the index in the predicate.
double average = wValues.Where((d, i) => wDates[i].Month == 1).Average();
Something like:
double wAvg = wDates.Select((d,i) => new { Month = d.Month, Index = i })
.Where(x => x.Month == 1)
.Select(x => wValues[i])
.Average();
Anyway, in this case you will also be creating N instances of that anonymous type.

Resources