java-based collection on day 16

Collections Framework:

  • Iterable (iterator interface, implements the interface can use foreach, iterator objects can also get a collection of objects, through iterates over the collection)
    |
    | -iterator () =======> Iterator
    | |
    | listIterator ( ) Gets ListIterator
    |

  • Collection (set of all the root interface)
    | ---- List (Interface) represented by an ordered (deposit and withdrawal) may be repeated a set of elements may be stored null
    | -ArrayList (implementation class) based on the array, fast change check
    | -LinkedList (implementation class) based on the variables, fast plug deletions
    | LinkedList because there are a plurality of interfaces implemented method, therefore, the same function can be implemented with a plurality of methods, however, do not try to use the cross (use the same interface), because it will lead to cross-use of which neither fully with a full-featured interface

          	|---Vector(就是一个线程安全的ArrayList) 
          	|----Set(接口)表示无序(存入和取出)不可重复集合,可以保存null元素
     		|----HashSet(实现类):基于HashMap,HashMap基于HashCode,是一个非线程安全。
     			 HashMap底层是键值对的数组+链表+红黑树
     				判断重复的机制:
     					1. 先判断元素的hashCode值
     					2. 在hashCode相等的情况下,继续判断equals比较,如果都相等认为是同一个元素
     		|----LinkedHashSet(是一个有序的HashSet,在HashSet基础上维护了一个链表) 
    
     		|----TreeSet(实现类):基于TreeMap,TreeMap底层基于红黑树(特殊的二叉树,左边叉值小,右边叉值大,有自平衡机制)
     				1. 自然排序:元素类型必须实现了Comparable接口,且必须重写compareTo()方法
     					返回值  正数:升序
     				         返回值  负数:降序
     					返回值0:相同的元素,就不会添加了	
     				2. 定制排序:
     					在创建TreeSet对象的时候,传入一个Comparator接口的实现类,该实现类必须重写compare方法
     					  返回值  正数:升序
     				      返回值  负数:降序
     					  返回值 0:相同的元素
     			当自然排序和定制排序同时使用,定制排序优先级更高
     			一般情况下,默认排序用自然排序,特殊排序用定制排序
        	|-----Queue队列 
     		|---Deque双端队列
     	 	|---Stack栈
    

Key ************
usage scenarios:
If necessary, repeat the storage elements, with the List interface implementation classes in
the ArrayList: check if the change is more, with the ArrayList
the LinkedList: if multiple additions inserted, with LinkedList
Vector: If there is a thread-safe requirements for Vector

	如果是不能存放重复的元素,用Set接口下的实现类
 		HashSet:如果没有任何排序要求,用HashSet,因为效率高
 		TreeSet: 如果有排序要求用TreeSet,
 			如果是自然排序,需要元素实现Comparable接口,重写compareTo方法
 			如果是定制排序,需要创建TreeSet对象的时候,传入一个Comparetor接口实现类对象,、
 				重写compare方法
 			一般是默认排序用自然排序,特殊排序用定制排序
 		LinkedHashSet:如果需要元素不重复,并且,有存入和取出顺序要求用LinkedHashSet

集合遍历:
	集合遍历方式:只有List才有双向迭代器
	1. foreach()因为ArrayList间接实现了Iterable接口
	2. 迭代器:一个集合对象可以获取多个迭代器对象(多次调用方法),指针都是在初始位置(第一个元素前面)
		Iterator<E> iterator()  单向迭代器
			1. boolean hasNext() 如果迭代具有更多元素,则返回 true 。  
			2. Object next() 返回迭代中的下一个元素。  
			3. default void remove() 从底层集合中删除此迭代器返回的最后一个元素(可选操作)。 
		ListIterator<E> listIterator() 双向迭代器
			void add(E e) 
				将指定的元素插入列表(可选操作)。  
			boolean hasNext() 
				返回 true如果遍历正向列表,列表迭代器有多个元素。  
			boolean hasPrevious() 
				返回 true如果遍历反向列表,列表迭代器有多个元素。  
			E next() 
				返回列表中的下一个元素,并且前进光标位置。  
			int nextIndex() 
				返回随后调用 next()返回的元素的索引。  
			E previous() 
				返回列表中的上一个元素,并向后移动光标位置。  
			int previousIndex() 
				返回由后续调用 previous()返回的元素的索引。  
			void remove() 
				从列表中删除由 next()或 previous()返回的最后一个元素(可选操作)。  
			void set(E e) 
				用 指定的元素替换由 next()或 previous()返回的最后一个元素(可选操作)。  

	3.遍历集合方式3:通过普通for循环,利用集合size()方法和get(int index)	(不常用)

	注意:在使用迭代器遍历集合的时候,不能使用集合自己的方法(增删改)操作集合对象

Collections tools common method
Here Insert Picture Description
(above) to write a case:
requires use of standard classes design approach, a design class model Model class, has the following properties: chest, hip, waist.
Please add 6 model objects with TreeSet, then, with the natural order to achieve the following functions:
first in descending order according to Bust, Bust equal, then descending in accordance with hip, if the bust and hips are equal, then in ascending order according to the waist
and then implement a custom sort.

in conclusion:

        ArrayList:如果是查改多,用ArrayList
      	LinkedList:如果是增删插多,用LinkedList
 		Vector:如果是有线程安全要求用Vector
如果是不能存放重复的元素,用Set接口下的实现类
 		HashSet:如果没有任何排序要求,用HashSet,因为效率高
 		TreeSet: 如果有排序要求用TreeSet,
 如果是自然排序,需要元素实现Comparable接口,重写compareTo方法
 如果是定制排序,需要创建TreeSet对象的时候,传入一个Comparetor接口实现类对象,重写compare方法
 		一般是默认排序用自然排序(Comparable接口),特殊排序用定制排序(Comparetor接口实现)
 		LinkedHashSet:如果需要元素不重复,并且,有存入和取出顺序要求用LinkedHashSet
Published 14 original articles · won praise 1 · views 252

Guess you like

Origin blog.csdn.net/weixin_45061669/article/details/104760391