(5) next to the previous article, let's continue to discuss a small problem that may occur during collection class traversal-concurrentmodificationexception
We use arraylist as an example. When traversing a list, we can use the following three forms:
(1) Using Indexes
(2) Use iterator
(3) Use an enhanced for Loop
If the elements in the List need to be deleted during traversal, improper use will trigger: Java. util. concurrentmodificationexception
Public static void main (string [] ARGs ){
List <string> List = new arraylist <string> ();
List. add ("1"); list. add ("2"); list. add ("4"); list. add ("6"); list. add ("3 ");
Useindex (list );
Useiterator1 (list );
Useiterator2 (list );
Useenhancedfor (list );
// In fact, this Traversal method of useiterator2 and useenhancedfor is equivalent.
}
Public static void useindex (list <string> srclist ){
List <string> List = new arraylist <string> (srclist );
// Use index. No problem
For (INT I = 0; I <list. Size (); I ++ ){
String S = list. Get (I );
If (integer. parseint (s) % 2 = 0 ){
List. Remove (I --); // note -- here. This sentence can be replaced with the following two sentences.
// List. Remove (s );
// I --;
}
}
System. Out. println (list );
}
Public static void useiterator1 (list <string> srclist ){
List <string> List = new arraylist <string> (srclist );
// Use iterator and iterator. Remove (). OK is OK.
For (iterator <string> it = List. iterator (); it. hasnext ();){
String S = it. Next ();
If (integer. parseint (s) % 2 = 0 ){
It. Remove ();
}
}
System. Out. println (list );
}
Public static void useiterator2 (list <string> srclist ){
List <string> List = new arraylist <string> (srclist );
// Use iterator and list. Remove ().
For (iterator <string> it = List. iterator (); it. hasnext ();){
String S = it. Next ();
If (integer. parseint (s) % 2 = 0 ){
List. Remove (s );
}
}
System. Out. println (list );
}
Public static void useenhancedfor (list <string> srclist ){
List <string> List = new arraylist <string> (srclist );
// Use an enhanced for loop.
For (string S: List ){
If (integer. parseint (s) % 2 = 0 ){
List. Remove (s );
}
}
System. Out. println (list );
}
The difference between useiterator1 and useiterator2 is that the use of list. Remove () and the use of iterator. Remove ().
List. iterator () generates a new iterator object. After iterator is created, a single-chain index table pointing to the original list will be created. When the number of original objects changes, the content of this index table will not change synchronously,
Therefore, when the index pointer moves backwards, the object to be iterated cannot be found. Therefore, according to the fail-fast principle, iterator immediately throws java. util. concurrentmodificationexception.
Therefore, iterator cannot be changed when it is working. However, you can use the iterator method to remove () to delete objects. The iterator. Remove () method will maintain index consistency while deleting the current iteration object.
6. Let's talk about the date that is very common in the project.
Java contains dates. util. date, Java. SQL. date, Java. SQL. time, Java. SQL. timestamp, Java. SQL. date, Java. SQL. time and Java. SQL. timestamp is Java. util. date subclass. Java. SQL. date corresponds to the date type of the database (year, month, day), Java. SQL. time corresponds to the database time (hour, minute, and second), Java. SQL. timestamp corresponds to the timestamp of the database (year, month, day, hour, minute, second), Java. SQL. timestamp and Java. util. date is similar, but Java. util. the seconds stored by date are all integers, Java. SQL. timestamp can also store decimal seconds, so it is more accurate.
Java. SQL. Time is similar to Java. util. date. It only provides a class corresponding to the database type, and most of the methods have been discarded.
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Time time = new time (system. currenttimemillis ());
System. Out. println (SDF. Format (time ));
Java. SQL. Date = new java. SQL. Date (system. currenttimemillis ());
System. Out. println (SDF. Format (date ));
Java. util. Date date2 = new java. util. Date (system. currenttimemillis ());
System. Out. println (SDF. Format (date2 ));
The above three outputs are the same.
The resultset when accessing the database. getdate (); and preparedstatement. setdate () The date here is Java. SQL. date, even if your database is stored in the year, month, day, hour, minute, second, but use resultset. when getdate () is read, the information of the time, minute, and second will still be lost:
While (Rs. Next ()){
Java. SQL. date Birth = Rs. getdate ("birth ");
System. Out. println (SDF. Format (birth ));
}
In this case, the information of the hour, minute, and second will be lost. Therefore, you need to use Rs. gettimestamp ("birth.
Generally, we use Java. util. date in the model object, which is inconsistent with the database. Therefore, when reading and writing data, you need to convert the data. If you need time, you have to replace it with timestamp.
7. The date format is simpledateformat.
Every time a date is formatted and output, we usually use a new simpledateformat object. However, it is expensive to create a simpledateformat instance, frequent creation of instances with a short life cycle during String Parsing results in poor performance, but simpledateformat is NOT thread-safe. A good solution to this problem is to use the threadlocal mode:
Public class dateutil {
Private Static threadlocal <Map <string, simpledateformat> dateformatholder = new threadlocal <Map <string, simpledateformat> (){
@ Override
Protected Map <string, simpledateformat> initialvalue (){
Map <string, simpledateformat> map = new hashmap <string, simpledateformat> ();
Map. Put ("yyyy-mm-dd", new simpledateformat ("yyyy-mm-dd "));
Map. Put ("yyyy-mm-dd hh: mm: SS", new simpledateformat ("yyyy-mm-dd hh: mm: SS "));
Return map;
}
};
Public static simpledateformat getdateformat (string pattern ){
Map <string, simpledateformat> map = dateformatholder. Get ();
Simpledateformat SDF = map. Get (pattern );
If (SDF = NULL ){
SDF = new simpledateformat (pattern );
Map. Put (pattern, SDF );
}
Return SDF;
}
Public static simpledateformat getdateformat10 (){
Return getdateformat ("yyyy-mm-dd ");
}
Public static simpledateformat getdateformat19 (){
Return getdateformat ("yyyy-mm-dd hh: mm: SS ");
}
}
Reference: http://ari.iteye.com/blog/757641