Remote Software Engineer at Stripe and cellist based out of Ontario. Previously at GitLab. Fascinated with building usable, delightful software.
December 25, 2012 | Less than 1 minute to read
Java 7 introduced a new operator - <>
- referred to as the “diamond” operator. This new keystroke-saving syntax allows you to maintain the benefits of compile-time generics without typing out long strings of redundant type parameters. Prior to Java 7, creating and initializing a variable with nested type parameters was an arduous task:
HashMap<TreeSet<TreeMap<String, Integer>>, Double> myTerribleDataStructure =
new HashMap<TreeSet<TreeMap<String, Integer>>, Double>();
The diamond operator greatly simplifies this unnecessary complexity. The following is semantically identical to the above example:
HashMap<TreeSet<TreeMap<String, Integer>>, Double> myTerribleDataStructure = new HashMap<>();
Note that this is not the same as:
HashMap<TreeSet<TreeMap<String, Integer>>, Double> myTerribleDataStructure = new HashMap();
Without the diamond operator, myTerribleDataStructure
is initialized to the raw HashMap
type instead of its type-specific counterpart.
Other posts you may enjoy:
October 14, 2024 | 3 minutes to read
May 31, 2024 | 6 minutes to read
June 26, 2023 | 14 minutes to read
January 25, 2022 | 6 minutes to read
August 26, 2021 | 2 minutes to read