Java tutorial
//package com.java2s; import com.google.common.collect.ImmutableSortedSet; import javax.annotation.Nonnull; import java.util.*; public class Main { @Nonnull private static <T> SortedSet<? extends T> toSortedSet(@Nonnull Comparator<? super T> elementComparator, @Nonnull Collection<? extends T> collection) { if (collection instanceof SortedSet) { SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) collection; Comparator<?> comparator = sortedSet.comparator(); if (comparator != null && comparator.equals(elementComparator)) { return sortedSet; } } return ImmutableSortedSet.copyOf(elementComparator, collection); } }