Calculate different speeds between min and max speed with a stepsize public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh &lt;= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}


I have a minimum car speed and a maximum car speed. In this case 10 and 20. And I want every speed which is between minimum and maximum where the speed gets incremented by a step speed. 

I don't like my current version because it is large in my opinion. Is it possible to shorten the code to 1-2 liner?
First off, you should capture this functionality in a dedicated method, rather than in the class' main method.

Secondly, I'm not sure why you want an array of String. I don't know the context, but as you're already calculating with speeds, it seems likely you'll calculate more with them further on, and you'll want them to be numbers again.

Next, your method of doing this is appending everything in one giant String, and then splitting it again, after dealing with an unnecessary separator at the end. Looks like a lot of work for nothing. You can just add everything to an array directly (you'll have to precalculate its size beforehand though)

Java 8 has IntStream, which is ideal for this task. It enables the one-liner you're looking for.

private int[] allSpeeds(int lower, int upper, int step) {
    return IntStream.iterate(lower, i -&gt; i + step)
            .limit(Math.max(0, ((upper - lower) / step) + 1)) // limit to the needed size, ensure this value cannot be negative
            .toArray();
}