Almost every day you work with Strings. Guava has few convenient methods to make every day operations easier. Strings class Few static methods to notice: Strings.padEnd("foo", 6, 'xxx') will pad provided string up to 6 chars with 'x' char. The result of this call will be "fooxxx" Strings.padStart works analogously - it pads string from … Continue reading Guava Strings utilities
Tag: guava
Guava Splitter class demo
Split String with a delimiter to obtain the array of parts - a common task in programming. Especially when you work on parsing files (like CSV), input streams, etc. Let's take the row of a sample csv file: String csvRow = "desk,,chair,table,couch,"; Notice that it's a bit tricky, since that particular row has second and … Continue reading Guava Splitter class demo
Guava Joiner: Join all strings in an array or map
How often do you have an array of strings: List<String> listOfStrings = Lists.newArrayList("egg", "apple", null, "pineapple", "orange"); and needed to join its items in one string separated with commas (avoiding nulls) like that: egg, apple, pineapple, orange Source Code for this Guide is on my github: https://github.com/yacekmm/looksok/tree/master/Guava/GettingStarted Doing it Java way And how often did … Continue reading Guava Joiner: Join all strings in an array or map
Guava ListenableFuture intro
ListenableFuture is an abstraction to run asynchronous tasks in highly concurrent apps. It allows you to start a task, register callbacks and leave it to run and complete in separate thread. To fully explain it, let's start with standard JDK Future. JDK Fututre JDK Future is dedicated to use with long running tasks. It returns … Continue reading Guava ListenableFuture intro
Guava RateLimiter to control the frequency of events
Having a long running process, consisting of many small, fast steps, updating its listeners after processing each step? Assume that you process the list of thousands items in a thread and want to show each processed item to the user. Processing each item is very fast, so the updates will be send to the GUI … Continue reading Guava RateLimiter to control the frequency of events
Guava null checkers
A short demo introducing methods of handling null objects, null checks and null comparisons in guava. These guava methods will reduce the amount of ifs, NPEs and try-catch blocks. Here is what you can do: Null safe equality check // --------- null-safe equality check --------- String nullString = null; String nameString = "Jack"; Objects.equal(nullString, nameString); … Continue reading Guava null checkers
Guava collection initializers
Java requires quite verbose collection initialization (due to generics) like this: Map<String, Map<String, Integer>> lookupTraditional = new HashMap<String, Map<String, Integer>>(); It barely fits one line... It's long and readability is reduced. Java 7 introduced the Diamond Operator, so the developer does not have to repeat all the types declaration after the new operator: Map<String, Map<String, … Continue reading Guava collection initializers
Guava Multimap demo
The problem Handling maps that store collection of items under each key is very common. The thing I have in mind is this: Map<String, List<Integer>> playerScoresMap = new HashMap<String, List<Integer>>(); Let's assume that it stores scores for players. The player name is the key, and the value is a list of points scored by the … Continue reading Guava Multimap demo