java知识点汇总(三)–集合框架(Summary of java knowledge points (3) — collection framework)

1.管理对象(数组 + 集合框架)

2.数组

–数组和集合的区别

(1) 数组只能存放相同类型对象的一个对象,包括基本数据类型对象;集合可存放不同类型的对象,不包括基本数据类型对象

(2) 数组不能改变大小,集合可改变大小

–数组可转换为List List numbers = Array.asList(9,4,-9,100);

–数组相关

(1) 操作数组的工具类 java.util.Arrays

(2) length字段获取数组元素数量,索引值从0开始

(3) 没有元素的数组为空数组

(4) 声明和创建数组 String[] names = new String[4]; names[0]=”hello”; / String[] names = {“hello”,”yan”};

(5) 使用一个负索引或大于等于数组大小的索引将抛出异常java.lang.ArrayIndexOutOfBoundsException

(6) 迭代数组 for(String name : names){ }

(7) 多维数组,它的元素是数组 int[][] numbers = new int[3][2];

3.集合框架( 接口 + 类 )

–集合是集中存放其他对象的一个对象,存放的对象可称为元素

–集合也称为容器,提供存储、检索和操作元素的方法

–接口

Iterator 对象可排序

Comparator 对象可比较

Collection (包含子接口 Set, Queue, List)

**Queue: 实现类:LinkedList Queue queue = new LinkedList(); queue.add(“one”);

**Queue添加到其中的元素支持先进先出FIFO的元素排序方法

**Set: Set set = new HashSet();

(1) Set不允许存储重复元素

(2) 实现类最多允许一个null

(3) 使用HashSet时,不能保证元素的顺序一直不变

**List: 实现类:ArrayList List myList = new ArrayList();

(1) 可添加null,可有多个null

(2) 列表初始容量为10

(3) List是有序元素的集合,也称序列,可使用索引访问元素/将元素插入指定位置(0开始)

(4) List运行存储重复的元素

**collection提供了很多管理对象的方法,add,addAll,clear,size,isEmpty,toArray等

**Collection继承Iterator接口,可返回一个Iterator对象,可用该对象迭代集合中的元素

**for循环对集合进行迭代是使用Iterator的快捷方式

private static List<String> serviceIdNums11 = new ArrayList(12) { { addAll(claimIdNums); } };

Map (存储键值对,包含子接口 SortedMap, AbstractMap)

**SortedMap: 键按升序排序

**AbstractMap: 实现类:HashMap

**Map中的键不能重复,每个键最多映射一个值

**put方法添加键值对

**键和值不能为基本类型 map.put(1,300);(合法:调用put前执行了装箱操作)

**方法 put,putAll,remove,size,isEmpty, get(key);

**keySet()— 返回Map中包含所有键的Set

**values()—返回Map中包含所有值的Collection

**entrySet()—-返回一个包含Map.Entry对象的Set,每个Entry对象表示一个键/值对,Map.Entry接口提供getKey(),getValue()

–集合的转换

List转换为Set,去重

List myList = new ArrayList();

myList.add(“hello”);

myList.add(“hello”);

myList.add(“world”);

Set set = new HashSet(myList);

Queue转换为List

Queue queue = new LinkedList();

queue.add(“hello”);

queue.add(“world”);

List list = new ArrayList(queue);

————————

1. Management object (array + collection framework) < / strong >

2. Array < / strong >

–The difference between arrays and collections

(1) Array can only store one object of the same type, including basic data type objects; Collection can store different types of objects, excluding basic data type objects

(2) Arrays cannot be resized, collections can be resized

–Array can be converted to list numbers = array asList(9,4,-9,100);

–Array correlation

(1) Tool class for manipulating arrays, Java util. Arrays

(2) The length field gets the number of array elements, and the index value starts from 0

(3) An array without elements is an empty array

(4) 声明和创建数组 String[] names = new String[4]; names[0]=”hello”; / String[] names = {“hello”,”yan”};

(5) Using a negative index or an index greater than or equal to the array size will throw an exception Java lang.ArrayIndexOutOfBoundsException

(6) Iterative array for (string Name: names) {}

(7) Multidimensional array whose elements are array int [] [] numbers = New Int [3] [2];

3. Collection framework (interface + class) < / strong >

–A collection is an object that stores other objects in a set. The stored objects can be called elements

–Collections, also known as containers, provide methods for storing, retrieving, and manipulating elements

–Interface

The iterator object can be sorted

Comparator objects are comparable

Collection (包含子接口 Set, Queue, List)

**Queue: 实现类:LinkedList Queue queue = new LinkedList(); queue.add(“one”);

**The elements added to queue support the element sorting method of first in first out FIFO

**Set: Set set = new HashSet();

(1) Set does not allow duplicate elements to be stored

(2) The implementation class allows at most one null

(3) When using HashSet, the order of elements cannot be guaranteed

**List: 实现类:ArrayList List myList = new ArrayList();

(1) Null can be added, and there can be multiple nulls

(2) The initial capacity of the list is 10

(3) List is a collection of ordered elements, also known as a sequence. You can use an index to access elements / insert elements into a specified location (starting from 0)

(4) List run stores duplicate elements

**Collection provides many methods for managing objects, such as add, addall, clear, size, isempty, toArray, etc

**Collection inherits the iterator interface and returns an iterator object, which can be used to iterate over the elements in the collection

**The for loop iterates over the collection as a shortcut to using iterator

private static List<String> serviceIdNums11 = new ArrayList(12) { { addAll(claimIdNums); } };

Map (store key value pairs, including sub interfaces SortedMap and abstractmap)

**SortedMap: sort keys in ascending order

**AbstractMap: 实现类:HashMap

**The keys in the map cannot be repeated. Each key can map at most one value

**Add a key value pair to the put method

**Keys and values cannot be of the basic type map put(1,300); (legal: boxing operation is performed before calling put)

**方法 put,putAll,remove,size,isEmpty, get(key);

**Keyset() — returns the set containing all keys in the map

**Values() — returns the collection containing all values in the map

**Entryset() — returns a map containing Set of entry objects. Each entry object represents a key / value pair, map The entry interface provides getkey(), getvalue()

–Conversion of sets

List is converted to set, and the duplicate is removed

List myList = new ArrayList();

myList.add(“hello”);

myList.add(“hello”);

myList.add(“world”);

Set set = new HashSet(myList);

Convert queue to list

Queue queue = new LinkedList();

queue.add(“hello”);

queue.add(“world”);

List list = new ArrayList(queue);