Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedList : Collections.max() throwing NoSuchElementException

I am not iterating the LinkedList by any means like scanner or other methods, I am using Collections.max() to get maximum number from the LinkedList.

I have read on Stack Overflow that this exception is thrown due to iterator or scanner or tokenizer, but I am using none of them.

import java.io.*;
import java.util.*;

class TLG {
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        LinkedList<Integer> first = new LinkedList<Integer>();
        LinkedList<Integer> second = new LinkedList<Integer>();

        int cases = Integer.parseInt(br.readLine());

        for(int i=1;i<=cases;i++) {
            String score = br.readLine();
            int number1 = Integer.parseInt(score.split(" ")[0]); 
            int number2 = Integer.parseInt(score.split(" ")[1]); 
            int diff = number1 - number2;

            if(diff > 0){
                first.add(diff);    
            }
            else {
                second.add(java.lang.Math.abs(diff));    
            }
        }

        Integer max1 = Collections.max(first);  // Getting Exception here
        Integer max2 = Collections.max(second); // Getting Exception here

        if(max1 > max2) {
            System.out.println(1+" "+max1);
        }
        else {
            System.out.println(2+" "+max2);
        }
    }
}
like image 965
PankajKushwaha Avatar asked Oct 22 '14 18:10

PankajKushwaha


People also ask

How do I fix NoSuchElementException?

Solution. The solution to this​ exception is to check whether the next position of an iterable is filled or empty. You should only move to this position if the check returns that the position is not empty.

What is the cause of a NoSuchElementException?

Cause for NosuchElementException If you call the nextElement() method of the Enumeration class on an empty enumeration object or, if the current position is at the end of the Enumeration, a NosuchElementException is generated at run time.

What does NoSuchElementException mean in Java?

The NoSuchElementException is an unchecked exception in Java that can be thrown by various accessor methods to indicate that the element being requested does not exist. Since the NoSuchElementException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.


3 Answers

/**
 * Returns the maximum element of the given collection, according to the
 * <i>natural ordering</i> of its elements.  All elements in the
 * collection must implement the <tt>Comparable</tt> interface.
 * Furthermore, all elements in the collection must be <i>mutually
 * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
 * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
 * <tt>e2</tt> in the collection).<p>
 *
 * This method iterates over the entire collection, hence it requires
 * time proportional to the size of the collection.
 *
 * @param  coll the collection whose maximum element is to be determined.
 * @return the maximum element of the given collection, according
 *         to the <i>natural ordering</i> of its elements.
 * @throws ClassCastException if the collection contains elements that are
 *         not <i>mutually comparable</i> (for example, strings and
 *         integers).
 * @throws NoSuchElementException if the collection is empty. <---------------
 * @see Comparable
 */
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
like image 51
Eran Avatar answered Nov 15 '22 12:11

Eran


you are calling Collections.max() with empty list.

like image 44
Suresh Sajja Avatar answered Nov 15 '22 13:11

Suresh Sajja


You are not checking against Empty case: So Collections.max() will throw NoSuchElementException

First check if any of the Lists is Empty (then you know the max and which list is providing it)

    Integer max;
    int list;
    if (first.isEmpty()) {
        max = Collections.max(second);
        list = 2;
    } else if (second.isEmpty()) {
        max = Collections.max(first);
        list = 1;
    } else {
        Integer max1 = Collections.max(first);
        Integer max2 = Collections.max(second);
        if (max1 > max2) {
            max = max1;
            list = 1;
        } else {
            max = max2;
            list = 2;
        }
    }
    System.out.println(list + " " + max);
like image 20
gtgaxiola Avatar answered Nov 15 '22 12:11

gtgaxiola