A collection of three
1 , the Map Overview:
Map Interface Overview: The key is mapped to the target value. A map can not contain duplicate keys , each key can only be mapped to a value at most.
Map interface and collection of different interfaces: Map is a double-row, single-column collection is; Map of Unique, collection sub-system set and only; a collection of data structures Map key for effective, nothing to do with value.
Map interface member method:
V put (K key, V value) // add data
V remove (Object key) // delete data
void clear () // clear all data
boolean containsKey (Object key) // if the collection contains the key
boolean containsValue (Object value) // if the value contained in the collection
boolean isEmpty () // is empty
int size () // number of key-value pairs.
V get (Object key) // obtain value through key
Set <K> keySet () // return all set consisting of a set of keys
Collection <V> values () // return all values collection consisting of a set of
Set <Map.Entry <K, V >> entrySet () // set the composition of the set of key values.
Traversing the map
In two ways:
Get all the keys, traversing keys, get the value through the key
Acquiring key object Set, by traversing the object key Set, obtain a key for each object, acquired by the object key and value key.
2 , the HashMap class Overview
Key is a hash table structure, you can guarantee the uniqueness of the key. key is disordered.
HashMap Case
HashMap<String,String>
HashMap<Integer,String>
HashMap<String,Student>
HashMap<Student,String>
. 3 , a LinkedHashMap Class Overview
Hash table and linked list implementation of the Map interface, with predictable iteration order. Ordered
. 4 , the TreeMap Class Overview
TreeMap class overview
Key is red-black tree structure, you can guarantee the sort key and uniqueness
5 , the Collections class overview and member methods
A set of tools for operations
Collections member method
public static <T> void sort(List<T> list) // 排序
public static <T> int binarySearch(List<?> list,T key) // 二分查找
public static <T> T max (Collection <?> coll) // get the maximum value
public static void reverse(List<?> list) // 逆序
public static void shuffle (List <?> list) // upset collection of content
Exception、File
1 , abnormal Overview:
Exception: Abnormal java program is error occur during operation.
Abnormal Origin: real-life issues are also a particular transaction, it can also be described by the form of java classes and packages to objects. In fact, Java is an object of the abnormal situation reflected in the description.
We have seen abnormal, subscript bounds exception, null pointer exception
2 , abnormal classification
Throwable: Throwable class is the Java language, any errors or abnormal superclass. Only when the object is such examples (or one sub-class) when, in order to be thrown by the Java Virtual Machine or Java throw statement.
Error: Error is a subclass of Throwable, indicating a serious problem for a reasonable application should not try to catch. Error (error) indicates an abnormal system-level error and the program does not have to deal with is an internal error or a hardware problem java runtime environment, for example, insufficient memory resources for this error, the program basically do nothing, nothing outside except out of operation select.
Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
RuntimeException is the superclass of those exceptions that might be thrown during the normal operation of the Java Virtual Machine.
Abnormal program: Throwable
Serious problems: Error
Question: Exception
The compiler problem: not RuntimeException exception
Runtime problems: RuntimeException
3 , exception handling
JVM default processing program: the output unusual name, location and cause of the error exception occurs in the console
Program to stop execution
Scheme discretion: two kinds of
Option One: try ... catch ... finally
Option Two: throws
try ... catch handling:
An exception handling program:
try{
Exception code that may occur
} Catch (Exception exception object name) {
Captured exception object
}finally {
No matter how to handle an exception, the code must be executed here.
}
A: try as possible inside the code, the code exception occurs in the try exception code, is not performed.
B: catch which must have content, even give simple tips
Multiple exceptions:
Same level case
Presence on the lower level of
JDK7 new features and precautions
try{
}catch(){
}catch(){
}
C: clear can clear as much as possible, do not use big to handle
D: Abnormal same level of relationship, who should and who does not matter, if the parent-child relations, the first child after the father
Try{
} Catch (exception name | name of the exception | exception name variable) {
}
Consistent disadvantage treatment; same level must be an abnormal relationship between
Unusual and abnormal operation of the difference between compile time:
Java Exception is divided into two categories: abnormal compile-time and run-time exception. All instances of the class RuntimeException and its subclasses are called abnormal, other exception is the run-time exceptions when you compile
compile an exception
Java program must display processing, otherwise the program will error, not compile
Runtime exception
Without displaying processing, can also be treated as an exception and compile-time
. 4 , the Throwable Methods:
getMessage()
Gets the exception message, returns a string.
toString()
Gets the exception class name and exception information, returns a string.
printStackTrace ()
Gets the exception class name and exception information, as well as locations of the anomalies in the program. Return value void.
printStackTrace(PrintStream s)
The anomaly is usually the method used to save the contents of the log file for review.
5、throws
When you define a function method, need to be exposed so that the problem of the caller to deal with. Then identified on the method throws.
If this method does not resolve this error, throwing an exception
6、throw
Appears inside functional approach in some cases, the program can not continue to run, jump when needed, use throw the exception object thrown.
7 , throws and throw a difference
throws
In a later statement method, with the class name is unusual
Can talk to multiple exception classes, separated by commas
Represents an exception is thrown, handled by the caller of the method
One possibility expressed throws an exception, and not necessarily these exceptions occur
throw
Used in vivo, with the exception object name is
You can only throw an exception object name
It represents an exception is thrown, the statements in vivo treatment
throw an exception is thrown, throw it must perform some kind of an exception thrown
8 , we in the end how to deal with it abnormal
Principle: If the internal processing function can be a problem, with the try, if not treated, the caller referred to treatment, which is throws
the difference:
Follow-up procedures need to continue to try to run
Follow-up program does not continue to run on throws
For example:
Cold to eat on his own medicine like, try
I did not eat for several days medicine had good results H7N9, it would have to throw to the hospital
If the hospital is not a cure becomes Error
. 9 , the finally characteristic action face questions and
finally features
Controlled body will finally statement execution
Special circumstances: Before performing to finally jvm withdrew (such as System.exit (0))
finally the role
For releasing resources in the IO stream operations and database operations will see
finally face questions related
The difference between final, finally and finalize the
If you catch a return statement there, what finally code will execute it? If so, what is the return before or after the return.
finally performed before the inevitable return.
10 , custom exception
Custom exception
Inherited from Exception
Inherited from RuntimeException
Failure Notes:
When subclass overrides inherited methods, for subclasses must throw exception same exception class or subclass parent. (Bad father, son can not be worse than his father)
If more than one parent class throws an exception, subclasses override the parent class, you can only throw the same exception or is he a subset of the parent class subclass can not throw no exceptions
If the method is not covered by an exception is thrown, then the subclass method must not throw an exception if there is a sub-class method exception occurs, then the subclass can only try, not throws
11 , File class
File Overview: abstract file name and directory path representation.
Construction method:
public File(String pathname)
public File(String parent,String child)
public File(File parent,String child)
Member method:
Creating features:
public boolean createNewFile () // file created based on file path
public boolean mkdir () // Create Folder
public boolean mkdirs () // create multiple subfolders
Delete function:
public boolean delete () // delete a file or directory. (You can only delete the last time, multi-level can not be deleted)
Rename
public boolean renameTo(File dest)
Judgment function
public boolean isDirectory () // judge is not a directory
public boolean isFile () // judge is not a file
public boolean exists () // judgment is not present
public boolean canRead () // determine whether it can read
public boolean canWrite () // determine whether to modify, write operation
public boolean isHidden () // whether hidden files
The basic acquisition function
public String getAbsolutePath () // absolute file path
public String getPath () // file path
public String getName () // file name
public long length () // file length
public long lastModified () // file modification time
Advanced acquisition function
public String [] list () // returns more than some files, directories, the directory name string
public File [] listFiles () // return more than some files, directories, file objects in the directory