Here you can find the source of sortedIfPossible(Collection
@SuppressWarnings("unchecked") public static <T> List<T> sortedIfPossible(Collection<T> collection)
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; public class Main { @SuppressWarnings("unchecked") public static <T> List<T> sortedIfPossible(Collection<T> collection) { List<T> result = new ArrayList<>(collection); try {//from w ww .j a v a 2s . c o m Collections.sort((List) result); } catch (ClassCastException e) { // unable to sort, just return the copy } catch (NullPointerException npe) { // this happens if there are null elements in the collection; just return the copy } return result; } }