- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Joiner class Guava Java
Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −
Example
import com.google.common.base.Joiner; import java.util.*; public class Demo{ public static void main(String[] args){ String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" }; System.out.println("The original array is : "+ Arrays.toString(my_arr)); String my_result = Joiner.on('+').skipNulls().join(my_arr); System.out.println("The joined string is : " + my_result); } }
Output
The original array is [hel, null, lo, wo, r, null, ld] The joined string is hel+lo+wo+r+ld
A class named Demo contains the main function, which defines a string array. The array is converted to string and displayed on the string. The array also contains some null values. While displayed this array, the nulls are removed and replaced with the ‘+’ operator, all because of the Joiner class present in the Guava package. This output is displayed on the console.
Advertisements