Share
Embed
Transcript
19 slides
-
public int hashCode()
{ int result = super.hashCode(); result = 31 * result + (amount != null ? amount.hashcode() : 0); result = 31 * result + (uuid != null ? uuid.hashcode() : 0); result = 31 * result + (channel != null ? channel.hashcode() : 0); result = 31 * result + (type != null ? type.hashcode() : 0); return result; } public int hashCode() { return Objects.hashCode(amount, uuid, channel, type); } -
public
Period(Date start, Date end) { if (start.after(end)) { throw new IllegralArgumentException("Start cannot be after end"); } this.start = start; this.end = end; } public Public(Date start, Date end) { checkArguments(start.after(end), "Start cannot be after end"); this.start = start; this.end = end; } -
public
Period(Date start, Date end) { if (start == null || end == null) { throw new NullPointerException("Dates cannot be null"); } if (start.after(end)) { throw new IllegralArgumentException("Start can’t be after end"); } this.start = start; this.end = end; } public Period(Date start, Date end) { this.start = checkNotNull(start, "Start cannot be null"); this.end = checkNotNull(end, "End cannot be null"); checkArgument(start.after(end), "Start cannot be after end"); } -
List<SomeJavaObject> list = new ArrayList<SomeJavaObject>();
list.add(firstObj); list.add(secondObj); List<SomeJavaObject> list = newArrayList(firstObj, secondObj); -
BiMap<String, String> biMap = HashBiMap.create(); biMap.put("google",
"guava"); biMap.put("apache", "commons"); assertEquals("guava", biMap.get("google")); assertEquals("google", biMap.inverse().get("guava")); -
Map<String, List<String>>
map = new HashMap<String, List<String>>(); if (map.get(key) == null) { map.get(key) = new ArrayList<String>(); } map.get(key).add(value); Multimap<String, String> map = HashMultimap.create(); map.put(key, value); assertTrue(map.get(key) instanceof Collection); -
List<String> strings
= newArrayList(“a”, “b”, “c”); List<String> unmodifiableStrings = Collections.unmodifiableList(strings); strings.add(“d”); assertEquals(4, unmodifiableStrings()); @Test(expected = UnsupportedOperationException.class) public void immutableList_addItem_throwException() { List<Integer> ints = ImmutableList.of(1, 2, 3); ints.add(4) } -
Collection<String> strings = newArrayList(); for
(String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); } } Iterable<String> strings = Splitter.on(",") .omitEmptyStrings() .trimResults() .split("abc,def,,gh, ijk "); -
Collection<String> strings = newArrayList(); for
(String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); } } Splitter splitter = Splitter.on(",") .omitEmptyStrings() .trimResults(); Iterable<String> strings = splitter.split("abc,def,,gh, ijk "); Iterable<String> strings2 = splitter.split("lmno,, pqrstu,vwxyz "); -
List<String> strings = newArrayList("Atomicity", "Consistency",
"Isolation", null, "Durability"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < strings.size(); i++) { String s = strings.get(i); if (s == null) { continue; } sb.append(s); if (i < strings.size() - 1) { sb.append(", "); } } String s = Joiner.on(", ") .skipNulls() .join ("Atomicity", "Consistency", "Isolation", null, "Durability"); -
Set<Integer> acceptableMarks = newHashSet(); for
(int i = 60; i <= 100; i++) { acceptableMarks.add(i); } boolean examPassed = acceptableMarks.contains(mark); Range<Integer> acceptableMarks = Ranges.closed(60, 100); // [60, 100] boolean examPassed = acceptableMarks.contains(mark);