tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Google Guava > Collections > Immutable Map Builder

Immutable Map Builder 

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 ImmutableMap.Builder class. It returns an immutable map implementation which doesn't allow null keys and values.

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

import java.util.Map;

import com.google.common.collect.ImmutableMap;

public class ImmutableMapBuilderTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Map<String, Integer> immutableMap = 
        new ImmutableMap.Builder<String, Integer>()
          .put("ONE"1)
          .put("TWO"2)
          .put("THREE"3).build();
    System.out.println(immutableMap);
    
    immutableMap = ImmutableMap.<String, Integer>builder()
            .put("ONE"1)
            .put("TWO"2)
            .put("THREE"3).build();
    System.out.println(immutableMap);
  }

}
   

It gives the following output,
{ONE=1, TWO=2, THREE=3}
{ONE=1, TWO=2, THREE=3}


                    Print

   



Books Recommendation

 
 
bl  br