![]() But Java 8 provides an Optional, so let’s use it! Optional optional = Next, pre Java 8, we could always use a null check as seen above. Person person = (Person::named, "Mary Smith") Thus, the code throws a NullPointerException. When we call detectWith, person gets set to null as we couldn’t find anyone that satisfied the predicate. ![]() See the code example below (taken from our kata tutorial materials): Person person = (Person::named, "Mary Smith") Īssert.assertEquals("Mary", person.getFirstName()) Īssert.assertEquals("Smith", person.getLastName()) Instead of returning the element or null, it returns an Optional which is then left to the user to handle. Thus, in 8.0 we introduced detectWithOptional(). If it does not find any element, it returns null. detectWith accepts a Predicate argument and returns the first element in the collection that satisfies that condition. So, where can we use this in Eclipse Collections? tectWith() is a perfect fit. Basically, Optional helps protect us from NullPointerExceptions by forcing us to handle potentially null items. If a value is present, isPresent() will return true and get() will return the value". From the Javadoc, "A container object which may or may not contain a non-null value. ![]() Optional is one of the most popular new features for Java 8. Now, starting with Eclipse Collections, we have made the design decision to be compatible with Java 8+ in order to start leveraging some of the cool Java 8 features in our own codebase. Eclipse Collections was compatible with Java 8, but it did not use or embrace it. You could also use Java 8 and leverage lambdas and method references when using the rich API, and in fact it worked quite well.īut that’s all you really got. Prior to the Eclipse Collections release, EC was compatible with Java versions 5 - 7. We have a list of people (type Person), each person can have a list of Pets, and each pet is of a certain PetType enum. The Domainīefore we dive into any details or code examples, let’s walk through the domain that we will use in this article for our code snippets. If you’d like some good introductory literature, take a look at Donald Raab’s InfoQ articles, "GS Collections By Example" part I and part II. In 2015, it was migrated to the Eclipse foundation, and since then, all active development for the framework was done under the Eclipse Collections name and repository. It was developed internally at Goldman Sachs for 10 years before being open sourced in 2012 as GS Collections. Eclipse Collections also has a full complement of primitive containers. It has JDK-compatible List, Set and Map implementations with a rich API, as well as additional types not found in the JDK such as Bags, Multimaps and BiMaps. 30 Second Introduction – What is Eclipse Collections?Įclipse Collections is a drop in replacement for the Java Collections framework. ![]()
0 Comments
Leave a Reply. |