Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

public class Main {
    public static <E> E getSingleElement(List<E> list) {
        if (list == null) {
            return null;
        }

        int listSize = list.size();
        if (listSize > 1) {
            throw new IllegalArgumentException("Expected only one entry in the list.");
        }

        return listSize == 1 ? list.get(0) : null;
    }
}