-
Notifications
You must be signed in to change notification settings - Fork 4
Java
Mingtao edited this page Sep 21, 2016
·
15 revisions
//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);
<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();
<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();
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
Sets.difference(...)
<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"));
//Call a restful service
String str = new RestTemplate().getForObject(Uri, String.class);
<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
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 |
<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). |
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;
}
}