Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to initialize ArrayList in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to initialize ArrayList in Java", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to initialize ArrayList in Java" this article bar.

1 Cause

In actual business development, we often encounter situations where we need to create an array temporarily.

2 solutions

To conclude, there are four initialization methods:

double bracket method

Arrays.asList

stream

Lists

2.1 List test = new ArrayList(){{ add(1); add(2);}};2.2 Arrays.asListList test = Arrays.asList(1, 2, 3);

The array generated in this way cannot be modified. To modify it, you can use the following method:

List test = new ArrayList(Arrays.asList(1, 2, 3));2.3 streamList test = Stream.of(1, 2, 3).collect(Collectors.toList());2.4 Lists

Java 9 can be used later.

List test = Lists.newArrayList(1, 2, 3);3 Summary

Finally, I ran a race for the first three methods, which took the following time (FYI):

Arrays.asList(0) > Double parenthesis (9) > stream(36)

The above is "How to initialize ArrayList in Java" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report