java learning and application of (3.2) - data structure

Topology collection

 

 

A set of common methods described

Collection, unlike arrays, variable length storage object.
Collection Interface, commonly used functions such as: the Add Add, Clear Clear, Remove removed, the contains contains, inEmpty is empty, size number, toArray stored into the array
Iterator iterator interface, remove the elements in the set until no element in the set of . As used hasNext there is a next element, and next fetches the next element method.
Use iterates over the collection, using the collection method of obtaining an iterator iterator (including generics), then traverse. Iterator essence from -1 backward movement start position pointer traversal.
For enhanced for loop to traverse, format simplifies writing iterator. for (variable, variable names: set / array name) {} access variable name

 

Generics

Generics, an unknown data type, E type data representative of the unknown, which determines the type of the object created when E. E is the default Object type.
Generic can store and retrieve data stored in the same type of collection. Check the code specifications at compile time.
Creating contains generic class that allows class of widely used generic data types. Format public class Name <E> {private E xxx , etc.}, E represents implemented using return values and data definitions.
Containing generic methods , such as M represents a change is transmitted to an internal data, and returns. Formats such as public [static] <M> void method01 (M [ using generic] m) {xxx}
containing generic interface , formats such as public interface Xxx <E> {xxxx }. Then create an implementation class that defines a generic data types. Before generics can also be inherited, and then rewritten.
Generics wildcard ?, Receiving data representing an arbitrary, Object object is its essence, only when using transmission (not defined when used) parameter
generic limit defined :? Generic extends E can only be used on behalf of type E subclass / itself, generic limit defined:? generic super E can only be used on behalf of the type of the parent class E / itself, which defines <> range used
collections collection tools, shuffle method may disrupt the collection in order. Enhanced for loop can be used to generate quick idea

 

The basic data structure

Delete Java array of other operations, may change their preferences address (frequently open space).
Sort tree, on the basis of binary tree, left subtree large, small tree of the right child. Balanced tree, the same number of children left and right children. Unbalanced tree, left-right difference.
Red-black tree, tree close to balance, speed is very fast queries, query the maximum number of leaf nodes and the minimum number can not be more than two times. Query speed.

 

 

List Interface

 List interface, ordered set, indexed, allowing the storage repeating elements. Commonly used methods of adding elements to add to the specified location, get returns the specified position of the element, remove the element Removes the specified location, set the specified set position of the element. You can use an iterator, get and for other methods of traversal.
ArrayList array, use multi-threading technology, the process is repeated additions and deletions to open up space and assignment, additions and deletions lead to slow.
LinkedList list method, the same multi-threading technology, add, addFirst, addLast, push add elements, getFirst, getLast get the elements, clear empty elements, removeFirst, removeLast, pop removing elements, etc., and last elements
Vector collection of single-threaded technology ,Synchronize. The same variable length arrays.

 

Set Interface

Set接口,不包含重复元素,没有索引,不能使用for遍历。
HashSet集合,哈希表结构(查询快),无序,不同步,使用迭代器或增强for遍历。hashCode方法,获取操作系统随机给出的十进制整数(哈希值(模拟地址值))。
String类重写了hashCode方法。不同字符串可能有相同的哈希值。
java1.8以后,哈希表使用数组,链表和红黑树提高查询速度。
数组结构:把元素进行了分组(相同哈希值的元素是一组,链表/红黑树结构把相同哈希值的元素连接到一起。每组数量大于8则将链表变成红黑树。数组长度定为16。
因在哈希冲突后,会比较组内的元素是否存在,确定是否存储,以保障Set集合中元素不重复。存储的引用类型数据必定重写了hashCode和equals方法(保障数据不重复)。
自定义的数据类型可以通过idea自动生成hashCode和equals方法。
LinkedHashSet集合,哈希表+链表与红黑树结构,另外多了一条链表用于保障元素有序。遍历有序。
可变参数,类型确定,个数不定,格式如int ...arr,其实质为创建数组。使用增强for遍历。可变参数只有一个,且位于所有变量末尾。另外如:Object ...args。

 

Collections工具类

Collections的工具类,包含静态方法如:add添加元素,shuffle打乱元素,addAll添加多个元素,sort按默认规则排序(自定义类需要实现接口Comparable,重写方法compareTo)
sort排序(使用Comparator匿名类重写compare方法作为参数进行排序)其中自定义的排序方法可以组合进行多个关键字排序

 

Map接口

Map接口,包含K和V两个泛型(键、值)(双列集合,一一对应,键值不能重复)。HashMap集合,实现了Map接口,集合底层为哈希表,查询快。LinkedHashMap底层是哈希表和链表(保障数据顺序)。
Map的方法,put放入(返回被替换的键),get获取,remove删除(返回删除值),containsValue,containsKey是否包含。
keySet方法,返回的key会放到Set集合中,使用迭代器或增强for进行遍历key,键找值,进行遍历。
Map接口的实现集合被创建后,为每个键值对其内部的创建了Entry对象(Map.Entry),多个Entry用于记录键值对映射关系的集合(使用entrySet取出)。使用Entry中的getKey方法获取key,使用getValue创建vaule。
HashMap类存储自定义类型,key需要重写hashCode和equals方法,保障key的唯一性。
LinkedHashMap类,有序集合存储。HashTable键和值都不为空,同步单线程,双列集合(区别于HashMap的允许空值等)。
哈希表的优点和利用在于其快速查找,配合Map可以快速统计。
of方法,一次性添加多个元素,适用于List,Set,Map接口,返回的集合不能改变(JDK9)。

 

Guess you like

Origin www.cnblogs.com/bai2018/p/12274454.html