Java tutorial
//package com.java2s; import java.util.Comparator; import java.util.List; import java.util.ListIterator; import java.util.function.BiPredicate; public class Main { public static <E> E setInSortedList(List<E> list, int index, E item, Comparator<? super E> comparator, BiPredicate<? super E, ? super E> distincter) { if (distincter != null) { ListIterator<E> iter = list.listIterator(); while (iter.hasNext()) { int currIndex = iter.nextIndex(); E currItem = iter.next(); if (index != currIndex && distincter.test(currItem, item)) { return null; } } } E previousItem = list.set(index, item); list.sort(comparator); return previousItem; } }