Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Java 7 new feature : Using the diamond operator for type inference

Program to demonstrate Java 7 new feature : Using the diamond operator for type inference

package com.hubberspot.java7;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DiamondOperatorTypeInference {

 public static void main(String[] args) {

  // Scenario 1 - 

  // Code prior Java 7   
  List< String > list1 = new ArrayList< String >();

  // Code after coming of Java 7
  // we can now provide only the diamond operator
  // the compiler comes to know that list2 type
  // is a String
  List< String > list2 = new ArrayList< >();

  // Scenario 2 - 

  // Code prior Java 7  
  // the list3 below code is a raw type code for Java 6 and 
  // earlier versions.
  // The compiler generates warning as : ArrayList is a raw type. 
  // References to generic type ArrayList< E > should be parameterized
  List< String > list3 = new ArrayList();

  // Code after coming of Java 7
  // No warnings generated by the compiler.
  List< String > list4 = new ArrayList< >();

  // Scenario 3 - 

  // Code prior Java 7  
  // the list5 below code is a raw type code for Java 6 and 
  // earlier versions.
  // The compiler generates warning as : ArrayList is a raw type. 
  // References to generic type ArrayList< E > should be parameterized
  // to remove this warning we use @SuppressedWarning annotation
  // as : 

  @SuppressWarnings(value={ "rawtypes", "unchecked" })
  List< String > list5 = new ArrayList();

  // Code after coming of Java 7
  // No warnings generated by the compiler.
  // No need of placing @SuppressWarnings({"rawtypes","unchecked"})
  List< String > list6 = new ArrayList< >();


  // Scenario 4 - 

  // Code prior Java 7  
  // the list7 below code is a complex type code 

  List< Map< String, String > > list7 = new ArrayList();

  // Code after coming of Java 7
  // No warnings generated by the compiler.

  List< Map< String, String > > list8 = new ArrayList< >();

 }

}



 
© 2021 Learn Java by Examples Template by Hubberspot