Posts Tagged ‘java’

Guava – Google Collections

Saturday, January 9th, 2010

Guava

Guava is Google Core Libraries for Java 1.5, it include extension libraries which provides many common pieces of code. Long long time ago apache starts with commons-* libraries, but unfortunately most of them lack java 5 support.

Currently Google Collection is included into Guava and on 30 Dec google released Google Collection 1.0-fianal version. This version is not 1.0 but it has frozen API. It’s nearly done, but it is used in google production projects now.

So, what’s inside ?

  • Immutable Collections
  • Multisets
  • Multimaps
  • New types
  • Static utilitis

Immutable Collections

JDK currently has utility wrapper in Collections which can change our map/set/list into unmodifiable, but this is just a partial solution because internally our collection is mutable. Immutable means that we can never change the object state. So there are:

com.google.common.collect.ImmutableMap
An immutable, hash-based Map
com.google.common.collect.ImmutableSortedMap
An immutable SortedMap
com.google.common.collect.ImmutableBiMap
An immutable BiMap
com.google.common.collect.ImmutableCollection
An immutable collection.
com.google.common.collect.ImmutableMultiset
An immutable hash-based multiset
com.google.common.collect.ImmutableSet
A high-performance immutable Set
com.google.common.collect.ImmutableSortedSet
An immutable SortedSet that stores its elements in a sorted array.

All this Collections don’t allow Null values and Maps don’t allow Null keys or values.

Multisets

Multiset is a type of collection which allows duplicate elements and has unordered equality (lists have ordered equality). A multiset is also called bag. Multiset is better from list in such operation as contains(), checking if two collection are equal ignoring order and providing histograms (providing info about element count). Additionally Multiset performance depends on the number of distinct elements.

There are a lot of different implementation of Multiset interface.

com.google.common.collect.ConcurrentHashMultiset
A multiset that supports concurrent modifications
com.google.common.collect.EnumMultiset
Multiset implementation backed by an EnumMap.
com.google.common.collect.ForwardingMultiset
A multiset which forwards all its method calls to another multiset. (Decorator pattern alike)
com.google.common.collect.HashMultiset
Multiset implementation backed by a HashMap.
com.google.common.collect.ImmutableMultiset
Immputable hash-based.
com.google.common.collect.LinkedHashMultiset
A Multiset implementation with predictable iteration order.
com.google.common.collect.TreeMultiset
Multiset which maintains the ordering of its elements.

Multimaps

Similar to Multiset is Multimap iterface. It provides abstraction like a Map but keys aren’t unique or we can thing about this that unique key has a list of values, so if you have somewhere in the code type such as Map> Multimap is for you. As 1.0 version we’ve got Multimap interface implementation such as:

BiMap

BiMap is the map with unique values. Than it can be inverted so bimap.inverse().inverse() == bimap. There are few implementation of BiMap interface:

com.google.common.collect.EnumBiMap
BiMap backed by two EnumMap instances
com.google.common.collect.EnumHashBiMap
BiMap backed by an EnumMap instance for keys-to-values, and a HashMap instance for values-to-keys.
com.google.common.collect.HashBiMap
BiMap backed by two HashMap instances.
com.google.common.collect.ImmutableBiMap
Immutable BiMap

Static Utility Class

In this category there are a lot of class which has only static members to extend and provide function which are useful but not available today.

Collections2
Provides filter with Predicate and transform with Function methods
Functions
Function extension eg compose
Lists
Provides such methods as new* for easier List creation and two additional methods partition(create sublists of the same size) and transform which takes Function as a parameter and apply it to every list element.
Maps
Provides such methods as new* for easier Map creation, difference method, filter* methods with Predicate, fromProperties, transformValues with Function parameter.
Multimaps
Provides methods such as new*, synchronized*, unmodifiable*
ObjectArrays
Provides contact and newArray methods
Sets
Provides methods: differece, filter, intersecton, new*, union
Suppliers
Some Supplier extensions
Predicates
Predicate extension eg. and, or, not etc
Preconditions
Static methods to be called at beginning of methods to check preconditions.
Objects
Helper for Object class has great hashCode(Object …) method for generating hash code for multiple objects.

And more …

Ordering
Comparator with added methods to support common functions such as min, max etc. So instead of iterate over list to find min value, we can use min(Iterable) method.
MapMaker
ConcurrentMap builder, providing any combination of these features: soft or weak keys, soft or weak values, timed expiration, and on-demand computation of values.
Function
A transformation from one object to another. Used in transform static utility methods for Collection API extension.
Predicate
Determines a true or false value for a given input. Used in filter static utility methods.
Supplier
Class that supply object of single type. Used in new* static utility methods

Final thoughts

So…, in my opinion this library is must have in every project and there are a chance that this library will be standard JDK7 library, there are a lot of structures and static utility methods which makes our code much cleaner and simpler. Googler made great job!

References


Long Time

Saturday, January 31st, 2009

I’m back.

Probably you think that I’m dead but it far from that. Rather I’ve got so many activities that I can’t manage writing about it on my blog. What’s a shame.

First at all in November I must finalize training. It was a lot of work to do, and preparing labs is very difficult task. The training was conducted in December and January, after that I’m very glad because we’ve got 5.24 note in 1-6 scale. Probably our note will be higher but on one of our training we have frustrated colleague with poor technology knowledge so he get nervous and he give us 2.5 note.

During this preparation, I was on JBoss Training. It was JBoss for Advanced J2EE Developers (JB261). It was in Amersfoort. It was one of the best training I’ve had. Maybe the reason was that was only four of us, so after this training participants and trainee is my freind, we have a lot of time to talk, to do extra exercises, it was great time. I have some thoughts about some post from this training.

Few days latter I went to Devoxx. It was quite good conference and we met there our friends so we had hard time, conference from morning to evening and than we tasted Belgium beers. Belgium beers are famous, if you like beer you should visit Belgium.

First Devoxx day I started with Brian Goetz with “From concurrent to parallel”. It was very nice but also hard topic. He talk about evolving concurrency API to support multi-core processors. The Brian’s wisdom is incredible. I’ve just get familiar with current concurrency API, but he is another step further. Next session I went to “Introduction to the SpringSource dm Server with Sam Brannen. It was nice talk but I went also to “How to hack and secure your java web application”, it was happened due to my involvement into security program in my company. Next session I’ve chosen “Envers” probably because Adam Warski is from Poland :) . Due to training the next session was obvious, so I went on “What’s new in Spring Framework 3.0?” Arjen with Alef was talking about new features in Spring Framework, and where was a lots of questions about schedule sources and new spring license policy. The last slot I was in several session and probably JavaFX with applet out of the browser stay in my mind. There was one quickie about “MinuteProject” by Florian Adler. MinuteProject is generator tool. It can generate DAO operation to hibernate/jpa and even iBatis, after this we have a little discussion about MDA and such projects future.

Next day was harder than a day before, but I started with “Be smart!” with Ivar Jacobson. You probably know Jacobson, if not try to get know something about him. It was one of the best session at Devoxx 2008. It will be on Parleys.com so Be smart and watch it. Next session was about JavaFX and after first impression I was a disappointed, but it just version 1.0 we’ll see what happen. Finally there was my favorite session javaposse.com with Belgium beer from Atlassian. After that I took break with my friends and we went outside to discuss sessions. Than I went on two sessions about Seem and preventing bugs. The last was about Hibernate Performance Tuning it was quite interesting. This day was quite nice quickies, one about Atlassian Connector for InteliJ and what I didn’t know that this plugin is developed in Poland.

The last day was short so I with my friends went to Brussels, when we stay for a whole weekend.

Quite long post. I finish it here, because after Devoxx Christmas was came :) .


about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.