Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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