Java List Move Item moveBefore(List list, T element, T referenceElement)

Here you can find the source of moveBefore(List list, T element, T referenceElement)

Description

Moves the given element before the reference element.

License

Open Source License

Parameter

Parameter Description
list a parameter
element a parameter
referenceElement a parameter

Return

Returns true if the element has been moved or inserted. Otherwise false.

Declaration

private static <T> boolean moveBefore(List<T> list, T element, T referenceElement) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* w  ww  . j  ava  2s. c  o  m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Moves the given element before the reference element. Both are expected to be part of the given list. If the
     * reference element is not in the list, the list remains untouched. If the element to move is not part of the list,
     * it is added before the reference element.
     * 
     * @param list
     * @param element
     * @param referenceElement
     * @return Returns <code>true</code> if the element has been moved or inserted. Otherwise <code>false</code>.
     * @since 3.8.2
     */
    private static <T> boolean moveBefore(List<T> list, T element, T referenceElement) {
        int index = list.indexOf(referenceElement);
        if (index != -1) {
            list.remove(element);
            list.add(index, element);
            return true;
        }
        return false;
    }
}

Related

  1. getRemoveAll(List list1, Collection list2)
  2. minus(List initialList, List elementsToRemove)
  3. move(List aList, int anIndex1, int anIndex2)
  4. move(List collection, int indexToMoveFrom, int indexToMoveAt)
  5. moveBackward(final List list, final int[] indices)
  6. moved(List old, List nu, Collection added, Collection removed)
  7. moveDownItems(List list, int[] positions)
  8. moveElementInList(List list, int targetIndex, int sourceIndex)
  9. moveElementToIndex( List list, ELEMENT fromElement, ELEMENT toElement)

  10. HOME | Copyright © www.java2s.com 2016