Chapter 4. Collective Groovy datatypes

 

This chapter covers

  • Understanding Groovy’s collective datatypes: ranges, lists, and maps
  • How to declare them
  • Operators and library methods for these types
  • How to use them in action

The intuitive mind is a sacred gift and the rational mind is a faithful servant. We have created a society that honors the servant and has forgotten the gift.

Albert Einstein

The nice thing about computers is that they never get tired of repeatedly doing the same task. This is probably the singlemost important quality that justifies letting them take part in our lives. Searching through countless files or web pages, downloading emails every 10 minutes, looking up all values of a stock symbol for the last quarter to paint a nice graph—these are only a few examples in which a computer needs to repeatedly process an item of a data collection. It’s no wonder that a great deal of programming work is about collections.

Because collections are so prominent in programming, Groovy alleviates the tedium of using them by directly supporting datatypes of a collective nature: ranges, lists, and maps. Just as with simple datatypes, Groovy’s support for collective datatypes encompasses new lightweight means for literal declaration, specialized operators, and numerous GDK enhancements.

4.1. Working with ranges

4.2. Working with lists

4.3. Working with maps

4.4. Notes on Groovy collections

4.5. Summary