• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Should ListBuffer be choosen to use where one would have choosen ArrayList in case of Java

 
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was tying to create list in Scala for which I would have used ArrayList. I can see that in Scala , List is immutable and ListBuffer is mutable. Is ListBuffer to be used for cases where one would use ArrayList in Java?
thanks
 
Master Rancher
Posts: 3723
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, historically people have used ArrayList for almost everything in Java, even where an immutable list would be better.  That's simply because immutable lists weren't part of Java's libraries initially.  Now they're somewhat easier with List.of() and other techniques.  But you will still see ArrayList used in Java in many places where it's not the best fit necessarily.  So don't let that carry over to Scala.   When you need a mutable list, ListBuffer is a good candidate.  But if you don't need mutability, don't use it.

Having said that, in terms of performance characteristics, ListBuffer looks similar to ArrayList, except it also offers constant-time prepending, like LinkedList or ArrayDeque.  Probably it has similar weaknesses if you need to add or delete away from the ends.  But I haven't looked closely at its internals.
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Attractive, successful people love this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic