preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now

omg

Unconsciously, I also wrote more than ten articles, thank you for your attention here, I will continue to refueling. Next, we’ll talk about Java containers. As for how many articles we can write, how much detail we can write, let’s see.

You don’t need to read the source code to get started. Review and read the source code before your interview!

Origin of sets

Typically, our Java programs need to know how many objects have been created based on when the program runs. But until the program runs and develops, we don’t know exactly how many objects we need, or even the exact type. To meet these general programming needs, we want to be able to create any number of objects at any time, anywhere. What will hold these objects? We thought of arrays first, but! An array can only hold the same type of data, and its length is fixed. The collection was born.

The difference between collections and arrays:

A collection of figure

In fact, the most important or I circle these several

Java.util packages let’s look at the package from the top down. This article will cover a few of the upper interfaces and the specific containers as we go

The Iterator Iterator

One feature we can see is that all of the above collection classes, except for the collection of the Map family, the collection on the left, implement the Iterator interface

It is the top-level interface of the Java collection (excluding the collection of the Map family, which is the top-level interface of the Map family collection)

Object Next () : Returns a reference to the element that the iterator just passed. The return value is Object, which needs to be cast to its desired type.

Boolean hasNext() : Checks whether there are any more accessible elements in the container.

Void remove() : Removes the element that the iterator just passed.

So except for the map family of collections, we can iterate through the elements of the collection through iterators.

Note: We can trace the top-level interface of the Collection in the source code, such as the Collection interface, to see that it inherits the class Iterable

And then Iterable has Iterator

Let’s talk more specifically about Iterator

There are four methods

  • Determines whether the next iterator has a next element
  • Returns the value of the next element and moves its offset one bit
  • The third method is to delete elements from the set of iterators. (Note that if you delete the iterator from the set, you will find that the iterator is still the same. You will have to retrieve the iterator after deleting the element.)
  • – The new method in 1.8 allows iterators to iterate over the rest of an iterator, or all iterators from the start.

So what I’m trying to say is that all sets have an iterator that you can iterate over and it’s the highest level of all sets

ListIterator

Why are we going to talk about it? I wasn’t going to talk about it, but I thought about it, and I’m going to write all of it

ListIterator is a subinterface of Iterator, and ListIterator can iterate forward as well as backward. Compared with the Iterator,

  • It adds the following methods:
  • boolean hasPrevious();
  • E previous();
  • int nextIndex();
  • int previousIndex();
  • void set(E e);
  • void add(E e);

It’s just adding operations that can go one subscript ahead. And you can write a test that you can do for yourself and you can replace the set() method with the iterated elements

Collection Interface Introduction

The purpose of a Collection is to specify the basic operations of a Collection.

The main operations here are inserting data, clearing data, whether it contains data, whether it is equal, the number of data in the set and converting mature groups.

Such as:

Int size() gets the number of elements

Boolean isEmpty() specifies whether the number is zero

Boolean Contains (Object element) Specifies whether it contains the specified element

Boolean add(E Element) Adds an element, returns true on success

Boolean remove(Object element) Removes an element, returning true on success

Iterator Iterator () gets a Stream from Iterator Stream 1.8.

There are also ways to manipulate the entire collection, such as:

boolean containsAll(Collection<? > c) whether to contain all elements of the specified set C

boolean addAll(Collection<? Extends E> c) adds all elements of collection C to this collection, returning true if the collection has changed

boolean removeAll(Collection<? > c) delete the same element as in c, return true if the set has changed

boolean retainAll(Collection<? < span style = “box-sizing: border-box; color: RGB (51, 51, 51); line-height: 22px; font-size: 14px! Important; white-space: inherit! Important;

Void clear() removes all elements

There are also methods for manipulating arrays: Object[] toArray() returns an array containing all the elements in the collection

T[] toArray(T[] a) returns an array containing all the elements in the collection. The runtime specifies the type of the array based on the type of the elements in the collection

At the end

So we’re done with the introduction to collections of containers and I’m going to talk a little bit more about their implementation, and we’re going to learn together and make progress together

I have a goal to write two or three articles a week. I hope I can keep it up for a year. I hope you can give me more suggestions so that I can learn more and make progress together.

Daily for praise

All right, everybody, that’s all for this article. All the people here are talented.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!