Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Can someone explain the diamond operator?

    So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. After looking through the tutorials, I have come here :D. Take the following code for example: Why could we not just do this without the diamond? Can someone explain its use and why we should prefer it? Thanks!

    Java Code:
    import java.util.HashMap;
    class Untitled {
    	public static void main(String[] args) {
    		HashMap<String, String> map = new HashMap<String, String>();
    		
    		map.put("California","Sacramento");
    		map.put("Oregon","Salem");
    		map.put("Washington","Olympia");
    		
    		System.out.println(map);
    			}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Can someone explain the diamond operator?

    Look up Generics.
    Or simply read the tutorial.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Can someone explain the diamond operator?

    Quote Originally Posted by MW130 View Post
    So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. After looking through the tutorials, I have come here :D. Take the following code for example: Why could we not just do this without the diamond? Can someone explain its use and why we should prefer it? Thanks!

    Java Code:
    import java.util.HashMap;
    class Untitled {
    	public static void main(String[] args) {
    		HashMap<String, String> map = new HashMap<String, String>();
    		
    		map.put("California","Sacramento");
    		map.put("Oregon","Salem");
    		map.put("Washington","Olympia");
    		
    		System.out.println(map);
    			}
    }
    I don't get it; that code is not using the diamond operator at all :s This code does:

    Java Code:
    HashMap<String, String> map = new HashMap<>();  // <> = diamond
    And as you can see, its to save you some typing when using generics. Learn to love it.

    Btw, you are of course not the first to ask this:

    generics - What is the point of the diamond operator in Java 7? - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Unary operator and binary operator
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 10
    Last Post: 08-27-2013, 04:39 PM
  2. Replies: 2
    Last Post: 11-15-2012, 04:20 PM
  3. Replies: 5
    Last Post: 01-21-2012, 11:02 PM
  4. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  5. Replies: 3
    Last Post: 12-13-2010, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •