(JAVA in the following important order
· Goal-oriented
·Class set structure
·JDBC
·File programming
Program this season
First explain the following:
·Collection
·List
·Set
·Map
·Iterator
·ListIterator
·Enumeration
·for...each sentence (new feature of JDK1.5)
Comparable interface
·Binary tree
1. Class set structure
(1) The purpose of generating the class set
The class set is simply a dynamic target array, the target array can be changed in size, and the target can be added, deleted, and output at will. All classes are stored in the java.util package.
(2) Division of clusters
1. Consignment single value: Collection
2. Register a pair of values: Map
3. Output: Iterator
(3) Detailed concept of class set
lt;1gt;Collection (only one value can be registered)
- Primary function: for output use
- Sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>ist (repeated elements are allowed, and the order of participation is the order of output)
-Subclass: ArrayList, which was launched after Java2, is a new class, uses asynchronous management methods, and has higher functions
-Subclass: Vector, which was launched in JDK1.0. Because it is an old class, it has many functions that List does not have. It uses a synchronization management method, and its thread safety is high and it will be slower. In addition to using the Iterator output, you can also use the Enumeration for output. Both are generic. The Vector subclass allows direct use of addElement(Objectobj), and can also add elements to the collection.
-Added methods by yourself:
- Take each object: publicObjectget(intind);
- Subclass: Stack (stack)
- Sub-interface: Set (repeated elements are not allowed, repeated elements will be replaced)? How can we distinguish repeated elements?
- Subclass: HashSet: is an unordered register
- Subclass: TreeSet: Ordered storage, implemented through the Comparable interface
-Common method:
- add an object: publicbooleanadd(Objectobj)
- Get the length of the class set: publicintsize();
- Determine whether the content in the set is empty: publicbooleanisEmpty()
-Deleting an object: publicbooleanremove(Objectobj): must involve the comparison of the object.
- Instantiate the Iterator target: publicIteratoriterator();
lt;2gt;JDK1.5 provides a convenient output operation: for...each sentence.
lt;3gt;Iterator interface output is the most common
-Common methods
-publicbooleanhasNext(): Determine if there is a next element
-publicObjectnext(): take out the next element
- The sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>istIterator, which can perform bidirectional output, is only valid for the List interface
-Common methods: with comprehensive content in the Iterator interface
-publicbooleanhasPrevious()
-publicObjectprevious()
lt;4gt;Enumeration (also participate after 1.5, generic support ha~)
-Common method:
- Determine if there is a next element: publicbooleanhasMoreElements()
- Take out elements: publicObjectelement()
5. Map (register a pair of values)
-Function: Unlike the function of Collection, the primary function of Map is for searching.
-be careful:
- When using Map operations, iterator cannot be used directly for output.
-And there cannot be duplicate keys in it.
- Assuming that you have to use Iterator for output, you must do the following process:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;The difference between key and value
-Common subclasses:
-HashMap: unordered storage, is a new class, launched in JDK1.2, asynchronous management, high function
-Hashtable: It is an old class, launched when JDK1.0, it is thread-safe and has lower functions
-Properties (emphasis): Properties file operation class
-TreeMap: ordered storage
-Common method:
-Retain data in the call set: publicObjectput(Objectkey, Objectvalue): The value returned is
-Search data from the collection: publicObjectget(Objectkey): Search by key.
- Turn Map data into Set instances: Setlt;Map.Entrylt;K,Vgt;gt;entrySet()
- Turn a comprehensive key into a set instance: Setlt;Kgt;keySet()
- Turn a comprehensive vlaue into an instance of the Collection interface: Collectionlt;Vgt;values()
-Map.Entry: keep key and value
-publicObjectgetKey()
-publicObjectgetValue()
-Notes on storing keys:
################Michael dividing line####################
2. Code Explanation
(8)Vector
Use the List interface to receive the target of the Vector
Similar to Demo10, a subclass of Vector has been changed. Let's see the effect:
Use Enumeration for output
The Vector subclass allows to use addElement(Objectobj) directly, and can also add elements to the call set, see the effect below:
(9)TreeSet
Take a look at the effect and sort it~
How to sort? Can any target be sorted? Verify it~!
Look at the effect, it's wrong~
Suppose you write a goal yourself, you can't sort it
How is TreeSet sorting managed? Comparable (Comparable interface) is used
Suppose a class wants to use TreeSet for sorting operation, the target class must implement the Comparable interface. Together specify the ordering rules (binary trees).
Implement the above content: Can the previous String be sorted? It must be.
((Student)obj).name downcast ha~
Take a look at the effect:
Change the above needs: I left a title before:
Declare a student class, which contains name, age, and achievement attributes, sorted by achievement from high to low, assuming that the results are the same, then sort by age from low to high.
lt;1gt;, you can directly use TreeSet to sort
lt;2gt;, you can use the Arrays.sort method to sort directly
There is no difference in the core implementation of the two methods.
The compareTo() method in the Comparable interface can only return three values
·If less than: return -1
·If greater than: return 1
·If equal to: then return 0
Take a look at the effect:
Assuming the results are the same, then sort by age from low to high~
Take a look at the effect:
Assuming that TreeSet is used here, it is normal to add content to it
Take a look at the effect:
The principle of the above sorting is a binary algorithm~
(10) Binary algorithm (understanding)
Suppose we now have the following strings:
How to sort "A", "X", "B", "Y", "Z"?
A
X
B
Y
Z
In-order traversal: ABXYZ---gt; sort
Demo21:
importjava.util.
;
classBi)