Java tutorial
//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; } }