You are previewing Getting Started with Google Guava.
O'Reilly logo
Getting Started with Google Guava

Book Description

Google Guava can transform the way you work with Java and this book shows you how. From beginner to expert, everyone can benefit from this smart guide that teaches faster, better coding.

  • Write more robust code that is easier to read and maintain

  • Learn how to use Preconditions to prevent and find errors faster

  • Shows how Guava Collections can make working with Java Collections a breeze

  • In Detail

    Java continues to maintain its popularity and is still one of the main languages used in the software industry today. But there are things in Java that are difficult to do that can be made easier; that’s where Guava comes in. Guava provides developers with a way to write better code, with less effort.

    Getting Started with Google Guava will show the reader how to start improving their code from the very first chapter. Packed with examples and loads of source code, this book will have an immediate impact on how you work with Java.

    This book starts with using Guava to help with the common tasks that Java developers perform. Then you’ll work your way through more specialized situations and finally some great functionality Guava provides that can add a lot of power to your applications with little effort. You will learn about Guava’s famous Collections classes that add unique features, like the Bi-Map, to Java’s already great Collection classes. We’ll see how to add some functional programming aspects to our code. We will also learn about using a self-loading cache for improved performance in our applications, and how to use the EventBus to create software that takes advantage of event-based programming.

    Table of Contents

    1. Getting Started with Google Guava
      1. Table of Contents
      2. Getting Started with Google Guava
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
        2. Why Subscribe?
        3. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started
        1. Introducing Google Guava
        2. The case for using Guava
        3. What is this book about?
        4. Installing Guava
        5. Using Guava with Maven or Gradle
          1. Getting the source code for the book
        6. Summary
      9. 2. Basic Guava Utilities
        1. Using the Joiner class
          1. Time for a review
        2. Using the Splitter class
          1. Time for a review
        3. Working with strings in Guava
          1. Using the Charsets class
          2. Using the Strings class
        4. Using the CharMatcher class
        5. Using the Preconditions class
        6. Object utilities
          1. Getting help with the toString method
          2. Checking for null values
          3. Generating hash codes
          4. Implementing CompareTo
        7. Summary
      10. 3. Functional Programming with Guava
        1. Using the Function interface
          1. Guidelines for using the Function interface
        2. Using the Functions class
          1. Using the Functions.forMap method
          2. Using the Functions.compose method
        3. Using the Predicate interface
          1. An example of the Predicate interface
        4. Using the Predicates class
          1. Using the Predicates.and method
          2. Using the Predicates.or method
          3. Using the Predicates.not method
          4. Using the Predicates.compose method
        5. Using the Supplier interface
          1. An example of the Supplier interface
        6. Using the Suppliers class
          1. Using the Suppliers.memoize method
          2. Using the Suppliers.memoizeWithExpiration method
        7. Summary
      11. 4. Working with Collections
        1. The FluentIterable class
          1. Using the FluentIterable.filter method
          2. Using the FluentIterable.transform method
        2. Lists
          1. Using the Lists.partition method
        3. Sets
          1. Using the Sets.difference method
          2. Using the Sets.symmetricDifference method
          3. Using the Sets.intersection method
          4. Using the Sets.union method
        4. Maps
          1. Using the Maps.uniqueIndex method
          2. Using the Maps.asMap method
          3. Transforming maps
        5. Multimaps
          1. ArrayListMultimap
          2. HashMultimap
        6. BiMap
          1. Using the BiMap.forcePut method
          2. Using the BiMap.inverse method
        7. Table
          1. Table operations
          2. Table views
        8. Range
          1. Ranges with arbitrary comparable objects
        9. Immutable collections
          1. Creating immutable collection instances
        10. Ordering
          1. Creating an Ordering instance
          2. Reverse sorting
          3. Accounting for null
          4. Secondary sorting
          5. Retrieving minimum and maximum values
        11. Summary
      12. 5. Concurrency
        1. Synchronizing threads
        2. Monitor
          1. Monitor explained
            1. Monitor best practice
            2. Different Monitor access methods
        3. ListenableFuture
          1. Obtaining a ListenableFuture interface
        4. FutureCallback
          1. Using the FutureCallback
        5. SettableFuture
        6. AsyncFunction
        7. FutureFallback
        8. Futures
          1. Asynchronous Transforms
          2. Applying FutureFallbacks
        9. RateLimiter
        10. Summary
      13. 6. Guava Cache
        1. MapMaker
        2. Guava caches
          1. Cache
          2. LoadingCache
            1. Loading values
            2. Refreshing values in the cache
        3. CacheBuilder
        4. CacheBuilderSpec
        5. CacheLoader
        6. CacheStats
        7. RemovalListener
          1. RemovalNotification
          2. RemovalListeners
        8. Summary
      14. 7. The EventBus Class
        1. EventBus
          1. Creating an EventBus instance
          2. Subscribing to events
          3. Posting the events
          4. Defining handler methods
          5. Concurrency
        2. Subscribe – An example
        3. Event Publishing – An example
        4. Finer-grained subscribing
        5. Unsubscribing to events
        6. AsyncEventBus
          1. Creating an AsyncEventBus instance
        7. DeadEvents
        8. Dependency injection
        9. Summary
      15. 8. Working with Files
        1. Copying a file
        2. Moving/renaming a File
        3. Working with files as strings
        4. Hashing a file
        5. Writing to files
          1. Writing and appending
        6. InputSupplier and OutputSupplier
          1. Sources and Sinks
        7. ByteSource
        8. ByteSink
        9. Copying from a ByteSource class to a ByteSink class
        10. ByteStreams and CharStreams
          1. Limiting the size of InputStreams
          2. Joining CharStreams
        11. Closer
        12. BaseEncoding
        13. Summary
      16. 9. Odds and Ends
        1. Creating proper hash functions
          1. Checksum hash functions
          2. General hash functions
          3. Cryptographic hash functions
        2. BloomFilter
          1. BloomFilter in a nutshell
          2. Funnels and PrimitiveSinks
          3. Creating a BloomFilter instance
        3. Optional
          1. Creating an Optional instance
        4. Throwables
          1. Getting the chain of Throwables
          2. Obtaining the Root Cause Throwable
        5. Summary
      17. Index