Java examples for java.util:Collection Element
add Element Before And After an Element in Collection
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] argv) { Collection collection = java.util.Arrays.asList("asdf", "java2s.com"); String toAddBefore = "java2s.com"; String toAddAfter = "java2s.com"; System.out.println(addElementBeforeAndAfter(collection, toAddBefore, toAddAfter)); }// w ww.ja va 2 s . c om public static Collection<String> addElementBeforeAndAfter( Collection<?> collection, String toAddBefore, String toAddAfter) { if (collection == null || collection.isEmpty()) { return Collections.emptyList(); } List<String> result = new ArrayList<>(collection.size()); for (Object o : collection) { StringBuilder stringBuilder = new StringBuilder(o.toString() .length() + toAddBefore.length() + toAddAfter.length()); stringBuilder.append(toAddBefore); stringBuilder.append(o.toString().trim()); stringBuilder.append(toAddAfter); result.add(stringBuilder.toString()); } return result; } }