preface

For the record, this article uses JDK1.8

Start today to formally learn the most important thing in Java fundamentals -> collections

No matter in the development, in the interview this knowledge point is very very important, therefore, I spend a lot of time here, have to read a lot of information, the following may not be more day…

Of course, if there is something wrong, please bear with me and correct me in the comments

I. Introduction to Collection

1.1 Why is Collection needed

  1. As Java is an object-oriented language, it is inevitable to deal with objects
  2. To make it easier to manipulate multiple objects, we need to store them
  3. If you want to store multiple objects (variables), you can easily think of a container
  4. Common containers we know are ->StringBuffered, array (although there are arrays of objects, the length of the array is immutable!)
  5. So Java provides us with a Collection of ~

1.2 The difference between arrays and collections

The difference between arrays and collections is as follows:

The difference between arrays and collections:

  • 1: Length difference
    • The length of the array is fixed
    • The length of the set is variable
  • 2: Content is not acceptable
    • Arrays store elements of the same type
    • Collections can store different types of elements (but generally we don’t do this…).
  • 3: The data type of the element
    • Arrays can store primitive data types as well as reference types
    • Collections can only store reference types (you store simple ints, which are automatically boxed into Integer)

1.3 Origin and functions of Collection

Origin of Collection:

  • Collections can store multiple elements, but weThere are also different requirements for multiple elements
    • Multiple elements, cannot have the same
    • Multiple elements that can be sorted according to certain rules
  • For different needs: Java provides many collection classes with different data structures. But structure doesn’t matter, it’s about being able to store things, being able to judge things, being able to access them
  • The common content of the Collection is continuously extracted to form the inheritance system of the Collection —->Collection

The general structure of the Collection looks like this:

However, we generally don’t need that much, just a few commonly used collection classes on the line. The ones I circled below:

Refine again:

Basic features of Collection:

Introduction to iterators

Iterable () : iterator(); iterator() : iterator();

Iterable is an interface:

It has the iterator() method, which returns an iterator

Iterator is also an interface that has only three methods:

  • hasNext()
  • next()
  • remove()

However, we could not find the corresponding implementation method. We had to look under the subclass of Collection, so we found ArrayList.

Thus, we find the implementation of Iterator in the ArrayList: it is implemented as a partial class in the ArrayList! And, as you can see from the source, Iterator is essentially iterating over the collection

So we can use Iterator to iterate over the elements of a Collection, and implement it as an inner class!

List collection introduction

A Collection can be a Collection of two types: a Set and a List.

Let’s look at the methods of the List interface, a little bit more than Collection:

  • The List collection is characterized by order (the order of storage and retrieval is the same) and repeatability

A Collection returns an Iterator interface, and a List has its own implementation — the ListIterator interface

This interface has several more methods than the normal Iterator interface:

As you can see from the method name, ListIterator iterates through, adds elements, and sets elements

3.1 Common subclasses of List Collection

There are three common subclasses of the List collection:

  • ArrayList
    • The underlying data structure is an array. Thread insecurity
  • LinkedList
    • The underlying data structure is a linked list. Thread insecurity
  • Vector
    • The underlying data structure is an array. Thread safety

There are three commonly used collection classes, which will be explained in a new article

4. Introduction to Set

We can see from the methods of the Set Collection that there are no more methods than Collection

  • A Set is characterized by non-repeatable elements

4.1 Common subclasses of Set

  • HashSet collection
    • A: The underlying data structure is A hash table (which is an array of linked list elements)
  • TreeSet collection
    • A: The underlying data structure is A red-black tree (which is A self-balanced binary tree)
    • B: Ensure that elements are sorted
  • LinkedHashSet collection
    • A: : The underlying data structure consists of hash tables and linked lists.

Five, the last

This paper mainly summarizes the main points of Collection, without in-depth study of various Collection classes. This part will be explained in the new chapter, please look forward to it

Tomorrow is Tomb-sweeping Day (going back home), the collection will not be updated these two days, if you are free at home may brush a few algorithm problems and then after sorting will update the public account ~

The article directory navigation: zhongfucheng.bitcron.com/post/shou-j…

References:

  • Core Java
  • Data structure and algorithm analysis, Java language description

If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y

Open source project (6 K STAR) :Github.com/ZhongFuChen…

If you want to follow my updated articles and shared dry goods in real time, you can search Java3y on wechat.

The content of the PDF document is typed by hand. If you don’t understand anything, you can ask me directly (the official account has my contact information).