• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Collection literals  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As part of the JDK 9  proposals, I am not too sure about this, but there was some discussion going on over
Factory Methods for Collections

Such as , Collection literals that would allow the initialization of a collection with a compact expression as depicted here
for example creating a new Array in programming language Ruby can be done in the following ways:

new_array  = Array.new
new_array = []
new_array = ["a", "b", "c"]
new_array = %w[a b c]

If written, it should increase productivity and, with shorter lines of code, increase clarity as the resulting object could be immutable.

Would appreciate your thoughts on this.

Thanks
Sathya
 
Saloon Keeper
Posts: 9239
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a huge fan of collection literals in C#. Too often in Java have I done something like this:

Whereas in C# I could do this:

I would be very happy if this feature came to Java.
 
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise you would have to create a Stream from an array and use collect(Collectors.toList()) on it, or use Arrays#asList(). Agree: collection initialisers would be a nice feature.
 
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, but you can do this, which is not far from C# version:
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And slightly more crazy stuff, but I like it, as in some of my cases it is useful:
 
Stephan van Hulst
Saloon Keeper
Posts: 9239
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I give up on generic type inference AND my application introduces a new class? Thanks for the suggestion, but no thanks. This also doesn't work for custom collection types that are not extensible.
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So I give up on generic type inference AND my application introduces a new class?


Yes, that's the price. Just wrote as an existing possibility.
 
Stephan van Hulst
Saloon Keeper
Posts: 9239
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks, I hadn't considered the possibility :)
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 9 doesn't add collection literals, but it does add factory methods to List, Set and Map. These are all overload quite a bit, for 0 to 10 elements/entries, plus a varargs version in case 10 isn't enough. For Map, you add keys and values in alternating order, and if 10 isn't enough Map has new static method entry for creating entries. So Stephan's example can be written in two ways:

 
satya Priya Sundar
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of suggestions and wish list.

  my 2 cents :
                   List<Integer> list = #[ 1, 2, 3 ];

 
Rob Spoor
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Reinhold has already confirmed at Devoxx BE last year that real collection literals will not happen any time soon, and that the factory methods I mentioned will be the Oracle answer to the demand for collection literals.
 
satya Priya Sundar
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose they have accepted this proposal and made its way through to the final
release....


JEP 269: Convenience Factory Methods for Collections

Define library APIs to make it convenient to create instances of collections
and maps with small numbers of elements, so as to ease the pain of not
having collection literals in the Java programming language

• Decrease the amount of code needed for creating small collections and
maps
core-libs / java.util:collections
Set<String> alphabet = Set.of("a", "b", "c");

 
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rob said above, those are the new "of" factory methods in Java 9. Note that the resulting collections are immutable. I think it'll be interesting to see how that affects the way developers code -- they're not necessarily expecting immutable collections from a method like that.
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth A. Kousen wrote:I think it'll be interesting to see how that affects the way developers code -- they're not necessarily expecting immutable collections from a method like that.


It shouldn't be much surprising though. More recent languages as Scala, Kotlin also does that. However, both languages have both, mutable and immutable collections. As a general practice immutable are preferred. And that is probably also not so surprising as nowadays paradigm heading towards more and more functional programming. As far as I understand Java 9 will support both too, standard way of creating will give a mutable, and using factory methods will lead to immutable.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but nothing about the name of the factory method implies that the resulting collection is immutable. For example, I used Arrays.asList(...) for years without realized that also produced an immutable collection.

I agree that it makes sense and even that it's a good thing. I think it's going to surprise a lot of developers though.
 
Rob Spoor
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth A. Kousen wrote:Sure, but nothing about the name of the factory method implies that the resulting collection is immutable. For example, I used Arrays.asList(...) for years without realized that also produced an immutable collection.


It's not immutable, you can still replace values. You just can't add or remove anything.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The fact that I keep accidentally making that mistake means I need to work on it, but also that maybe they should be a little clearer. Thanks for the reminder.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!