Here you can find the source of replaceOrAdd(List
public static <T> List<T> replaceOrAdd(List<T> list, T object)
//package com.java2s; //License from project: Open Source License import java.util.List; public class Main { public static <T> List<T> replaceOrAdd(List<T> list, T object) { if (list == null || object == null) return list; int i = list.indexOf(object); if (i != -1) { list.remove(object);// ww w . j a v a 2 s .c om list.add(i, object); } else { list.add(object); } return list; } }