Java examples for java.util:List Element
Move an object in List down
//package com.book2s; import java.util.ArrayList; import java.util.List; import java.util.function.Function; public class Main { /**/*www .j av a 2s . c om*/ * Move an object in List down * * @param list * @param key * @param keyMapper * @return */ public static <T, K> boolean moveDown(List<T> list, K key, Function<T, K> keyMapper, int n) { ArrayList<T> newList = new ArrayList<T>(); boolean changed = false; int start = list.size() - 1; for (int i = start; i >= 0; i--) { T item = list.get(i); if (i != start && key.equals(keyMapper.apply(item))) { int posi = n; if (newList.size() < posi) posi = newList.size(); newList.add(posi, item); changed = true; } else newList.add(0, item); } if (changed) { list.clear(); list.addAll(newList); return true; } return false; } public static <T, K> boolean moveDown(List<T> list, K key, Function<T, K> keyMapper) { return moveDown(list, key, keyMapper, 1); } }