In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the Collection interface, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
# 15Collection-List-ArrayList/LinkedList/
* * methods in Collection interface
* A: add features
* boolean add (Object obj): add the element obj to the collection
* boolean addAll (Collection c): add all the elements in the c collection to this collection
* B: delete Featur
* void clear (): deletes all elements in the collection.
* boolean remove (Object obj): removes the element obj from the collection
* boolean removeAll (Collection c): removes all elements contained in c from the collection
* C: judgment function
* boolean isEmpty (): determines whether the collection is empty.
* boolean contains (Object obj): determines whether the element obj exists in the collection.
* boolean containsAll (Collection c): determines whether all the elements in the c set exist in the set.
* D: traversal function
* Iterator iterator (): is used to get every element in the collection.
* E: length function
* int size (): gets the number of elements in the collection
* F: intersection featur
* boolean retainAll (Collection c): retains the common elements in this collection and c
* G: convert a collection into an array
* Object [] toArray (): turn the collection into an array of type Object.
, /
/ *
* methods in the List interface (the List interface inherits the Collection interface, so only the specific methods of the List interface are listed below)
* unique features of List:
*
* A: add features
* void add (int index, Object obj): add the element obj in the location where the index is index
* B: delete Featur
* Object remove (int index): delete the element with index index and return this element
* C: modify the function
* Object set (int index, Object obj): change the index to the location of index, change it to obj, and return the element before the modification.
* D: get the function
* int indexOf (Object obj): returns the index of the element obj that appears for the first time in the collection
* Object get (int index): gets the element object whose index is index
* ListIterator listIterator (): list iterator
* E: intercept function
* List subList (int fromIndex, int toIndex): returns a new collection. The elements in this collection are part of the original collection. Look directly at the following code
*
# 15.08 _ Collection Framework (principle of iterator and source code parsing) (understanding)
Iterator it = iterator iterator (); / / get iterator
It.hasNext () / / determines whether there is an element. General while (it.hasNext ())
It.next (); / / get the element
# 15.12 _ Collection Framework (ListIterator) (understand)
* whether boolean hasNext () has a next
* whether boolean hasPrevious () has the previous one
* Object next () returns the next element
* Object previous (); returns the previous element
# 15.13 _ Collection Framework (unique to Vector) (not used in interview development)
* Overview of A:Vector classes
* specific features of B:Vector class
* public void addElement (E obj)
* public E elementAt (int index)
* public Enumeration elements ()
B:List has three sons. Who do we use?
Query multi-purpose ArrayList
Add and delete multi-purpose LinkedList
If all have more ArrayList
Collection framework (unique to LinkedList) (mastery)
* Overview of A:LinkedList classes
* specific features of B:LinkedList class
* public void addFirst (E e) and addLast (E e) / / add elements at the beginning / / add elements at the end
* public E getFirst () and getLast () / get the first element / / get the last element
* public E removeFirst () and public E removeLast () / / Delete the first element / / delete the last element
* public E get (int index); / / get the element at the index
_ collection framework (stack and queue data structures) (master)
* Stack
* first in, last out
* queue
* first in, first out
_ collection framework (generics overview and basic usage) (master)
* A: Overview of generics
* B: benefits of generics
* improve security (convert runtime errors to compile time)
* save the trouble of turning around
* C: basic use of generics
* must be a reference data type
* D: precautions for using generics
* generics must be consistent before and after, or subsequent generics can be omitted (1.7 new feature diamond generics)
Collection Framework (Overview and use of enhanced for) (Mastering)
* A: Overview of enhanced for
* simplify traversal of arrays and Collection collections
* B: format:
*
For (element data type variable: array or Collection collection) {
Just use a variable, which is an element
}
_ collection framework (can the three iterations be deleted) (master)
* ordinary for loop, which can be deleted, but the index should be--
* the iterator can be deleted, but the iterator's own remove method must be used, otherwise a concurrent modification exception will occur
* enhanced for loop cannot be deleted but can only be traversed
_ Collection Framework (Overview and use of static imports) (understand)
* A: Overview of static import
* B: format:
* import static package name … . Class name. Method name
* can be imported directly to the level of the method
* C: notes
* methods must be static. If there are multiple static methods with the same name, it is easy not to know who to use?
To use it at this time, you must add a prefix. Thus it can be seen that it is of little significance, so it is generally not necessary, but it must be able to understand.
_ collection framework (overview and use of variable parameters) (master)
* A: Overview of variable parameters
* do not know how many parameters to define when defining a method
* B: format
* the modifier returns the value type method name (data type … Variable name) {}
* C: notes:
* the variable here is actually an array
* if a method has variable parameters and has multiple parameters, then the variable parameter must be the last
/ * *
* * A: Overview of variable parameters
* do not know how many parameters to define when defining a method
* B: format
* the modifier returns the value type method name (data type … Variable name) {}
* C: notes:
* the variable here is actually an array
* if a method has variable parameters and has multiple parameters, then the variable parameter must be the last
, /
Public static void main (String [] args) {
Int [] arr = {11, 22, 33, 44, 55}
/ / print (arr)
Print (11, 22, 33, 4, 55)
System.out.println ("-")
/ / print ()
}
/ * public static void print (int [] arr) {
For (int I = 0; I < arr.length; iTunes +) {
System.out.println (arr [I])
}
} * /
Public static void print (int... Arr) {/ / variable parameter is actually an array
For (int I = 0; I <
Mistake! No catalog entries were found.
Arr.length; iTunes +) {
System.out.println (arr [I])
}
}
Collection framework (use of the asList () method of the Arrays utility class) (master)
* A: case demonstration
* use of the asList () method of the Arrays utility class
Integer [] I = {1, 2, 3, 4, 5}
List l = Arrays.asList (I); / / converting arrays to collections can only use arrays of reference types
System.out.println (l)
* set rotation array of toArray (T [] a) generic version in Collection
_ collection framework (ArrayList nested ArrayList for collection nesting) (master)
* A: case demonstration
* ArrayList nested ArrayList for collection nesting
HashSet method
1) the basic data type removes duplicate elements and has no index (different in and out)
2) reference data type: to override the toString method
If you want to remove repetition, override the hashCode () method and the equals () method.
The principle set by Hashcode (): / if the value of the member variable of the object is the same, then find a way to make the return value of this method the same, otherwise the return value is different
If you want to remove duplicate elements in HashSet, you must override the hashCode () and equals () methods
LinkedHashSet method
Features: the underlying layer is a linked list, which implements the only one in the Set () collection that can guarantee how to save and how to get the function.
Because it is a subclass of HashSet, the index also ensures the uniqueness of the element.
TreeSet Collection:
General types are used to sort elements and ensure that elements are unique (remove duplicate elements and sort)
Reference data type: first: override the toString () method; (if you don't override the output is the address value)
When implementing interface implements comparable, the class name will report an error (ctrl+1).
Automatically create a method compareTo (class name) {
Judge member variable
}
None of the above three steps is indispensable.
/ * @ Override
/ / sort by age
Public int compareTo (Person o) {
Int num = this.age-o.age; / / Age is the main condition for comparison.
Return num = = 0? This.name.compareTo (o.name): num;// name is a secondary condition for comparison
} * /
/ * @ Override
/ / sort by name
Public int compareTo (Person o) {
Int num = this.name.compareTo (o.name); / / name is the main condition
Return num = = 0? This.age-o.age: num; / / Age is a secondary condition
} * /
/ *
* aaa
* bbb
, /
Public int compareTo (Person o) {
Int length = this.name.length ()-o.name.length (); / / comparison length is the main condition
Int num = length = = 0? This.name.compareTo (o.name): length; / / comparison content is a secondary condition
Return num = = 0? This.age-o.age: num; / / Comparative age is a secondary condition
}
Handwritten interview
Public boolean equals (Object obj) {
System.out.println ("did you execute")
Person p = (Person) obj
Return this.name.equals (p.name) & & this.age = = p.age
}
@ Override
Public int hashCode () {
Final int NUM = 38
Return name.hashCode () * NUM + age
} * /
HashSet ()
* 1. Characteristics
* TreeSet is used to sort. You can specify an order in which objects are stored in the specified order.
* 2. Mode of use
* a. Natural sequence (Comparable)
* the stored object is promoted to Comparable type in the add () method of the TreeSet class
* compare the compareTo () method of the calling object with the objects in the collection
* Storage based on the results returned by the compareTo () method
* b. Comparator order (Comparator)
* you can create a Comparator when you create a TreeSet
* if subclass objects of Comparator are passed in, then TreeSet will sort according to the order in the comparator
* the compare () method in the Comparator interface is automatically called inside the add () method to sort.
* the object called is the first parameter of the compare method, and the object in the collection is the second parameter of the compare method.
* c. The difference between the two ways
* the TreeSet constructor does not pass anything. By default, it follows the order of Comparable in the class (error ClassCastException is reported if no error occurs)
* if Comparator is passed into TreeSet, priority will be given to Comparator.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.