Tuesday 25 October 2011

Collection creation and Immutability with Google Guava

So, thought I'd take a look at some of the collection creation patterns Guava offers, and also some of the Immutable collection types it offers.

If you've not seen my previous posts, you may want to start here:

Guava part 1 - MultiMaps
Guava part 2 - BiMaps
Guava part 3 - MultiSets

create methods

All of Guava's collection implementations contain one or more static create methods, these do what you'd expect, and generally offer a slightly more concise way of instantiating the collection classes.

Here are two different ways of creating a ArrayListMultimap

    Multimap<String,String> multimap1 = new ArrayListMultimap<String,String>();
    Multimap<String,String> multimap2 = ArrayListMultimap.create();

Ok, so there's not a huge amount in it - 12 characters in this example - but the way I see it, you're removing some redundancy, do we really need to reproduce the Generic type information twice?

That's nice, it's a shame Sun didn't think to add create methods to their Collection types when the created Java 5!

Again, Guava rides to your rescue here, and provides some utility classes for dealing with the standard collection types. com.google.common.collect.Lists, com.google.common.collect.Sets, and com.google.common.collect.Maps

These each provide several methods of the format newCollectionType(), here's some examples

    List<String> myList1 = new ArrayList<String>(); //old way
    List<String> myList2 = Lists.newArrayList();    //guava way

    Set<String> mySet1 = new HashSet<String>(); //old way
    Set<String> mySet2 = Sets.newHashSet();     //guava way

Since the "new" methods are static, you can cut out even more characters by using a static import ie...

    import static com.google.common.collect.Lists.newArrayList;
    import static com.google.common.collect.Sets.newHashSet;

    //elsewhere in code
    List<String> myList2 = newArrayList();
    Set<String> mySet2 = newHashSet();

Keypress wise you're not really saving a great deal using the guava methods, so I guess this is a matter of taste as much as anything else. Personally I think the Guava way reads much better, although I think I'd go without the static imports.

Good so far, but hardly earth shattering, what next?

Immutable collections

These are essentially collection objects that you can't change once they've been created, and are useful for all sorts of reasons. Guava provides Immutable implementations for most of the regular Collection interfaces: ImmutableList, ImmutableSet, and ImmutableMap , and also immutable implementations of some of the Guava collection interfaces ( ImmutableMultimap etc.)

My main use for them is creating static constants. For example lets say you need to hardcode a Set of Strings for some purpose. One way to do this might be, eg.

    private static final Set<String> farmAnimals =
                new HashSet<String>(Arrays.asList("Cow","Pig","Sheep"));

Doesn't look great does it, and it suffers from one major problem. Any code that can access this Set can also change it, which could lead to all sorts of unexpected problems.

Couldn't we just use Collection.unmodifiableSet(Set s) to solve this?

Well, in this particular example I guess we could write...

    private static final Set<String> farmAnimals =
         Collections.unmodifiableSet(
                new HashSet<String>(Arrays.asList("Cow","Pig","Sheep")));

...but that's starting to look a bit unwieldy, and the unmodifiable methods have one other problem. They only return an unmodifiable view of the collection, if you have a reference to the original collection, you can still alter it!

Whilst this may not be a problem in the last example, I still think a much better way of doing this is to use an ImmutableSet

    private static final Set<String> farmAnimals = 
                       ImmutableSet.of("Cow","Pig","Sheep");

That's much nicer isn't it! And there's several other ways we can create them, here's some examples:

    // use copyOf()...
    public void doStuffWithList(List<Object> unsafeList) {
       List<Object> safeList = ImmutableList.copyOf(unsafeList);
    }
    // use a builder...
    public Map<String,Integer> makeImmutableMap() {
        ImmutableMap.Builder<String,Integer> mapBuilder = 
                         new ImmutableMap.Builder<String,Integer>();
        Entry<String,Integer> entry = null;
        while((entry = getEntry()) != null) {
            mapBuilder.put(entry.getKey(), entry.getValue());
        }
        return builder.build();
    }
So, any other advantages of using Immutable collections?

Well there's several. They can simplify logic considerably, especially in multi-threaded environments. If threads only have read access to an object, them you don't need to worry about complicated thread synchronization logic

They are also slightly more efficient to use once they've been created. If a collection knows beforehand what it needs to store, and there's never going to be any changes, you can make various time and space savings. For example, most implementations of ArrayLists or HashMaps, will leave some unused space for new objects, so they don't have to constantly resize themselves. If you know there's never going to be any new objects, there's no need for this.

Finally you could also use them as hash keys. If the contents of a collection can't change, then neither will it's hashcode!

Any disadvantages?

There is of course one big disadvantage of Immutable objects, which is pretty obvious. You can't change them! If you need to alter the collection, you'll first need to take a copy of it. In certain situations - ie where concurrency is a concern - you may in fact want to take this approach. However this is going to be impractical where collections contain many many objects and you'll probably want a good old fashioned mutable collection (complete with synchronization code if required).

The only other thing to be aware of is, just because your collection is immutable, it doesn't mean the objects contained in them automatically are. If you can get a reference to an object in an immutable collection, then there's nothing to stop you changing any mutable state on that object! As a consequence it's best practice to make sure anything you keep in an immutable collection is immutable itself!

21 comments:

Unknown said...

very nice post, appreciate your efforts. Best essay writing service

Curtis said...

The study of the different subjects and domains of academics is very vital and important. Success in academic papers is achieved by seeking help in assignments service from an expert and the acquisition of knowledge in different stages.
Academic Term Paper Writing Service

Unknown said...

Hi

Ive just started to read up on this and have hit a problem After downloading the guava-18 jar file and including this in my net beans project for some reason I don't seem to be able to access the sets class or imutableIterator class

What could I be doing wrong Current Os is linux JDK 8

Cheers Mark

Mique Fahey Kalsder said...

In light of a couple of reasons ravens can't complete last quarter. Their beginning and end partners play without him since some individual hurt ravens http://www.resume2016.com/best-resume-examples-2016/ that is the reason he can't bounce back in this match. His beginning and end partner bid to God for his prosperity and play without him.

Unknown said...

Thanks for sharing this code. This is very helpful for us beginners in the programming world. We could certainly toy around this and learn from this. Make a few mistakes perhaps but this guide will be great.

Jeff (click here)

admin said...

Can you Write My Essay for Cheap? Yes, we provide Cheap and High-Quality Essay Writing Service with 24/7 Customer Care Support, and 100% Plagiarism Free Paper. You must know that essay writing is not an easy work for the students. It requires in-depth, detailed, and accurate information towards the topic. Especially the essays having the word count of more than 1,000 words are really difficult to write when the students are busy in their exam preparation.

Students want someone in this case to whom they can trust and hand over their assignments. They feel it difficult to lift the burden of the assignments, follow the deadlines, prepare the exams, and make presentations and projects. After all, doing all of this by hand can make students very upset. The Top Academic Tutors is working for the help of students who are not able to do their assignment and want someone to share their burden with.

In the past, students were not able to get the write my essay for me services from professional writers. But now the time has changed. There are many websites providing the online assignment writing services. And we are one of them. We are leading the market of writers for a few years now. Our packages for the students are set according to the financial conditions of the students.

Write my essay is one of our major writing services. Our services have many features and qualities that make our services best for the students. In this service, we write the essay for students or for other customers on the given topics. Your topic of the essay can belong to any subject like business and law, communication or history. We write essays on all subjects.

We have a team that specializes in business, physics, biology, law, history, other natural sciences, and science subjects. Our team of writers writes on all the subjects and topics. The benefits and qualities of our essay help services are discussed later in this article. Read the full article to get complete information about the write my essay for me service.
https://liveassignmenthelp.com
https://topacademictutors.com/write-my-essay/

Riya Rai said...

Our Dwarka Escorts call girl, is high professionalism, dependability, reliability to offer such sexual relations that you cannot fail to remember. If a decent sex experience is a thing that you need hire Escorts in Dwarka for full fulfillment.

Call girls in mahipalpur said...

Delhi Call Girls Agency, we are one among the various escort organization providing food young ladies for sexual administrations in Delhi and NCR. Our administrations are expanded all over Delhi and edges. We keep up unmistakable class of young ladies at all the majority of noticeable districts of the Delhi to guarantee our customers immediate and wanted administrations. Our Call Girls in Delhi are best class of young ladies giving extreme joy and furthermore moving message to not to surrender except if you achieve your point. Our Escort Service in Delhi attempt to change viewpoint towards life however yes they show the procedures to appreciate life and grin against the odd of life. Likewise become familiar with the procedures to make meeting more acceptable and interesting with escort young ladies.

Anonymous said...

Are you trying to find some fun and amusement tonight? Appointing escorts for this enjoyment would be an excellent choice. If you're frightened of being caught so you'll prevent the danger by availing In-call Escort Service in Karol Bagh. Our organization conducts affordable hotels for you. you'll enjoy intercourse with charming females of the town in luxury hotel suites. there's a variety of options and services also. you'll personalize your enjoyment by adding the specified delights from our wide selection of services. There are several services getting to provide by our professional Karol Bagh escorts. we've everything whatever keeps a man happy. you'll get a perfect companion for taking pleasure of your life from here. we've passionate college girls and mature housewife escorts in Karol Bagh also. you'll avail the most cost effective Call Girls in Karol Bagh by contact us or calling @9773622641.

Unknown said...

Welcome guys, Our Escorts in AJmer and contemplate young ladies of AJmer Escorts have the ability to perform you and make you unwinding in your life. With only a couple of minutes proceed with them are fundamental and getting a charge out of one prerequisite to accept over and over and esteem those minutes. Ajmer Call Girls Escort Provides with the different unbelievable and wonderful such young women of which you may have thought in your musings.
Call Girls in Ajmer

Activatecard said...

Very good write-up. I certainly love this website. Thanks! Walmart Card Activation

Thank you for posting such a great information activate chase debit card

Diyanka Call Girls said...

Such a great post you will share on your post.
Delhi Escorts
Escorts in Delhi
Escorts Service in Delhi
Delhi Escorts Service

Lucas Mason said...

Local Bitcoin ATM Customer Service & Live Chat Support

Local Bitcoin ATM Customer Service Bitcoin ATMs are operational since 2013, but they are long outnumbered by online trading. This has changed in any case due to the huge increase in Bitcoin ATMs.

Lucas Mason said...

BTC Customer Service Contact & Live Chat

If You Searching BTC Customer Service Content do visit our website.BTC Customer Service Contact

Unknown said...

Promote Abhi is the top-rated SEO services company in Hyderabad or SEO consultant Hyderabad. As a top digital marketing agency, We offer the best SEO services in Hyderabad
Now, you can buy indoor plants online near dwarka, new delhi with Ferns N Petals. Choose from our wide range of indoor plants on our website and send them across on various!
Fix SilverSingles Login Problem ... decreases the genuineness of a user profile so it's terribly necessary to spot and solve the issue.
For example, an Interior Designers and Decorators in Gorakhpur, Uttar Pradesh: may recommend changing the layout of the furniture. In other words, decorators are hands-off in the design process!
chasecard asks you to verifycard delivery whenever you get a new credit card in the mail. This is the same thing as activating a Chase credit card.

traininginstitute said...

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
cyber security certification malaysia

Henry said...

This is so good. I really like the way you resolve the problems. This one also helped me a lot. Now it's time to avail roller shutters Birmingham for more information.

Billy Kimber said...

Your programming blogs are always helpful with my every assignment. Keep helping us like that and thanks for this awesome blog. Now its time to get the services of Custom Soap Boxes, click for more information.

John Hardy said...

I really apricate your valuable and useful article. Everyone here to love your good article. Thanks for sharing this article. Now it's time to avail Custom Kraft Boxes for more information.

zamran said...

Your blog are always helpful for my assignment . And the programming part of your blog is really good. Thank you sharing the this type of content. Its time to avail of the service ofdoctors-child-specialist-lahore,click Here .

Chris said...
This comment has been removed by the author.