Questions tagged [diamond-operator]

The diamond operator (<>) is used in Perl for I/O and in Java for generics.

Perl

The Perl diamond operator is a special case of the stream input operator <FILEHANDLE> when the filehandle to read from is left void. In such a case, it strives to emulate sed/awk behavior by reading from files from the command line, falling back to standard input if there are not files left on the command line.

Java

The Java <> construct (in formally knows as "the diamond") was introduced in Java SE 7 to make declaring and initializing generic types more brief by automatic type inference during generic class instantiation. You can learn more about this in the Oracle Java Tutorial or the Java Language Specification.

A simple example of the concept is the following. Instead of writing this (before Java 7)

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Starting in Java 7, you can write the above as:

Map<String, List<String>> myMap = new HashMap<>();

(Note that <> is NOT an operator in Java, despite what you may hear or see in many places, and despite the name of this tag! The official Oracle sources all bear this out.)

78 questions
495
votes
7 answers

What is the point of the diamond operator (<>) in Java?

The diamond operator in java 7 allows code like the following: List list = new LinkedList<>(); However in Java 5/6, I can simply write: List list = new LinkedList(); My understanding of type erasure is that these are exactly the…
tofarr
  • 7,682
  • 5
  • 22
  • 30
42
votes
1 answer

Why explicit type argument should be replaced by diamond?

I'm using Android Studio and I write this : List
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
41
votes
5 answers

Why can't Java 7 diamond operator be used with anonymous classes?

Consider this Java code which attempts to instantiate some Lists: List list1 = new ArrayList(); List list2 = new ArrayList<>(); List list3 = new ArrayList() { }; List list4 = new ArrayList<>() {…
Dave Hartnoll
  • 1,144
  • 11
  • 21
40
votes
3 answers

Java 10: Will Java 7's Diamond Inference Work with Local Type Inference?

From JEP 286, we see that we'll be able to utilize local type inference (var) in JDK 10 (18.3). The JEP states that the following compiles, which is expected: var list = new ArrayList(); // infers ArrayList I'm curious to know…
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
22
votes
2 answers

Java- Diamond types are not supported at this language level

I have just started working on a Java project, and have downloaded the source code from GitHub, using IntelliJ- I have never used IntelliJ before, but am told that it is a much better IDE to use than Eclipse (which is what I was using when I last…
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
18
votes
4 answers

getting compile error for diamond operator in idea ide

I am getting this error while trying to compile some simple source code in idea ide. java: diamond operator is not supported in -source 1.6 (use -source 7 or higher to enable diamond operator) jdk is 1.7.40 from oracle but where is this place to…
Ashish Yadav
  • 557
  • 1
  • 5
  • 12
18
votes
2 answers

What is the diamond operator in Java?

I have an arraylist with type patient_class and the arraylist type has been underlined in yellow and the IDE has mentioned "redundant type arguments in new expression (use diamond operator instead)". My problem is: Should I use the diamond operator…
Troller
  • 1,108
  • 8
  • 29
  • 47
18
votes
1 answer

Which file is Perl's diamond operator (null file handle) currently reading from?

I'm using Perl's diamond <> operator to read from files specified on the command line. I'd like to be able to report messages like "Trouble on line $. of file $FILENAME", but how can I tell which file is currently used by the diamond?
PypeBros
  • 2,607
  • 24
  • 37
16
votes
2 answers

Java 7 diamond operator: why was it difficult to implement?

I watched the Oracle OTN Virtual Event: Java SE and JavaFX 2.0 (28 Feb 2012) and while talking about the new diamond operator (that Map> myMap = new HashMap<>(); thing) the speaker mentioned that it was not as simpleto implement…
jabal
  • 11,987
  • 12
  • 51
  • 99
13
votes
1 answer

How to fake input to perl's diamond operator?

The answers to this question describe how to fake input to . My goal is similar to that question: my unit test needs to fake input to <>. When I apply the same technique to fake input to <>, it doesn't work. The introductory-level…
bstpierre
  • 30,042
  • 15
  • 70
  • 103
13
votes
2 answers

Maven project Error: Diamond/multicatch operator not supported in -source 1.5

I can't build my maven java web application, because of the following two errors: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator) multi-catch statement is not supported in -source 1.5 (use…
MeesterPatat
  • 2,671
  • 9
  • 31
  • 54
13
votes
6 answers

Why diamond operator is used for Type Inference in Java 7?

List list = new ArrayList(); will result in compiler warning. However the following example compiles without any warning: List list = new ArrayList<>(); I'm curious why introducing of diamond operator is needed at all. Why not just…
Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65
11
votes
3 answers

Why doesn't the diamond operator work within a addAll() call in Java 7?

Given this example from the generics tutorial. List list = new ArrayList<>(); list.add("A"); // The following statement should fail since addAll expects // Collection list.addAll(new ArrayList<>()); Why does the last…
Pradeep Kumar
  • 471
  • 2
  • 6
  • 14
11
votes
4 answers

Why can't I use the diamond operator with an array in Perl?

Code $ cat test1 hello i am lazer nananana $ cat 1.pl use strict; use warnings; my @fh; open $fh[0], '<', 'test1', or die $!; my @res1 = <$fh[0]>; # Way1: why does this not work as expected? print @res1."\n"; my $fh2 = $fh[0]; my @res2 =…
Lazer
  • 90,700
  • 113
  • 281
  • 364
11
votes
3 answers

Why does the diamond operator not work for java.util.Collections methods in Java 7?

In Java 1.7.0_55, if I write this field declaration, I get a compilation error ("incompatible types"): private final Map myMap = Collections.synchronizedMap(new HashMap<>()); If I change that to read: private final…
Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
1
2 3 4 5 6