tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Google Guava > Basic > Object to String Helper

Object to String Helper 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Objects.toStringHelper() API.

File Name  :  
com/bethecoder/tutorials/guava/base_tests/Object2StringHelperTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.guava.base_tests;

import java.util.Date;

import com.bethecoder.tutorials.guava.common.Student;
import com.google.common.base.Objects;

public class Object2StringHelperTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    Student student = new Student("Sriram"2"Chess");
    
    String str = Objects.toStringHelper(student).toString();
    System.out.println(str);
    
    str = Objects.toStringHelper(student)
      .add("name", student.getName())
      .toString();
    System.out.println(str);
    
    str = Objects.toStringHelper(student)
      .add("name", student.getName())
      .add("age", student.getAge())
      .toString();
    System.out.println(str);
    
    str = Objects.toStringHelper(student)
    .add("name", student.getName())
    .add("age", student.getAge())
    .addValue(new Date())
    .toString();
    System.out.println(str);
  
  }

}
   

It gives the following output,
Student{}
Student{name=Sriram}
Student{name=Sriram, age=2}
Student{name=Sriram, age=2, Sat Nov 19 19:01:24 IST 2011}


                    Print

   



Books Recommendation

 
 
bl  br