Print all properties of an Object with name/value
public void printObjectFields(Object object) { |
Compare two objects
Compare every filed of two objects by using CompareToBuilder
…
private int compareBrand(Brand oldBrand, Brand newBrand) { |
URL encoding of query string parameter
Either using java.net.URLEncoder
:
String str = "a=b+c"; |
OR using Regex:
String str = "a=b+c"; |
Useful link: What every web developer must know about URL encoding
Base64 data encoding | decoding
import java.util.Base64; |
Scrambling a string (scrambled email)
Recently, I need a method to generate a fake email address with existing user email…
1. Scramble a input string by shuffling all charaters with equal probablities
2. Scrambling by appending some random letters and numbers
3. First character should be a letter
Here is the code:
public static String scrambleString(Random random, String str) |
A simple way to swap values
int swap(int a, int b) { |
This method is a bit tricky. It relies on the fact that a
will pass into swap()
before b
is assigned to a
. Then, b
is returned and assigned to a
.
This method can be generic and swap any number of objects of the same type:
int swap(int a, int b, int c) { |
Convert
String[]
toList<String>
In order to use some util method like add(), remove(), contains()…
List<String> newCurrencies = new ArrayList(Arrays.asList(currencies)); |
Note: If initializing it in the following way, the size of newCurrencies
will be fixed, so it cannot use some of the util methods.
E.g. List<String> newCurrencies = Arrays.asList(currencies);
Null-safe comparator
It’s case-insensitive with assumption of emptyString == null
.
public static int nullSafeStringComparator(final Object str1, final Object str2) { |