Skip to content
Mingtao edited this page Aug 8, 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>

@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

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).
Clone this wiki locally