Create list of objects with generated key value in Kotlin - list

New in Kotlin;
I need to get a list of HeightModel class objects from the function
class HeightModel(id: Int, height: Int)
I am using below code. but getting an empty list
var i = 1
var j = 145
val li = emptyList<HeightModel>()
while(i < 46 && j < 191){
li.toMutableList().add(HeightModel(i, j))
i ++
j ++
}
return li
I am getting an emptylist instead. My expected result shall be
listOf(HeightModel(1, 145), HeightModel(2, 146) .....HeightModel(46, 190))
Help is generously appreciated

The probles is that li.toMutableList() returns new instance of list and, thus li is still empty list.
Actually emptyList() will returns immutable empty list.
So the solutions actually is next:
var i = 1
var j = 145
val li = mutableListOf<HeightModel>()
while(i < 46 && j < 191){
li.add(HeightModel(i, j))
i ++
j ++
}
return li
or just val li = ArrayList<HeightModel>()

This is a more idiomatic Kotlin to achieve what you want:
data class HeightModel(val id: Int, val height: Int)
fun main() {
val ids = 1..45
val heights = 145..190
val heightModelList = (ids zip heights).map { HeightModel(it.first, it.second) }
}
If you would like to know more about the features used in this code, please read about Data Classes, Ranges and Progressions and zip function in Kotlin. If you want to code in Kotlin, it is useful to learn a more functional way of handling data as I did with the map function above.

Related

Fastest way to remove first n elements from MutableList

I am programming in Kotlin and have a MutableList from which I would like to remove the first n elements from that specific list instance. This means that functions like MutableList.drop(n) are out of the question.
One solution would of course be to loop and call MutableList.removeFirst() n times, but this feels inefficient, being O(n). Another way would be to choose another data type, but I would prefer not to clutter my project by implementing my own data type for this, if I can avoid it.
Is there a faster way to do this with a MutableList? If not, is there another built-in data type that can achieve this in less than O(n)?
In my opinion the best way to achieve this is
abstract fun subList(fromIndex: Int, toIndex: Int): List<E>.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/sub-list.html
Under the hood it creates a new instance of list(SubList class for AbstractClass) with elements between the selected indexes.
Using:
val yourList = listOf<YourType>(...)
val yourNewList = yourList.subList(5, yourList.size)
// return list from 6th elem to last
One method which seems to be faster if n is sufficiently large seems to be the following:
Store the last listSize - n bytes to keep in a temporary list,
Clear original list instance
Add temporary list to original list
Here is a quick benchmark for some example values that happen to fit my use case:
val numRepetitions = 15_000
val listSize = 1_000
val maxRemove = listSize
val rnd0 = Random(0)
val rnd1 = Random(0)
// 1. Store the last `listSize - n` bytes to keep in a temporary list,
// 2. Clear original list
// 3. Add temporary list to original list
var accumulatedMsClearAddAll = 0L
for (i in 0 until numRepetitions) {
val l = Random.nextBytes(listSize).toMutableList()
val numRemove = rnd0.nextInt(maxRemove)
val numKeep = listSize - numRemove
val startTime = System.currentTimeMillis()
val expectedOutput = l.takeLast(numKeep)
l.clear()
l.addAll(expectedOutput)
val endTime = System.currentTimeMillis()
assert(l == expectedOutput)
accumulatedMsClearAddAll += endTime - startTime
}
// Iteratively remove the first byte `n` times.
var accumulatedMsIterative = 0L
for (i in 0 until numRepetitions) {
val numRemove = rnd1.nextInt(maxRemove)
val l = Random.nextBytes(listSize).toMutableList()
val expectedOutput = l.takeLast(listSize - numRemove)
val startTime = System.currentTimeMillis()
for (ii in 0 until numRemove) {
l.removeFirst()
}
val endTime = System.currentTimeMillis()
assert(l == expectedOutput)
accumulatedMsIterative += endTime - startTime
}
println("clear+addAll removal: $accumulatedMsClearAddAll ms")
println("Iterative removal: $accumulatedMsIterative ms")
Output:
Clear+addAll removal: 478 ms
Iterative removal: 12683 ms

Kotlin a list of random distinct numbers

I am creating a list of random numbers using the following approach
val randomList = List(4) { Random.nextInt(0, 100) }
However, this approach doesn't work as I want to avoid repetitions
One way is to shuffle a Range and take as many items as you want:
val randomList = (0..99).shuffled().take(4)
This is not so efficient if the range is big and you only need just a few numbers.In this case it's better to use a Set like this:
val s: MutableSet<Int> = mutableSetOf()
while (s.size < 4) { s.add((0..99).random()) }
val randomList = s.toList()
Create:
val list = (0 until 100).toMutableList()
val randList = mutableListOf<Int>()
for (i in 0 until 4) {
val uniqueRand = list.random()
randList.add(uniqueRand)
list.remove(uniqueRand)
}
One line approach to get a list of n distinct random elements. Random is not limited in any way.
val list = mutableSetOf<Int>().let { while (it.size() < n) it += Random.nextInt(0, 100) }.toList()

List of Tuples remove error

ExpenseL is my list of tuples and I try to remove from the list starting from start to stop but I just get this error: in removeFromAtoB
expenseL.pop(i)
TypeError: 'tuple' object cannot be interpreted as an integer
Please help me!!! :)
def removeFromAtoB():
aux = copy.deepcopy(expenseL)
print(expenseL, "\n")
start = int(input("Starting point: "))
stop = int(input("Ending point: "))
j = 0
for i in expenseL:
if j >= start and j <= stop:
expenseL.pop(i)
j += 1
print(expenseL)
You're iterating over your list of tuples:
for i in expenseL
That means i will be one of those tuples. Then you try to use it in list.pop:
expenseL.pop(i)
This won't work, because list.pop expects an index. Just enumerate your list:
for index, tpl in enumerate(expenseL):
...
expenseL.pop(index)
But this breaks, too, because the indices change when you remove an element. You could circumvent that by not increasing j in that case, but the simpler way is just assigning an empty list to the slice:
def removeFromAtoB():
start = int(input("Starting point: "))
stop = int(input("Ending point: "))
expenseL[start:stop+1] = []

How can I get subList of a List in Apex Salesforce?

I have got a list of SObjects having N number of items/sObjects
SObject[] sList = [sobject1, sboject2, sboject3, ........ , sobjectN]
How can I get just 10 items from the begining of the list
Thanks in advance!
After running this code newList contains only first 10 objects from sList.
SObject[] sList = [sobject1, sboject2, sboject3, ... , sobjectN];
List<SObject> newList = new List<SObject>();
for (Integer i = 0; i< 10; i++) {
newList.add(sList[i]);
}
For more info please reffer to List documentation
I also thought of using while and remove method:
SObject[] sList = [object1, object2,...];
if(sList.size() >= 10){
while(sList.size() > 10){
sList.remove(sList.size() - 1);
}
}
System.debug('values: '+sList);

split List<User> in multiple List<User> in c#

I want to split List of user generic List into its small list with each 5 records.
Ex
I have List: u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15.
so must be split into
List1:u1,u2,u3,u4,u5
List2:u6,u7,u8,u9,u10
List3:u11,u12,u13,u14,u15
Any direct method available or need programing logic in c# ?
You can group on the index:
List<List<User>> lists =
list
.Select((u, i) => new { List = i / 5, User = u })
.GroupBy(g => g.List, g => g.User)
.Select(g => g.ToList())
.ToList();
You can also use Range to make a loop and get a part of the list for each iteration:
List<List<User>> lists =
Enumerable.Range(0, (list.Count + 4) / 5)
.Select(n => list.Skip(n * 5).Take(5).ToList())
.ToList();
You can Use Skip(count) and Take(count) methods.
// These are your objects, put here your init code
var list = Enumerable.Range(1, 20).ToList();
var lists = new List<int>[(list.Count + 4) / 5]; // The array of lists you wanted
for (int i = 0; i < lists.Length; i++)
{
lists[i] = list.Skip(i * 5).Take(5).ToList();
}
The (list.Count + 4) / 5 is a method to round UP the division (if I have 6 elements in list, I want two sublists)
If you really need a List of List...
var lists = new List<List<int>>((list.Count + 4) / 5);
for (int i = 0; i < lists.Capacity; i++)
{
lists.Add(list.Skip(i * 5).Take(5).ToList());
}
I think this might be more efficient since you're not doing groupings, orderings and such. This is just a single iteration over the dataset.
var splitList= new List<IEnumerable<User>>();
List<User> currentList = null;
int count = 0;
foreach(var user in users)
{
if (0 == count% 5)
{
currentList = new List<User>(5);
returnValue.Add(currentList);
}
currentList.Add(key);
count++;
}
return returnValue;