Example usage for java.util Collections replaceAll

List of usage examples for java.util Collections replaceAll

Introduction

In this page you can find the example usage for java.util Collections replaceAll.

Prototype

public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) 

Source Link

Document

Replaces all occurrences of one specified value in a list with another.

Usage

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();

    v.add("A");//w  w  w . ja v  a2 s.  c  om
    v.add("B");
    v.add("A");
    v.add("C");
    v.add("D");

    System.out.println(v);
    Collections.replaceAll(v, "A", "Replace All");
    System.out.println(v);
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("A");
    arrayList.add("C");
    arrayList.add("D");

    System.out.println(arrayList);

    Collections.replaceAll(arrayList, "A", "Replace All");

    System.out.println(arrayList);
}

From source file:Main.java

public static void main(String[] args) {
    // create vector
    List<String> vector = new Vector<String>();

    // populate the vector
    vector.add("R");
    vector.add("B");
    vector.add("R");
    vector.add("java2s.com");

    System.out.println("Initial values are :" + vector);

    Collections.replaceAll(vector, "R", "java2s.com");

    System.out.println("Value after replace :" + vector);
}

From source file:Utilities.java

public static void main(String[] args) {
    List list = Arrays.asList("one Two three Four five six one".split(" "));
    System.out.println(list);/*from   w  w  w .j a  va 2 s .  c  o  m*/
    System.out.println("max: " + Collections.max(list));
    System.out.println("min: " + Collections.min(list));
    AlphabeticComparator comp = new AlphabeticComparator();
    System.out.println("max w/ comparator: " + Collections.max(list, comp));
    System.out.println("min w/ comparator: " + Collections.min(list, comp));
    List sublist = Arrays.asList("Four five six".split(" "));
    System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
    System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
    Collections.replaceAll(list, "one", "Yo");
    System.out.println("replaceAll: " + list);
    Collections.reverse(list);
    System.out.println("reverse: " + list);
    Collections.rotate(list, 3);
    System.out.println("rotate: " + list);
    List source = Arrays.asList("in the matrix".split(" "));
    Collections.copy(list, source);
    System.out.println("copy: " + list);
    Collections.swap(list, 0, list.size() - 1);
    System.out.println("swap: " + list);
    Collections.fill(list, "pop");
    System.out.println("fill: " + list);
    List dups = Collections.nCopies(3, "snap");
    System.out.println("dups: " + dups);
    // Getting an old-style Enumeration:
    Enumeration e = Collections.enumeration(dups);
    Vector v = new Vector();
    while (e.hasMoreElements())
        v.addElement(e.nextElement());
    // Converting an old-style Vector
    // to a List via an Enumeration:
    ArrayList arrayList = Collections.list(v.elements());
    System.out.println("arrayList: " + arrayList);

}

From source file:Main.java

public static <T extends Object> boolean replaceAll(final List<T> list, final T element0, final T element1) {
    return Collections.replaceAll(list, element0, element1);
}

From source file:de.micromata.tpsb.doc.parser.MethodInfo.java

public void replaceTestStep(TestStepInfo oldTs, TestStepInfo newTs) {
    Collections.replaceAll(getTestSteps(), oldTs, newTs);
    setDirty();
}

From source file:org.bonitasoft.web.designer.model.widget.Widget.java

public void replaceProperty(Property oldProperty, Property newProperty) {
    Collections.replaceAll(properties, oldProperty, newProperty);
}

From source file:edu.sabanciuniv.sentilab.sare.models.setcover.DocumentSetCover.java

/**
 * Replaces a given document with another document.
 * @param original the original document to replace.
 * @param replacement the new document to replace with.
 * @return {@code true} if the document was replaced.
 *///w w  w .j  a  va  2 s .  c o m
public boolean replaceDocuments(SetCoverDocument original, SetCoverDocument replacement) {
    if (this.documents == null) {
        return false;
    }

    boolean replaced = Collections.replaceAll(this.documents, original, replacement);
    if (replaced) {
        replacement.setStore(this);
    }

    return replaced;
}

From source file:com.formkiq.core.service.ArchiveServiceImpl.java

/**
 * Reset {@link FormJSON} UUID.//  w w w.  java 2 s. c om
 * @param archive {@link ArchiveDTO}
 * @param form {@link FormJSON}
 * @param map {@link Map}
 */
private void resetFormUUID(final ArchiveDTO archive, final FormJSON form, final Map<String, String> map) {

    String oldUUID = form.getUUID();
    String newUUID = map.get(oldUUID);

    Workflow workflow = archive.getWorkflow();
    archive.removeForm(form);

    form.setSourceFormUUID(oldUUID);
    form.setUUID(newUUID);

    archive.addForm(form);

    form.setParentUUID(workflow.getParentUUID());

    Collections.replaceAll(workflow.getSteps(), oldUUID, newUUID);
    Collections.replaceAll(workflow.getPrintsteps(), oldUUID, newUUID);

    if (!CollectionUtils.isEmpty(workflow.getOutputs())) {
        resetWorkflowOutput(workflow, form, oldUUID, newUUID);
    }
}

From source file:com.calc.BracerParser.java

/**
 * Evaluates once parsed math expression with "var" variable included
 *
 * @param variableValue User-specified <code>Double</code> value
 * @return <code>String</code> representation of the result
 * @throws <code>ParseException</code> if the input expression is not
 *                                     correct
 * @since 3.0//from  w ww  .ja v  a2 s  . c o  m
 */
public String evaluate(double variableValue) throws ParseException {
    /* check if is there something to evaluate */
    if (stackRPN.empty()) {
        return "";
    }

    /* clean answer stack */
    stackAnswer.clear();

    /* get the clone of the RPN stack for further evaluating */
    @SuppressWarnings("unchecked")
    Stack<String> stackRPN = (Stack<String>) this.stackRPN.clone();

    /* enroll the variable value into expression */
    Collections.replaceAll(stackRPN, VARIABLE, Double.toString(variableValue));

    /* evaluating the RPN expression */
    while (!stackRPN.empty()) {
        String token = stackRPN.pop();
        if (isNumber(token)) {
            stackAnswer.push(token);
        } else if (isOperator(token)) {
            Complex a = complexFormat.parse(stackAnswer.pop());
            Complex b = complexFormat.parse(stackAnswer.pop());
            boolean aBoolean = a.getReal() == 1.0;
            boolean bBoolean = b.getReal() == 1.0;
            switch (token) {
            case "+":
                stackAnswer.push(complexFormat.format(b.add(a)));
                break;
            case "-":
                stackAnswer.push(complexFormat.format(b.subtract(a)));
                break;
            case "*":
                stackAnswer.push(complexFormat.format(b.multiply(a)));
                break;
            case "/":
                stackAnswer.push(complexFormat.format(b.divide(a)));
                break;
            case "|":
                stackAnswer.push(String.valueOf(aBoolean || bBoolean ? "1" : "0"));
                break;
            case "&":
                stackAnswer.push(String.valueOf(aBoolean && bBoolean ? "1" : "0"));
                break;
            }
        } else if (isFunction(token)) {
            Complex a = complexFormat.parse(stackAnswer.pop());
            boolean aBoolean = a.getReal() == 1.0;
            switch (token) {
            case "fat":
                stackAnswer.push(complexFormat.format(a.abs()));
                break;
            case "fib":
                stackAnswer.push(complexFormat.format(a.acos()));
                break;
            case "arg":
                stackAnswer.push(complexFormat.format(a.getArgument()));
                break;
            case "asin":
                stackAnswer.push(complexFormat.format(a.asin()));
                break;
            case "atan":
                stackAnswer.push(complexFormat.format(a.atan()));
                break;
            case "conj":
                stackAnswer.push(complexFormat.format(a.conjugate()));
                break;
            case "cos":
                stackAnswer.push(complexFormat.format(a.cos()));
                break;
            case "cosh":
                stackAnswer.push(complexFormat.format(a.cosh()));
                break;
            case "exp":
                stackAnswer.push(complexFormat.format(a.exp()));
                break;
            case "imag":
                stackAnswer.push(complexFormat.format(a.getImaginary()));
                break;
            case "log":
                stackAnswer.push(complexFormat.format(a.log()));
                break;
            case "neg":
                stackAnswer.push(complexFormat.format(a.negate()));
                break;
            case "real":
                stackAnswer.push(complexFormat.format(a.getReal()));
                break;
            case "sin":
                stackAnswer.push(complexFormat.format(a.sin()));
                break;
            case "sinh":
                stackAnswer.push(complexFormat.format(a.sinh()));
                break;
            case "sqrt":
                stackAnswer.push(complexFormat.format(a.sqrt()));
                break;
            case "tan":
                stackAnswer.push(complexFormat.format(a.tan()));
                break;
            case "tanh":
                stackAnswer.push(complexFormat.format(a.tanh()));
                break;
            case "pow":
                Complex b = complexFormat.parse(stackAnswer.pop());
                stackAnswer.push(complexFormat.format(b.pow(a)));
                break;
            case "not":
                stackAnswer.push(String.valueOf(!aBoolean ? "1" : "0"));
                break;
            }
        }
    }

    if (stackAnswer.size() > 1) {
        throw new ParseException("Some operator is missing", 0);
    }

    return stackAnswer.pop();
}