Here you can find the source of swapItems(final List
Parameter | Description |
---|---|
data | a parameter |
from | a parameter |
to | a parameter |
public static <T> void swapItems(final List<T> data, int from, int to)
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.List; import java.util.Map; public class Main { /**/*from w ww.ja v a2 s .c o m*/ * Swaps 2 items * * @param data * @param from * @param to */ public static <T> void swapItems(final List<T> data, int from, int to) { if (from == to) return; if (!hasElements(data)) return; if (from < 0 || from >= data.size()) { throw new IllegalArgumentException("'from' must be within 0 to n-1"); } if (to < 0 || to >= data.size()) { throw new IllegalArgumentException("'to' must be within 0 to n-1"); } T temp = data.get(from); data.set(from, data.get(to)); data.set(to, temp); } /** * Checks whether the COLLECTION is not NULL and has at least one element. * * @param <T> * the generic type * @param collection * the collection * @return true, if successful */ public static <T> boolean hasElements(Collection<T> collection) { return (null != collection && collection.size() > 0); } /** * Checks whether the MAP is not NULL and has at least one element. * * @param <T> * the generic type * @param <V> * the value type * @param map * the map * @return true, if successful */ public static <T, V> boolean hasElements(Map<T, V> map) { return (null != map && map.size() > 0); } }