Java examples for java.util:List Element
add Element Into a List in Order
//package com.java2s; import java.util.Collections; import java.util.List; public class Main { public static <T extends Comparable<T>> int addInOrder( final List<T> list, final T item) { final int insertAt; // The index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). final int index = Collections.binarySearch(list, item); if (index < 0) { insertAt = -(index + 1);/*from w w w.j av a 2s . c o m*/ } else { insertAt = index + 1; } list.add(insertAt, item); return insertAt; } }