Skip to content
Mingtao edited this page Oct 19, 2016 · 15 revisions

Json

//Json string to List
List<Employee> list = new ObjectMapper().readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, Employee.class));
//JSON string to Object
Staff obj = new ObjectMapper().readValue(jsonInString, Staff.class);
//Object to JSON
new ObjectMapper().valueToTree(object);

immutable map

<dependency>
    <groupId>com.google.collections</groupId>
    <artifactId>google-collections</artifactId>
    <version>1.0-rc2</version>
</dependency>

private ImmutableMap<Integer, String> immutableMap = ImmutableMap.<Integer, String>builder()
        .put(1, "Dog")
        .put(2, "Cat")
        .build();

http async call

<dependency>
    <groupId>com.ning</groupId>
    <artifactId>async-http-client</artifactId>
    <version>1.9.8</version>
</dependency>

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Future<Response> f = asyncHttpClient.prepareGet(url).execute();

Sets

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

Sets.difference(...)

Unit Test

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.5</version>
</dependency>

import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class Test {
    @Mock
    private Shoe shoe;

    @Test
    public void hit14StandHard17() {
        when(shoe.deal()).thenReturn(new Card(Rank.FOUR), new Card(Rank.KING), new Card(Rank.THREE));
        ...
    }
}

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

assertThat(actualValue, is("expectedValue"));

Restful

//Call a restful service
String str = new RestTemplate().getForObject(Uri, String.class);

H2 Database

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.192</version>
</dependency>

Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:dbfolder/dbname", "sa", "");

http://www.h2database.com/html/cheatSheet.html

Bitwise operator

Normal Bitwise
y = x / 8 y = x >> 3
y = x * 64 y = x << 6
y = x * 2 y = x << 1
y = x & 3 y = x % 4

FastDateFormat/SimpleDateFormat(Not thread safe)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.2</version>
</dependency>

DateParser DATE_PARSER = FastDateFormat.getInstance("yyyy-MM-dd");
Parameter Explanation
yy Year
MM Month (01-12; JAN = 01).
MMM Abbreviated name of month.
dd Day of month (1-31).
hh Hour of day (1-12).
HH Hour of day (0-23).
mm Minute (0-59).
ss Second (0-59).

FieldUtils (Reflection)

import org.apache.commons.lang3.reflect.FieldUtils;

public BigDecimal getHours(String fieldName){
    try {
        return (BigDecimal)FieldUtils.readField(this, fieldName, true);
    } catch (IllegalAccessException e) {
        return null;
    }
}

Predicate

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

List<String> list = (List<String>) CollectionUtils.select(ListToFilter, new Predicate(){
    @Override
    public boolean evaluate(Object object) {
        return !((String)object).isEmpty();
    }
});

Joda

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

import org.joda.time.DateTime;

public static Date plusDays(Date date, int day){
    if (date == null){
        return null;
    }
    return new DateTime(date).plusDays(day).toDate();
}
Clone this wiki locally