(JAVA in the following order of priority
· Goal-oriented
·Class set framework
·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 intention of generating the class set
In short, a class set is a dynamic target array, the size of this target array can be changed, and the addition, deletion and output of the final target can be arbitrarily. All classes are stored in the java.util package.
(2) Distinguishing of class sets
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, 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 the 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, and the repeated elements will be replaced)? How to distinguish the repeated elements?
- Subclass: HashSet: is an unordered register
- Subclass: TreeSet: Ordered storage, which is terminated 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 the 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.
-If 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 (points): Properties file operation class
-TreeMap: ordered storage
-Common method:
-Retain data in the call set: publicObjectput(Objectkey, Objectvalue): returns value
-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
(1)List instance
Goal-oriented clearly states that subclasses can instantiate their interfaces
In the use of the List interface, it can be found that duplicate elements are allowed in it.
The same, if the current code uses Collection to receive it is also possible
The previous code compiles with some warnings:
Note that <imgsrc="biggrin.gif"smilieid="3"border="0"alt=""/>emo02.java uses unchecked or unsafe operations.
Note: To understand the details, recompile with -Xlint:unchecked.
Saying is an unsafe operation and needs to be restricted.
Before JDK1.5, arbitrary data can be inserted into the class set. Since the received type is Object, but after JDK1.5, the generic technology has been added, so it can be clearly declared when all the interfaces are defined. After all, what is added inside is the target.
The advantage of doing this: If you use Object to receive directly, then any data object can be added to it.
Nowadays, data is received by strings. If the same type is used to receive data and the two types do not match, there must be a type conversion exception~
This program can participate in instanceof to discriminate, and then perform transformation after discrimination. But this will increase the complexity of the code, which is very annoying~ You can set only one type of target at the runtime~
This ensures that each target in the target array is of the same type. What we're talking about these days is just a specification in development, and a set is all about the same goal.
Determine if it is empty: list.isEmpty()
Deleting an object: publicbooleanremove(Objectobj): must involve the comparison of the object.
Take a look at the effect:
(2) Set instance
Use a HashSet subclass to instantiate the Set interface and investigate its operation
Take a look at the effect:
The Set interface does not allow duplicate elements, the duplicate elements will be replaced and the HashSet is an unordered register (3) for...each sentence (understanding)
for (type target name: collection name) --gt; JDK1.5 new features ha~
(4)Iterator
Iterator can only output from front to back. If you want to output in both directions, you can use its sub-interface - ListIterator
re
[Zero Basic Learning JAVA] JavaSE Use Part-35.One of JAVA Class Sets.doc)