Solved Question on maps and collections

Discussion in 'Plugin Development' started by kameronn, Jan 1, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    kameronn

    What's the difference between
    Code:
    HashSet<Object> obj = new HashSet<>();
    
    // and
    
    HashSet<Object> obj = new HashSet();
    Rather than this

    Code:
    HashSet<Object> obj = new HashSet<Object>();
    (This question applies for maps and all kinds of collections)
     
  2. @kameronn
    While there is no real difference between
    Code:java
    1. HashSet<Object> obj = new HashSet<>();
    and
    Code:java
    1. HashSet<Object> obj = new HashSet<Object>();
    (besides the fact that one is shorter to type), as the Diamond operator (<>) automatically figures out the arguments from the left side. This, however,
    Code:java
    1. HashSet<Object> obj = new HashSet();
    should never be used, as it is only a backwards-compatibility thing (Most IDE's will even give you a warning if you try to do this), and disables the inspection in your IDE to be able to do certain things, like if you do it this way, you can pass a List of Strings into the constructor of an ArrayList, even though you defined it on the left side to be of type Integer. See this stackoverflow answer for more info.
     
    kameronn likes this.
Thread Status:
Not open for further replies.

Share This Page