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 use ArrayList and HashSet of Java Collection collection

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about how to use ArrayList and HashSet in the Java Collection collection. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Collection

Collection interface is inherited by List interface and Set interface

List

ArrayList is the implementation class of the List interface

ArrayList

The ArrayList class is an array that can be modified dynamically. The difference from an ordinary array is that it has no fixed size limit, and we can add or remove elements.

ArrayList inherits AbstractList and implements the List interface.

Create an ArrayList object:

Public class List {public static void main (String [] args) {var list = new ArrayList ();}}

Add data to the collection:

Add ()

Public class List {public static void main (String [] args) {var list = new ArrayList (); list.add ("a"); list.add (1); list.add (1); System.out.println (list);}}

Clear the collection data:

Clear ()

Public class List {public static void main (String [] args) {var list = new ArrayList (); list.add ("a"); list.add (1); list.add (1); System.out.println (list); list.clear (); System.out.println (list);}}

Modify the object at the specified index location

Set ()

Public class List {public static void main (String [] args) {var list = new ArrayList (); list.add ("a"); list.add (1); list.add (1); System.out.println (list); list.set (2, "a"); System.out.println (list);}}

SetHashSet

HashSet is implemented based on HashMap and is a collection that does not allow repeating elements.

HashSet allows a null value.

The HashSet is unordered, that is, the order in which it is inserted is not recorded.

HashSet is not thread-safe, and if multiple threads try to modify HashSet at the same time, the end result is uncertain. You must explicitly synchronize concurrent access to HashSet during multithreaded access.

HashSet implements the Set interface.

Create an ArrayList object:

Public class Set {public static void main (String [] args) {var set = new HashSet ();}}

Add data to the collection:

Add ()

Elements in the collection cannot be repeated

Public class Set {public static void main (String [] args) {var set = new HashSet (); set.add (1); set.add ("a"); set.add ("a"); System.out.println (set);}}

Clear the collection data:

Clear ()

Public class Set {public static void main (String [] args) {var set = new HashSet (); set.add (1); set.add ("a"); set.add ("a"); System.out.println (set); set.clear (); System.out.println (set);}}

Delete the specified element

Public class Set {public static void main (String [] args) {var set = new HashSet (); set.add (1); set.add ("a"); set.add ("a"); System.out.println (set); set.remove ("a"); System.out.println (set);}}

The difference between ArrayList and HashSet

1.HashSet is unrepeatable and unordered! The only sexual guarantee. The repeating object equals method returns true, and the repeating object hashCode method returns the same integer HashSet is actually a HashMap, but you can only operate the key part of the HashMap through the Set API

2.ArrayList is repeatable and orderly: query efficiency is high, add and delete efficiency is low, lightweight threads are not safe. Arraylist: poor speed in inserting and deleting data, but faster in random extraction

Generics

Generic programming (generic programming) is a style or paradigm of programming language. Generics allow programmers to use types that are specified later when writing code in a strongly typed programming language and specify these types as parameters when instantiated. Various programming languages and their compilers and running environments have different support for generics.

Generics are generally used when using collections. Generics add a type constraint to the collection. It can be String or Object generic type does not support the basic type int, please use the wrapper type Integer

Create generics:

List list = new ArrayList ()

In this way, only String types can be added to this collection.

These are all the contents of the article "how to use ArrayList and HashSet in the Java Collection collection". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please 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