How to get the maximum value from a list?

Hi, I need a function to get the maximum value from a list of integers.

The function receives an object.


public static ActGetListMaxElement mosGetListMaxElement(java.lang.Object inParamList) {

}

The problem I have is that I can't create a List <Integer> in order to use the Collections class.


//cast to recordList

RecordList recordList = (RecordList) inParamList;

//List of integers 

List<Integer> MyIntegerList = new ArrayList<Integer>();


//////////////////////////

How can I get the list of enterios from the recordList?

for(IRecord value: recordList.getData$RecordList()){

 MyIntegerList.add(value);  //I tried like this, but it generates error
}

//////////////////////


ActGetListMaxElement result = new ActGetListMaxElement();


result.outParamValorMaximo = ( Collections.max(MyIntegerList).intValue());

return result;*/



Hi German,


are you talking about an Outsystems Record List ?  

I don't really know what your setup is, and why there's all this code (i'm assuming java ?) in your post, but could you not go the low code way and first use a listsort on your list, and then take the value from the first record in your list ?


Maybe I misunderstood the requirement ?

Dorine

Dorine Boudry wrote:

Hi German,


are you talking about an Outsystems Record List ?  

I don't really know what your setup is, and why there's all this code (i'm assuming java ?) in your post, but could you not go the low code way and first use a listsort on your list, and then take the value from the first record in your list ?


Maybe I misunderstood the requirement ?

Dorine

Hi, thanks for the reply.
What I need is a general way to use objects that are casted as RecordList using an xif extension


I tried this, but object this error


outsystems.hubedition.runtimeplatform.db.BasicTypeList cannot be cast to outsystems.hubedition.runtimeplatform.db.RecordList


RecordList recordList = (RecordList) inParamLista;
 List<Integer> MyIntegerList= new ArrayList<Integer>();


for(IRecord valor: recordList.getData$RecordList()){
///Obtención de la variable de outsystems
 MyIntegerList.add(Integer.valueOf(recordList.getCurrent().toString()));
}

 ActGetListMaxElement result = new ActGetListMaxElement();
 
 result.outParamValorMaximo = ( Collections.max(MyIntegerList).intValue());

 return result;









Dorine Boudry wrote:

Hi German,


are you talking about an Outsystems Record List ?  

I don't really know what your setup is, and why there's all this code (i'm assuming java ?) in your post, but could you not go the low code way and first use a listsort on your list, and then take the value from the first record in your list ?


Maybe I misunderstood the requirement ?

Dorine

Just a quick note that using a sort with the sole purpose of finding the maximum value is not the right option, as it is not computational efficient. Most sorting algorithms run in O(n log(n)), while finding the maximum by simply looping over all values in the list can be computed in O(n), which is faster than any sorting algorithms.


As for your error, it seems as if java isn't recieving a RecordList but a BasicTypeList and therefore can't cast it to a RecordList.

Hi German,

What is the expected size of the list that you want to get the maximum of?

If the list is not that big Dorine's solution is I think a simplest coding solution.

Timothy is right that iterating a list using a foreach and finding the maximum that way in many cases is fast enough and possible faster than first sorting the list.

I woud suggest you measure the performance of these two lowcode solutions, and only if it is not performing the way you want consider other alternatives.

Regarding the error, Timothy is also right regarding the error. There is training material on how to create and iterate record lists in C# extensions.

https://www.outsystems.com/learn/lesson/1842/using-c-code-with-compound-types/?LearningPathId=0

https://www.outsystems.com/learn/lesson/1843/iterating-a-record-list-in-c/?LearningPathId=0


There is also a forge component that allows you to use LINQ to query a recordset:

https://www.outsystems.com/forge/component-overview/1293/dynamic-linq

The advantage of this component is not performance but the flexibility.

Regards,

Daniel