Here you can find the source of max(List
Parameter | Description |
---|---|
list | a parameter |
maximumSize | a parameter |
public static <E> List<E> max(List<E> list, int maximumSize)
//package com.java2s; /******************************************************************************* * Copyright 2011 Danny Kunz//ww w . ja va2 s . c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.util.ArrayList; import java.util.List; public class Main { /** * Returns a sublist of the given {@link List} with the maximum given size. There will always a new {@link List} instance be * returned, but empty. If the given {@link List} has less elements only these are returned and the returned {@link List} will * not be filled with additional null references. * * @param list * @param maximumSize * @return */ public static <E> List<E> max(List<E> list, int maximumSize) { // final List<E> retlist = new ArrayList<E>(); // if (list != null && maximumSize >= 0) { retlist.addAll(list.subList(0, Math.min(list.size(), maximumSize))); } // return retlist; } /** * Adds all the elements from the {@link Iterable} to the given {@link List} instance. If the given {@link List} instance is * null a new {@link ArrayList} is created. <br> * If null is given as {@link Iterable} nothing will be added to the {@link List}. * * @param list * @param iterable * @return given {@link List} instance or a new {@link ArrayList} if null is given */ @SuppressWarnings("unchecked") public static <E> List<E> addAll(List<? extends E> list, Iterable<? extends E> iterable) { final List<E> retlist = (List<E>) (list != null ? list : new ArrayList<E>()); if (iterable != null) { for (E element : iterable) { retlist.add(element); } } return retlist; } /** * Similar to {@link #add(List, Object...)} * * @param list * @param elements * @return */ public static <E> List<E> addAll(List<? extends E> list, E... elements) { return add(list, elements); } /** * Similar to {@link #add(List, int, Object...)} * * @param list * @param index * @param elements * @return */ public static <E> List<E> addAll(List<? extends E> list, int index, E... elements) { return add(list, index, elements); } /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally the further given element. <br> * <br> * This function will return always an instance, even if the given list is null. * * @param list * @param element * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // retlist.add(element); // return retlist; } /** * Similar to {@link #add(List, Object)} allowing to specify an index position * * @param list * @param index * @param element * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // retlist.add(index, element); // return retlist; } /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally all further given elements. <br> * <br> * This function will return always a instance, even if the given list is null. * * @param list * @param elements * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // if (elements != null) { for (E element : elements) { retlist.add(element); } } // return retlist; } /** * Similar to {@link #add(List, Object...)} allowing to specify an index position * * @param list * @param index * @param elements * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // int relative = 0; if (elements != null) { for (E element : elements) { retlist.add(index + relative, element); relative++; } } // return retlist; } }