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 <T> T getSingle(List<T> collection) {
        boolean hasAnyElements = collection != null && !collection.isEmpty();
        if (!hasAnyElements) {
            throw new RuntimeException("Collection is null or empty");
        }

        boolean hasMultipleResults = collection.size() > 1;
        if (hasMultipleResults) {
            throw new RuntimeException("Collection contains multiple elements");
        }

        return collection.get(0);
    }
}