知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
      • Theory:What are collections
        • When arrays are not enough
        • Different collections
        • Features of collections
        • The simplest collection example
        • Conclusion
      • Theory:The collections Framework overview
      • Theory:ArrayList
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-09-29
目录

Theory:What are collections

# When arrays are not enough

The Java language supports arrays to store multiple values or objects of the same type together. An array is initialized with a predefined size during creation. The size cannot be changed in the future, and that imposes some limitations on their use for solving business problems. If we want to store more data, we need to create a new larger array and then copy the data in this array manually. This can be inefficient for programs that process a lot of data.

# Different collections

Fortunately, there is a set of containers called collections for grouping elements into a single unit. They are used to store, retrieve, manipulate, and communicate aggregated data.

Collections are more sophisticated and flexible than arrays. First of all, they are resizable: you can add any number of elements to a collection. A collection will automatically handle the deletion of an element from any position. The second point is collections provide a rich set of methods that are already implemented for you.

There are several types of collections with different internal storage structure. You can choose a collection type best matching your problem so that your most frequent operations will be convenient and efficient.

提示

Actually, collections are representations of different data structures and abstract data types from computer science. It is good to understand the relationship between them and collections in Java. This will help you in programming interviews, and in working to select an appropriate collection.

# Features of collections

There are several specific features of collections in Java:

  1. They are represented by different classes from the Java Standard Library.
  2. All modern collections are generic types while old collections are non-generic. We will only focus on new collections. As regular generics, they can store any reference types including classes defined by you (like Person or something else).
  3. Collections can be mutable (possible to add and remove elements) and immutable (impossible to do that).

In addition to standard collections, there are a number of external libraries with collections. One of such libraries is Guava Collections which was developed by Google. It can be used if standard collections are not enough for solving your problems.

# The simplest collection example

There is an example of a simple collection called ArrayList. To use it, make the following import:

java.util.ArrayList;
1

It works in a similar way to a regular array, but you do not have to manually resize it to add and remove elements.

ArrayList<String> list = new ArrayList<>();

list.add("first");
list.add("second");
list.add("third");

System.out.println(list); // [first, second, third]

System.out.println(list.get(0)); // first
System.out.println(list.get(1)); // second
System.out.println(list.get(2)); // third

list.remove("first");

System.out.println(list); // [second, third]

System.out.println(list.size()); // 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

提示

Note, in this example we used the get method to access an element by its index. Unlike arrays, collections do not have the [] operator.

We hope this is enough for the first acquaintance with the collections. In further topics, you will learn different kinds of collections in more detail. Now the main thing to understand is that using collections is not more difficult than using a regular array.

注意

All modern collections are generic, so you can specify any reference type as a generic parameter and store it in a collection. But there is one restriction, collections cannot store primitive values at all (int, long, char, double and so on). You should use one of the wrapper classes (Integer, Long, Character, Double or another one) instead.

# Conclusion

Sometimes arrays are not flexible enough to store and manipulate your data. For that, Java provides collections — mostly generic classes from the Java Standard Library or external libraries, either mutable or immutable, that are more adjusted to store objects for specific complicated purposes. It'll take you more than one topic to learn even about basic types of collections that represent different data structures and serve different purposes. Stay tuned!

编辑 (opens new window)
#Collection#Java
上次更新: 2022/10/12, 17:01:25
Theory:Generics and Object
Theory:The collections Framework overview

← Theory:Generics and Object Theory:The collections Framework overview→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式