Java.util Pack Learning notes One

Source: Internet
Author: User
Tags event listener interface pack tostring
Note Learning Java2sdk 1.4.0 Java.util has several important interfaces that are listed here as a summary of learning:

1 java.util.Enumeration
There are two methods hasmoreelements (), Nextelement (). Use the following methods:
Print all elements of vector v
for (Enumeratin e = v.elements (); e.hasmoreelements ();) {
System.out.println (E.nextelement (). toString ());
}
Notice here to call Nextelement () first to determine whether there are elements in the container, if not judged in the container without the next element will throw
Nosuchelementexception exception.
Of course, to traverse the elements in a vector, you can also use the vector size () and elementat (int) Two methods, but always feel that the
And the following methods are better (not tested, we can discuss)
Note: The only Java.util.StringTokenizer class that implements the interface in the Java.util package.

2 Java.util.Iterator
There are three methods Hasnext (), Next (), remove (). Iterator interface is used to replace the application of enumeration interface in Java set framework
The difference with enumeration: A Remove () method with a different name. Use the following methods:
Print vector v except for all elements with a value of "a"
Import java.util.*;

public class test{
public static void Main (string[] args) {
Vector v = new vector (4);
V.add ("a");
V.add ("B");
V.add ("C");
V.add ("D");
for (Iterator iter = V.iterator (); Iter.hasnext ();) {
if (Iter.next (). Equals ("a")) Iter.remove ();
Else System.out.println (Iter.next (). toString ());
}
}
}
Run result I was startled: only one value C was played, and exception Nosuchelementexception was thrown
Reason: The first call to the Iter.next () value of a, call Iter.remove () to delete it, correct.
The second loop calls the Iter.next () value B, enters the Else statement, and calls once Iter.next () value is C printing
The third loop calls the Iter.next () value D, enters the else statement, when there are no more elements in the vector V, and then calls next ()
An exception is thrown.
Conclusion: Like the enumeration interface, every time you call next (), you want to invoke Hasnext (), and you must prevent the invocation of the
Once Hasnext (), and then multiple calls next ();
The Remove () method can only delete the element specified by the next () method, and if you do not call next (), you should not call remove ()
Otherwise it throws a illegalstatementexception, which means you can't use the following code to do something like
To empty all the elements in the collection.
for (Iteraor iter = V.iterator (); Iter.hasnext ();) {
Iter.remove ();
}
Note: There is a iterator () method inside the Java collection collection that returns a iterator. The implementation depends on the class that implements the interface.
For example, Abstractcollection has a private class ITR implemented iterator, and the iterator () method returns this ITR.
All classes that implement the collection interface can call iterator () directly.
ArrayList, LinkedList, Vector, HashSet, TreeSet

3 Java.util.Comarator
There are two methods: Int Compare (object O1, Object O2), Boolean equals (object O)
The former with positive, 0, negative for two objects in the collation of the leading, equal, backward
The latter determines whether two objects are equal
Note: The class that implements the interface is collator. In addition, "effective Java" book on this interface seems to have a very detailed description

4 java.util.RandomAccess
This is just a flag interface, which indicates that the class that implements the interface supports fast (generally constant time) random access.
My understanding is that the time to find an element is independent of the number of elements in the container, and you can use the subscript to take the element. Below is the JDK
A comparison given:
for (int i = 0, int n = list.size (); i < n; i++)
List.get (i);
for (Iterator iter = List.iterator (); Iter.hasnext ();)
List.next ();
The former runs faster than the latter.
Note: The class that implements the interface has ArrayList, Vector

5 Java.util.EventListener
Flag interface, interfaces that all event listener interfaces must extend

6 Java.util.Observer can not understand how to use the actual: (

The most complex collection and map interfaces next time you learn, starve.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.