java string explode and implode

Some nice java function to explode string into array or implode array into string as equivalent to PHP implode and explode function. Explode Convert string to array of tokens. Function #1 – Explode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //explode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String[] explodeStringUsingStringUtils(String stringToExplode,String separator){ return StringUtils.splitPreserveAllTokens(stringToExplode, separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingStringUtils(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Function #2 – Explode using core java.lang //explode function #2 //using core java lang public static String[] explodeStringUsingCoreJava(String stringToExplode,String separator){ return stringToExplode.split(separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingCoreJava(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Implode Concatenate/merge array of string into a single continuous string. Function #1 – Implode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //implode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String implodeArrayUsingStringUtils(String[] arrayToImplode,String separator){ return StringUtils.join(arrayToImplode, separator); } ... //usage String[] arrExploded = {"Denny","de","Web","Monster"}; String implodeString = implodeArrayUsingStringUtils(arrExploded,"+"); /* * String implodeString will contain * "Denny+de+Web+Monster" */ Function #2 – Implode using for loop //implode function #2 //using for loop public static String implodeArrayUsingForLoop(String[] arrayToImplode,String separator){ if (arrayToImplode.length == 0) { //empty array return empty string return ""; } if(arrayToImplode.length < 2){ //only 1 item return arrayToImplode[0]; } StringBuffer...

Prevent / Solve java.util.ConcurrentModificationException

Your program throws ConcurrentModificationException while running and how can you prevent / solve it It happens when your program removing an item in an ArrayList, Collection or List while looping (iterating) over it. Example: for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { yourArrayList.remove(i); } } //Exception in thread "main" java.util.ConcurrentModificationException // at java.util.AbstractList$Itr.checkForComodification(Unknown Source) // at java.util.AbstractList$Itr.next(Unknown Source) To prevent this, there are several solutions depending on you program logic. Solution 1 – Using the java.util.Iterator: import java.util.Iterator; ... Iterator<String> iterator = yourArrayList.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); if (string.compareToIgnoreCase("apple") == 0) { iterator.remove(); } } Solution 2 – Using an temporary ArrayList to store items for removal: //Initialize an temporary array list with similar type ArrayList<String> itemToRemove = new ArrayList<String>(); //Search for items matching string "apple" for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { itemToRemove.add(yourArrayList.get(i)); } } //Remove items stored in itemToRemove ArrayList yourArrayList.removeAll(itemToRemove); Hope it...