Calculate the difference between the elements in a list So I have two lists which have the same size. One is called roadS and the second one is called simTime. My aim is to calculate the difference between two indexes which lie next to each other and divide them with the difference of the other list. On the picture above you see what I mean. 

This is my current function:

public List&lt;Double&gt; getSpeedS(List&lt;Double&gt; simTime, List&lt;Double&gt; roadS){
    List&lt;Double&gt;  speedS = new ArrayList&lt;&gt;();
    for(int idx = 1; idx &lt; roadS.size();idx++){
        double curSpeedS = (roadS.get(idx)-roadS.get(idx-1))/(simTime.get(idx)-simTime.get(idx-1));
        speedS.add(curSpeedS);
    } 
    return speedS;
}


Now although the function is quite short, I aim to find a better or faster solution.

For eg. in phyton you could you pantas library and do this:

player.df['speedS'] = player.df.roadS.diff().shift(-1) / player.df.simTime.diff().shift(-1)     


My aim is to solve this problem more efficient. Does Java or another library maybe provide such functions where I can for calculate the differences
Your implementation is good and easy to understand.

When using List&lt;Double&gt; for input data, be sure to use an efficient implementation. Otherwise you could get really bad read performance. E.g., when you access elements by index, ArrayList&lt;Double&gt; will be much faster than LinkedList&lt;Double&gt; (\$O(n)\$ vs. \$O(n^2)\$ for your getSpeedS).

If you must support arbitrary types of List&lt;Double&gt; you would probably better off when you use Iterators instead of element access by index.

If you use an ArrayList and know its final size in advance, always construct it with the capacity needed (new ArrayList&lt;&gt;(size)). This avoids reallocation and copying of the internal array that holds the elements while adding elements.

If you need maximum performance, then use arrays (double[]) instead of lists. For large data sets they can speed up the creation of the input in-memory objects by factors greater than two. Additionally they may give you a significant advantage in read performance due to better utilization of processor caches compared to list implementations.