开发者

Java 1.7 vs C# List/Set/Map syntactic sugar

The following is supposed to be part of Java 1.7:

List<String> list = ["item"];
String item = list[0];

Set<String> set = {"item"};

Map<String, Integer> map = {"key" : 1};
int value = map["key"];

The same can pretty much be done in C# (and VB.NET) with a slightly difference syntax. In .NET, this is implemented as indexed properties, which is a language feature any developer can use to apply onto own classes.

Does anyone know if these feature are str开发者_StackOverflow社区ictly hardcoded for List, Set, and Map and subclasses or there is a more general language feature behind this?


Neither of those features made it into JDK7. The Java language changes that made the cut are listed here as:

JSR 334: Small language enhancements (Project Coin). A set of small language changes intended to simplify common, day-to-day programming tasks: Strings in switch statements, try-with-resources statements, improved type inference for generic instance creation ("diamond"), simplified varargs method invocation, better integral literals, and improved exception handling (multi-catch)

The remaining "Project Coin" features and other language changes were punted to JDK8:

JSR 294: Language and VM support for modular programming. Enhancements to the Java language and virtual-machine specifications to support modular programming, at both compile time and run time

JSR 308: Annotations on Java types. An extension to the Java annotation syntax to permit annotations on any occurrence of a type

JSR TBD: Language support for collections. Literal expressions for immutable lists, sets,

JSR TBD: Project Lambda. Lambda expressions (informally, "closures") and defender methods for the Java programming language


You are referring to two distinct features of Project Coin that were NOT accepted for inclusion in JDK 7.

  1. Support for Collection literals

    SomeClass a;
    SomeClass b;
    List<SomeClass> list = [a, b];
    Map<String, SomeClass> map = { "key1": a, "key2": b };
    Set<SomeClass> set = { a, b };
    

    It should be noted that these literals only allow for the creation of unmodifiable (read-only in .NET) collections. As far as I know, such literals do not exist in C# (except for arrays, but such literals have also existed in Java for a long time). It also does not have anything to do with indexed properties.

  2. Indexing access syntax for Lists and Maps

    List<SomeClass> l = new ArrayList<>();
    SomeClass first = new SomeClass();
    l[0] = first; // instead of l.set(0, first);
    SomeClass retrieved = l[0] // instead of l.get(0);
    // Similar thing for maps.
    

    That is the feature which is implemented in C# as indexed properties, and it only applies to the List and Map interfaces (remember that, contrary to the .NET Class Library, the Java interfaces are not usually prefixed with an I) - it is nothing more than a transformation, by the compiler, into calls to these methods.

In both cases, there is no general language feature, just a bit of syntactic sugar:

  • For the first feature, the compiler will transform the literal into a call to a few methods to create a unmodifiable List, Set or Map. (I seem to remember that the concrete classes that are returned are not even publicly visible.)
  • For the second feature, the compiler will just transform the expression into calls to the relevant methods, as long as the class implements the List or Map interface.


They want add some features which other languages(static/dynamic) already have into Java.The initialization way for the map is the same as some dynamic languages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜