gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Hurricane Shutters
Home Improvement
Calling Cards
PDA Phones & Cases
Auto Insurance Quote
Promote Your Website
Data Center Solutions
Remote Online Backup
Baby Photo Contest
GPS
Shop
Career Education
KVM Switches
Compare Prices

 


Web Devs:
Moonlight as a Game Developer and Win Cool Prizes by Accepting the RIA Run Challenge

Now, your mission--should you choose to accept: Take your shot at gaming stardom if you think you might have what it takes to build a cool RIA game and you could win an Xbox 360 or other fabulous prizes. Hurry! You only have until May 15, 2008 to enter. »

 
Article:
Leveraging Your Flash Development with Silverlight

You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here. »

 
Article:
What Does it Take to Build the Best RIA?

With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application. »

 
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

The Google Collections Library, An Update
By Dick Wall

Go to page: 1  2  Next  

A couple of weeks ago, I wrote an article about the Google Collections Library (http://www.developer.com/java/ent/article.php/3735441), which gave some examples of the exciting features in this new open source library. The article carried a caveat that the 0.5 version of the library carried a few warnings, including the possibility that the API could change.

Less than 24 hours after the article was published, a new snapshot of the library was released, and this broke a couple of examples in the article. On top of that, a couple of bugs managed to sneak through into the article (this is the reason why developers typically use IDEs rather than word processors to write code ). As a result, this is a very short update article correcting the mistakes in the article, and also updating the examples where the API changed.

The new snapshot is still labeled 0.5 alpha on the site, so this is especially confusing. The snapshot itself carries a date that is the best way to track which version of the library you are using. This article will cover the changes in the file google-collect-snapshot-20080321.zip available from the Google Collections Library project page.

Correcting the Mistakes

First, there were a couple of small mistakes in the examples (typos, really) that I did not find until after the article had been published. These were in the examples for Multimap and Multiset. The original article had examples that read:

Multimap<Integer, String> numbers =
   Multimaps.newArrayListMultiMap();
...

and

Multiset<String> histogram =
   Multisets.newHashMultiSet();
...

If you tried out these examples, you will have probably already found the mistakes. They are in the capitalization of the newXXXMultiXXX methods. The name of these collections is Multimap and Multiset respectively, not MultiMap and MultiSet (even though I always, always want to type them like that).

So, the correct code examples should have been:

Multimap<Integer, String> numbers =
   Multimaps.newArrayListMultimap();

and

Multiset<String> histogram =
   Multisets.newHashMultiset();

The API Changes

The other changes in this new snapshot pertain to the immutable collections, or to be precise: ImmutableList and ImmutableSet.

The previous way of creating a new ImmutableList was to use a static convenience creator on the Lists class, as demonstrated in the example:

final List<String> immutableList =
   Lists.immutableList("Hello", "World");
// UnsupportedOperationException!
immutableList.add("!");

Using Lists.immutableList() to create a new ImmutableList has now been replaced by a static method on the ImmutableList class. The new way to do this is:

final List<String> immutableList =
   ImmutableList.of("Hello", "World");
// UnsupportedOperationException!
immutableList.add("!");

Likewise for ImmutableSet, just like an ImmutableList but guaranteeing unique values. This has a similar creation method:

final Set<String> immutableSet =
   ImmutableSet.of("Hello", "World");

These are not huge changes, but they're enough to cause some confusion if you just copied the code out of the article and tried it.

There is also a new way of converting an existing List (or Set) into an ImmutableList (or ImmutableSet) that more clearly explains to the developer what is going on. Instead of using Lists.immutableList(otherList) that could be mistaken for the same kind of inexpensive (but less safe) operation that Collections.unmodifiableList carries out, the new method to do this is: ImmutableList.copyOf(otherList). There is the equivalent method for ImmutableSet as well. The new copyOf method name leaves no doubt as to exactly what is going on to create the ImmutableList (or Set).

This affects the predicate example from the previous example, which changes from:

finalList<Car> premiumCars =
   Lists.immutableList(Iterables.filter(cars, expensiveCar));

to:

finalList<Car> premiumCars =
   ImmutableList.copyOf(Iterables.filter(cars, expensiveCar));

Finally, there is a helpful note for the Function example because a couple of people hit this problem when trying it out. The problem relates to the definition of the Function which maps the ints through the numbers BiMap:

final Function<Integer, String> nameForNumber =
   new Function<Integer, String>() {
   public String apply(Integer from) {
      return numbers.get(from);
   }
}

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Enterprise Java Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.
Data Sheet: IBM Information Server Blade
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES