-1

I have an ArrayList of Nodes. One of the fields of Node class is level.

I would like to find the level of the node that has the maximum level in the list.

This code gives me an NullPointerException...

private int findMaxLevel(ArrayList<Node> nodes)
{
    int level = 0;
    for(Node node : nodes)
    {
        if(node.getLevel() > level)
        {
            level = node.getLevel();
        }
    }
    return level;
}

I've tried several implementations of this page : Sorting an ArrayList of Contacts based on name? but I didn't found the solution.

EDIT :

Thank you for your suggestions, the level should never be null but I've forgot to add this.level = level in the Node class constructor....

Community
  • 1
  • 1
Grechka Vassili
  • 853
  • 4
  • 15
  • 32

4 Answers4

0

What is the null reference? What does the exceptions says? Anyway, the only reference in this piece of code is nodes so I assume it is it (getLevel() returns int type which is not nullable) Maybe you are sending the ArrayList<Node> before creating it? Maybe you are using the function like this somewhere:

findMaxLevel(null);

or

ArrayList<Node> nodes = null; // Something like that
findMaxLevel(nodes);
SapManTM
  • 126
  • 1
  • 10
0

Putting apart the NPE...

you can implement comparable in the Node class and use a Collections.max to get the max element in the list.

int max = Collections.max(a);
System.out.println(max);

but why overriding comparable:?

Collections.max() is implemented as:

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
    Iterator<? extends T> i = coll.iterator();
    T candidate = i.next();

    while (i.hasNext()) {
        T next = i.next();
        if (next.compareTo(candidate) > 0)
            candidate = next;
    }
    return candidate;
}

so compareTo will be called to find the max element

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Nope. Overriding `equals` does not define an order. You want to implement `Comparable` to make `Collections.max()` work! – Gyro Gearless May 09 '17 at 15:00
0

You might use stream api for that. I assume that your node class has an int field level

 class Node {
            private int level;
            public int getLevel() {
                return  level;
            }
            public void setLevel(int level) {
                this.level = level;
            }
 }

Now we need to find out maxim level in a List with Nodes:

Optional<Integer> maxLevel = nodes.stream().filter(Objects::nonNull)
                .map(Node::getLevel).max(Comparator.<Integer>naturalOrder());

I hope this helps.

Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
0

Keeping things simple with this one-liner:

    Node max = nodes.stream()
                    .filter(Objects::nonNull)
                    .max((Node node, Node t1) -> t1.level < node.level ? 1 : -1)
                    .get();

Make sure to check and see if the list is empty first or use Optional, otherwise you might get a NoSuchElementException.

Preston Garno
  • 1,175
  • 10
  • 33