3 ms·
I'm surprised he didn't mention AutoValue [1] or Lombok (using @Data and @Builder) in his section about data objects and builders. I prefer AutoValue, but both
by kilink 11y ago
I'm surprised he didn't mention AutoValue [1] or Lombok (using @Data and @Builder) in his section about data objects and builders. I prefer AutoValue, but both approaches cut down on a lot of the boilerplate (and they also generate sensible equals(), hashCode(), toString(), etc).
It's also kind of a nitpick, but I disagree with his preference for Guava's Maps.newHashMap() vs new HashMap<>(). I'm pretty sure the only reason those static methods exist is because pre-Java 7 didn't have the diamond operator.
[1] https://github.com/google/auto/tree/master/value https://github.com/google/auto/tree/master/value
- adpreese 11y agoHe mentions Lombok in the tools section. And Guava's collection creation methods are part of an intense desire to not use the new key word anywhere in application code.
- kilink 11y agoI realize he mentions Lombok, but he specifically bemoans the extra boilerplate with writing builders and data classes, but does not mention the @Data or @Builder annotations...
- kilink 11y agoAlso, if you look at the Javadocs for some of the creation methods, they explicitly state that they are for Java 6 and earlier, and that modern code should just use the constructor with the diamond syntax. http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Lists.html#newArrayList() http://docs.guava-libraries.googlecode.com/git/javadoc/com/g...
- Pyxl101 11y agoThe other replies kind of hit on this, but I'll expand on it by saying: The collection creation methods existed because Java could perform type inference on methods but could not on constructors: // Java 1.5 Map<String,Integer> map = new HashMap<String,Integer>(); // Guava Map<String,Integer> map = Maps.newHashMap(); // Java 1.8 diamond notation Map<String,Integer> map = new HashMap<>();
- mdasen 11y agoImmutables (http://immutables.org/ http://immutables.org/) is another option which creates nice, immutable value classes. It's a bit more succinct than AutoValue, but it does still require annotation processing in your IDE.
- CHY872 11y agoLombok's a bit sketchy as parts of it rely on some undocumented, non-standard parts of the Sun Java compiler - you have to be really into code generation to want to use it. Other libraries like Immutables are safer for this purpose.
- jonalmeida 11y agoWouldn't the best way to create an empty map be to use `Collections.emptyMap()`?