DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Functional Java Collections

Functional Java Collections

Mite Mitreski user avatar by
Mite Mitreski
·
Dec. 24, 12 · Interview
Like (0)
Save
Tweet
Share
3.95K Views

Join the DZone community and get the full member experience.

Join For Free

There is a lot of functional hype these days so I would give a short overview on what is out there at least when it comes to collections in Java. Personally I like standard collections API but i some cases can be awkward and add additional verboseness. This should not be a problem in latter version of Java 8+. There we would probably worry about not creating callback hell but hey there is no silver bullet for most stuff why should there be one for programming?

The Guava Way

Guava project is one of Google's core libraries where there are plenty of different core language aspects and problems covered. There are utilities and extensions for everyday usage like : collections, primitives, caching, common annotations, string processing, I/O, Math, Reflections and many others. We will only take a look at the Collections goodies so lets see some of them :
// list
    ImmutableList<String> of =
        ImmutableList.of("a", "b", "c", "d");
    // Same one for map
    ImmutableMap<String, String> map =
         ImmutableMap.of("key1", "value1", "key2", "value2");
    //list of ints
    List<Integer> theList = Ints.asList(1, 2, 3, 4, 522, 5, 6);
The Guava Collections are compatible with the JDK collections since they mostly extend or implement the standard classes. There are several cool utilities that are part of the API and have similar names with the ones from java.util.Collections. Basically any programmer who knows the JDK collections should be able to transfer to Guava easily. The ones for List is called Lists, one for Set is Sets, for Map is Maps and so on for the rest. For example:
//create new List
List<someLongName> list = Lists.newArrayList();
//create new  LinkedHashMap
Map<someKeyType, SomeValueType> map = Maps.newLinkedHashMap();

//initalize Array List on the spot
List<String> someList = Lists.newArrayList("one", "two", "three");

//set inital size for readability as well as performance
List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);
Methods corresponding to a particular interface are grouped in a very intuitive manner. There are also some extremely good ways of building cache with various of features :
Cache<Integer, Customer> cache = CacheBuilder.newBuilder()
        .weakKeys()
        .maximumSize(10000)
        .expireAfterWrite(10, TimeUnit.MINUTES)
        .build(new CacheLoader<Integer, Customer>() {

          @Override
          public Customer load(Integer key) throws Exception {

            return retreveCustomerForKey(key);
          }
        });
Since Guava is available in most of the maven repositories its very easy to add it to your build

lambdaj

The idea behind the project is to manipulate collections in a functional and statically typed way. This is achieved in a way the avoids repetitions of simple tasks we usually do with collections. Repetition makes programmers to go with copy/pasting and creates makes them create bugs by doing so. Accessing collections without explicit looping provides way of filtering, sorting, extraction, grouping, transforming, invoking a method on each item or sum up the elements or fields of those element in a collections. Additionally to all these features lambdaj is also a DSL in a way since it adds very cool "sugar" features to the syntax making it more readable in pseudo-english. This is done with static methods so in order to use them we include them directly:
import static ch.lambdaj.Lambda.*;
sAs it comes to checking and matching lambdaj relies deeply on Hamcrestmatchers. So for example to create a check for an odd integers and then filter a list with that check:
Matcher<Integer> odd = new Predicate<Integer>() {
        public boolean apply(Integer item) {
                return item % 2 == 1;
        }
};
List<Integer> oddNumbers = filter(odd, asList(1, 2, 3, 4, 5));
 and as expected the list will return the list [1,3,5]. Lambdaj take a step further with it's DSL, for example :
List<Beneficiary> beneficiaries = with(transactions)
    .retain(having(on(Transaction.class)
              .getQunatity(), lessThan(100)))
    .extract(on(Transaction.class).getBeneficiary())
    .sort(on(Beneficiary.class).getName());

Performance costs

Although the best way to make your application fast is to have the cleanest code as possible there comes a time when you must optimize.In order to do that there is some info provided by the creators on the memory usage and time. Lambdaj has a performance wiki page with code examples. There are also some test done by other programmers where they compare lambdaj with JDK8 for example. There are also some measurements on memory usage of Guava. As for performance of Guava most of it's functionality is standard JDK classes builders and utilities so the overhead is minimal. At the end of the day it's up to you to decide how much effect each of these libraries will have on your project and if that is positive. I'm for the idea that almost every project must have Guava on it's classpath.
Related links summary
  • Guavahttp://code.google.com/p/guava-libraries/
  • lambdaj http://code.google.com/p/lambdaj/
  • Hamcrest http://hamcrest.org/
  • Guava Links http://www.tfnico.com/presentations/google-guava
  • Guava examples https://github.com/mitemitreski/guava-examples
  • Guava presentation http://blog.mitemitreski.com/2012/07/google-guava-for-cleaner-code.html




Java (programming language)

Published at DZone with permission of Mite Mitreski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beginners’ Guide to Run a Linux Server Securely
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Easy Smart Contract Debugging With Truffle’s Console.log

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: