Here you can find the source of listToItemOrNull(List
null
if the list has no item.
Parameter | Description |
---|---|
list | the list to transform. May be <code>null</code>. |
null
.
public static <T> T listToItemOrNull(List<T> list)
//package com.java2s; //License from project: Open Source License import java.util.List; public class Main { /**//from ww w . ja v a2 s. com * Transforms a list to a single item or to a <code>null</code> if the list has no item. * <p> * Throws an {@link IllegalArgumentException} if the list has more than one item. * * @param list the list to transform. May be <code>null</code>. * @return the single list item or <code>null</code>. */ public static <T> T listToItemOrNull(List<T> list) { if (list == null) { return null; } else { switch (list.size()) { case 0: return null; case 1: return list.get(0); default: throw new IllegalArgumentException("List has more than one item: " + list); } } } }