-
Notifications
You must be signed in to change notification settings - Fork 4
Java
Mingtao edited this page Aug 23, 2019
·
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(...)
List<String> list = Lists.newArrayList();
<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;
@Spy private Shoe shoe2 = new Shoe();
@Test
public void hit14StandHard17() {
when(shoe.deal(anyLong(), any(Date.class))).thenReturn(new Card(Rank.FOUR), new Card(Rank.KING), new Card(Rank.THREE)); //This syntax works for Mock
doReturn(new Card(Rank.FOUR), new Card(Rank.KING), new Card(Rank.THREE)).when(shoe2).deal(anyLong(), any(Date.class)); //This syntax works for Spy
}
}
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;
}
}
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();
}
});
<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();
}
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
Iterables.getFirst(list, null);
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>2.2.4</version>
</dependency>
Joiner.on(",").skipNulls().join(Arrays.asList("a", "b", null));
Splitter.on(',').trimResults().omitEmptyStrings().split(str)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
@Getter @Setter @RequiredArgsConstructor @ToString
public class User {
private final String name;
private int age;
}
List<Student> list = Lists.newArrayList(student1, student2);
List<String> collect = list.stream().map(x -> x.getBooks()) // Stream<Set<String>>
.flatMap(x -> x.stream()) //Stream<String>
.distinct()
.collect(Collectors.toList());
public final class SingletonDemo {
private static volatile SingletonDemo instance;
private SingletonDemo() { }
public static SingletonDemo getInstance() {
if (instance == null ) {
synchronized (SingletonDemo.class) {
if (instance == null) {
instance = new SingletonDemo();
}
}
}
return instance;
}
}