The new, Java 8 way:

List<Integer> range = IntStream.range(1, 501).boxed().collect(Collectors.toList());
Answer from Norswap on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › listing numbers within a range in java
Listing Numbers Within a Range in Java | Baeldung
January 8, 2024 - The code above will generate a list containing numbers from start (inclusive) to end (exclusive). IntStream, introduced in JDK 8, can be used to generate numbers in a given range, alleviating the need for a for loop:
Top answer
1 of 7
10

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here.


if(list.size() == 0){
    return 0;
}
if(list.size() == 1){
    return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
    return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

2 of 7
11

Use this:

if (list.size() <= 1) {
    return list.size();
}

list.get(1) refers to the second element in the list, not the first. Either rename the variable or use list.get(0). With that change, you don't need to use a list size of 1 as a special case.


Only being able to do the job on ArrayList is quite useless. Your method can take an argument of type List instead.


No, it is no need to use else if in this case.

Rewriting with Iterable

You currently check the first item in the list twice. If your code would accept an Iterable<Integer>, you would support even more data types (You still support ArrayList also).

The only thing you need is the Iterator from the iterable. When you have that, you can first see if it contains at least one item. If it doesn't, return 0. Otherwise, you do similar to what you are already doing, but with different code.

public static int range(Iterable<Integer> numbers) {
    Iterator<Integer> iterator = numbers.iterator();
    if (!iterator.hasNext()) {
        return 0;
    }

    int firstElement = iterator.next();
    int max = firstElement;
    int min = firstElement;
    while (iterator.hasNext()) {
        int elementValue = iterator.next(i);
        if (max < elementValue) {
            max = elementValue;
        }
        if (elementValue < min) {
            min = elementValue;
        }
    }
    return (max - min) + 1;
}

Happy coding!

🌐
GitHub
github.com › glazedlists › glazedlists › blob › master › core › src › main › java › ca › odell › glazedlists › RangeList.java
glazedlists/core/src/main/java/ca/odell/glazedlists/RangeList.java at master · glazedlists/glazedlists
* <a href="https://glazedlists.dev.java.net/issues/show_bug.cgi?id=278">278</a> * </td></tr> * </table> * * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a> */ public class RangeList<E> extends TransformedList<E, E> { · /** the user-specified range of the source list to include */ private int desiredStart = 0; private int desiredEnd = -1; ·
Author   glazedlists
🌐
JTuts
jtuts.com › home › how to create a list of objects using a range of numbers in java 8
How to create a list of objects using a range of numbers in Java 8 - JTuts
April 21, 2017 - List<User> users = IntStream.range(0, 5) .mapToObj(i -> new User(i, USER_NAME_PREFIX + String.valueOf(i))) .collect(Collectors.toList()); ... blade blog blogger cassandra chrome css dokuwiki eclipse form git i18n ie8 immutable intellij-idea interview java java-config java8 javascript jconsole jsp junit laravel linux maven model monitoring mysql pseudo query rest spring spring-boot spring-data-jpa spring-mvc spring-security sql string sts ui validation web wiki windows xml
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
October 20, 2025 - Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list.
Top answer
1 of 4
5

One way you can remove duplicated code is to create a method:

public static int[] GetMaxMin (ArrayList <Integer> list)
{
    int max = list.get(0);
    int min = list.get(0);

    for (int i : list){
        if (i > max) {
            max = i;
        } else if (i < min) {
            min = i;
        }
    }
    int[] maxMin = {max, min};

    return maxMin;
}

This can then be called like this:

max = GetMaxMin(list)[0];
min = GetMaxMin(list)[1];

However, the above said, I would not recommend doing this because it has two purposes - to get the max value and the min value in the list. What I would recommend is to implement two methods - one to get the max value and one to get the min value:

public static int GetMax (ArrayList <Integer> list)
{
    int max = list.get(0);

    for (int i : list){
        if (i > max) {
            max = i;
        }
    }

    return max;
}

GetMin would be similar. Then, all you would need to do is this:

int max = GetMax(list1);
int min = GetMin(list1);
int max2 = GetMax(list2);
int min2 = GetMin(list2);

As you are calculating a range of values, though, you should just write a method to do that:

public static int CalculateRange (ArrayList <Integer> list)
{
    int max = list.get(0);
    int min = list.get(0);

    for (int i : list){
        if (i > max) {
            max = i;
        } else if (i < min) {
            min = i;
        }
    }

    return max - min;
}

As brought up in the comments, this will crash if there are 0 elements in the list. To prevent this, you should probably check this before you try to get the range, like this:

if (list.size() != 0) {
    System.out.printf("List 1 Range: %d\tList 2 Range: %d\n", CalculateRange(list1), CalculateRange(list2));
} else {
    System.out.println("There are no elements in this array");
}
2 of 4
4

Hosch250 and h.j.k. have the right idea here, to reduce the code to a function that returns the range (max - min) for each list. The function, according to Hosch250, will have the signature:

public static int CalculateRange (ArrayList <Integer> list)

I would recommend changing that to a more Java-standard capitalization of having a lower-case C in CalculateRange. i.e. calculateRange. Additionally, I would change the input type from ArrayList, to just Collection. There is no need to restrict the function to just one concrete type.

Finally, because you have the option, I would strongly recommend that you investigate the new Java 8 features which, in this case, would help a lot.

The idea would be to convert your Collection<Integer> to an int stream, and to then compute the IntSummaryStatistics you need.... something like:

public static int calculateRange(Collection<Integer> data) {
    IntSummaryStatistics stats = data.stream()
            .filter(d -> d != null)
            .mapToInt(d-> d.intValue())
            .summaryStatistics();
    return stats.count() == 0 ? 0 : stats.max() - stats.min();
}

Then, in your main method, I would also recommend the use of 'formatted' print, using the Format syntax

System.out.printf("List 1 Range: %d\tList 2 Range: %d\n",
        calculateRange(list1),
        calculateRange(list2));
Find elsewhere
Top answer
1 of 5
4

You could collect the numbers into a sorted set and then iterate over the numbers.

Quick and dirty example:

SortedSet<Integer> numbers = new TreeSet<Integer>();

numbers.add( 1 );
numbers.add( 2 );
numbers.add( 3 );
numbers.add( 6 );
numbers.add( 7 );
numbers.add( 10 );

Integer start = null;
Integer end = null;

for( Integer num : numbers ) {
  //initialize
  if( start == null || end == null ) {
    start = num;
    end = num;
  }
  //next number in range
  else if( end.equals( num - 1 ) ) {
    end = num;
  }
  //there's a gap
  else  {
    //range length 1
    if( start.equals( end )) {
      System.out.print(start + ",");
    }
    //range length 2
    else if ( start.equals( end - 1 )) {
      System.out.print(start + "," + end + ",");
    }
    //range lenth 2+
    else {
      System.out.print(start + "-" + end + ",");
    }

    start = num;
    end = num;
  }
}

if( start.equals( end )) {
  System.out.print(start);
}
else if ( start.equals( end - 1 )) {
  System.out.print(start + "," + end );
}
else {
  System.out.print(start + "-" + end);
}

Yields: 1-3,6,7,10

2 of 5
3

Apache Commons has the IntRange type that you can use. Unfortunately I didn't find a good corresponding set of utilities to create them. Here's the basic approach you could use:

//create a list of 1-integer ranges
List<IntRange> ranges = new LinkedList<IntRange>();
for ( int pageNum : pageNums ) {
    ranges.add(new IntRange(pageNum));
}

//sort the ranges
Collections.sort(ranges, new Comparator<IntRange>() {
    public int compare(IntRange a, IntRange b) {
       return Integer.valueOf(a.getMinimumInteger()).compareTo(b.getMinimumInteger());
    }
});

List<IntRange> output = new ArrayList<IntRange>();

if ( ranges.isEmpty() ) {
   return output;
}

//collapse consecutive ranges
IntRange range = ranges.remove(0);
while ( !ranges.isEmpty() ) {
   IntRange nextRange = ranges.remove(0);
   if ( range.getMaximumInteger() == nextRange.getMinimumInteger() - 1 ) {
      range = new IntRange(range.getMinimumInteger(), nextRange.getMaximumInteger());
   } else {
      output.add(range);
      range = nextRange;
   }
}
output.add(range);

Alternatively you could skip the first step and create the ranges directly from the sorted list of page numbers.

🌐
Baeldung
baeldung.com › home › java › java streams › get a range of items from a stream in java
Get a Range of Items from a Stream in Java | Baeldung
February 24, 2024 - To extract a range of items from a stream in Java, we have several approaches at our disposal. Let’s explore some of them. The skip() and limit() operations are often combined to extract a range of items from a stream.
🌐
CodersTea
coderstea.in › posts › how to generate list from 1 to n in java
How to Generate List from 1 to N in Java | CodersTea
October 21, 2025 - int n = 10; // 1 to 9 (n is excusive) List<Integer> list = IntStream.range(1, n) .boxed() .toList(); // 1 to 10 (n inclusive) List<Integer> list = IntStream.rangeClosed(1, n) .boxed() .toList(); You can also read Stream API: The Hero Without a Cape and Functional Interfaces in Java to know more about Stream API
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › interfaces › list.html
The List Interface (The Java™ Tutorials > Collections > Interfaces)
Range-view — The sublist method performs arbitrary range operations on the list. The Java platform contains two general-purpose List implementations.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-removerange-java-examples
Arraylist removeRange() Method in Java with Examples - GeeksforGeeks
August 6, 2025 - // Java program to demonstrate removeRange() method // with ArrayList of Integers import java.util.ArrayList; // custom subclass to access removeRange() class Custom extends ArrayList<Integer> { // Method to remove elements // from specified range public void removeRangeList(int s, int e) { removeRange(s, e); } } public class GFG { public static void main(String[] args) { // Creating an ArrayList Custom n = new Custom(); // Adding elements to // the ArrayList n.add(5); n.add(7); n.add(11); n.add(13); n.add(17); // Printing original list System.out.println("" + n); // Removing elements from ind
🌐
Coderanch
coderanch.com › t › 658900 › java › Array-range-numbers
Array range of numbers (Beginning Java forum at Coderanch)
Java7 is beyond its end of life and it is now nearer to Java9 than it was to the release of Java8.A Stream is an object which can iterate things without your having to write for do or while, and there is a specialised form (=subtype) called IntStream which iterates ints. That has two methods called rangeXXX and this rangeXXX method probably does what you want.
Top answer
1 of 4
4

What you are describing is a sparse matrix upon which you wish to do a range select.

I'm going to start out with no, neither a HashMap nor a LinkedHashMap will do what you want optimally. The reason for this to find the elements in a range HashMap is effectively walking an unordered list - O(n). The LinkedHashMap may be slightly more optimal than awful if the elements are put into the LinkedHashMap in sorted order and no new ones are added afterwards. Its still not an optimal approach because instead of O(n) you're getting O(n/m) (because you stop walking the list once you get past the range) which is still effectively O(n). You can't do a binary search on the LinkedHashMap list.

So, lets look at some other structures. What you are really after is the SortedMap interface. It allows you to quickly pull out the sub map from one key to another with SortedMap subMap(K fromKey, K toKey) which is what you want to do. Pick one of those classes and you will have what you are after.

You could then get the values for each of the submaps and do a boolean retainAll(Collection c) of one set to the other.

That's the easy answer. It is effectively the lists of lists approach for the sparse matrix. You could, however encode the matrix instead in some other format that gives you a more direct sub range option. This isn't something that is part of the Java standard library and you may find yourself going down this path for writing your own implementation in that case.

If you see the possibility in the future that this may be the case, you should consider writing your own interface for the sparse matrix and then implement a class behind it that does the lookups that you want. This way if you later decide that you need to change the implementation for some reason, its a matter of changing the implementation (a new class that wraps something else) rather than changing all the code to handle a different structure.

2 of 4
2

While your solution based on a treemap of treemaps will work, and is much better than the hashmap solution you started with, you may want to look into the possibility of using a QuadTree, which is a structure similar to a TreeMap but with a pair of coordinates as the key rather than just one. This makes selecting rectangular ranges from them particularly efficient. There are many implementations available for Java that can be found with a quick google.