Java ---- JSTL learning notes, java learning notes

Source: Internet
Author: User
Tags sorted by name

Java ---- JSTL learning notes (GO), java learning notes

Java container classes include List, ArrayList, Vector and map, HashTable, HashMap, and Hashset.

ArrayList and HashMap are asynchronous, while Vector and HashTable are synchronous. Therefore, Vector and HashTable are thread-safe, while ArrayList and HashMap are not thread-safe. Because synchronization takes machine time, the execution efficiency of Vector and HashTable is lower than that of ArrayList and HashMap.

The two interfaces derived from the Collection interface are List and Set.

List is an ordered and repeated set, and set is a set that does not need to be repeated.

Java's "STL" can be derived and understood from C ++. Java has the STL function of C ++, and these function classes are encapsulated in java. in the util package, these classes are collectively referred to as J-STL.

Util defines the following interface, the J-STL mainly implements the following interfaces-italic Representation

Collection (Java 2) List (Java 2) ObserverComparator (Java 2) ListIterator (Java 2) Set (Java 2)Enumeration Map (Java 2) SortedMap (Java 2)EventListener Map.Entry (Java 2) SortedSet (Java 2)Iterator (Java 2)

 

All of the J-STL classes we use are an implementation of these interfaces. The following is a brief introduction.

Collection Interface

Collection allows you to operate on object groups, which are located at the top layer of the class set hierarchy. The Collection interface is the basis for constructing the class set framework. It declares the core methods that all class sets will possess.

A class set cannot directly store values of the int, char, and double types. If you want to use these types, use an object, such as new Integer (1 ).

The common usage is as follows:

Boolean add (Object obj) adds obj to the call class set. If obj is added to the class set, true is returned. If obj is already a member of the class set or the class set cannot be copied, falseboolean addAll (Collection c) is returned) add all elements in c to the call class set. If the operation is successful (that is, the element is added), true is returned; otherwise, falsevoid clear () is returned () delete all elements from the call class set boolean contains (Object obj) If obj is an element of the call class set, true is returned. Otherwise, falseboolean containsAll (Collection c) is returned) if the call class set contains all the elements in c, true is returned; otherwise, falseboolean equals (Object obj) is returned. If the call class set is equal to obj, true is returned; otherwise, falseint hashCode () is returned, and the hash code boolean isEmpty () of the call class set is returned if If the call class set is empty, true is returned; otherwise, falseIterator iterator () is returned. The iteration program Boolean remove (Object obj) of the call class set deletes an instance of obj from the call class set. If this element is deleted, true is returned; otherwise, falseBoolean removeAll (Collection c) is returned to delete all elements of c from the call class set. If the class set is changed (that is, the element is deleted), true is returned; otherwise, falseBoolean retainAll (Collection c) is returned) delete all elements in the call class except the elements contained in c. If the class set is changed (that is, the element is deleted), true is returned; otherwise, falseint size () is returned, and the number of elements in the call class set is returned.

 

List Interface

The List interface is equivalent to a linked List, which is unordered and repeatable.

ArrayList class

The ArrayList class extends javasactlist and executes the List interface. ArrayList supports dynamic numbers that can increase as needed

Group. In Java, standard arrays are fixed. After an array is created, it cannot be extended or shortened.

You must know in advance how many elements an array can hold. However, you will not be able to know how much data is needed until running.

Group. To solve this problem, the class set framework defines the ArrayList. In essence, ArrayList is

Variable Length array. That is to say, ArrayList can dynamically increase or decrease its size. Array list with a raw size

Created. When it exceeds its size, the class set automatically increases. After an object is deleted, the array can be reduced.

 

Vector

Vector implements dynamic arrays. This is similar to ArrayList, but the two are different: Vector is synchronized, and it

Contains many methods left over from previous versions that do not belong to the class set framework. With the announcement of Java 2, Vector is duplicated

The new design extends actlist and implements the List interface. If no increment is specified, the vector size doubles in each allocation cycle.

Stack

Stack is a subclass of Vector. It implements a standard first-in-first-out Stack.

Sort list class

The listlist class extends AbstractSequentialList and executes the List interface. It provides a link list data knot

Structure.

Obviously, we need to understand the two data structures ArrayList and ArrayList, which can be analogous to the similarities and differences between linked lists and arrays.

The former inserts an element into the middle, causing a shift to the later element, which is less efficient, while the latter does not. Example:

private static void testArrayListandLinkedList(){long start = System.currentTimeMillis();List<String> list1 = new ArrayList<String>();for(int i=0;i<10000;i++){list1.add(0,"hello");}long end = System.currentTimeMillis();System.out.println("start:"+ start + " end:"+end+" time use :"+ (end-start)+"ms");start = System.currentTimeMillis();List<String> list2 = new LinkedList<String>();for(int i=0;i<10000;i++){list2.add(0,"hello");}end = System.currentTimeMillis();System.out.println("start:"+ start + " end:"+end+" time use :"+ (end-start)+"ms");}

 

Insert them to the first element. The efficiency of the shortlist is much higher:

Start: 1358144834832 end: 1358144834874 time use: 42 ms

Start: 1358144834875 end: 1358144834877 time use: 2 ms

Set Interface

The Set interface is equivalent to a Set, unordered, and repeatable.

The SortedSet interface is equivalent to an ordered set. The SortedSet interface extends the Set and describes the features of the Set in ascending order.

 

J-STL iterator

1. Call the iterator () method of the class set to obtain the iteration function of the class set header.

2. Create a loop that calls the hasNext () method. If hasNext () returns true, it performs loop iteration.

3. In the loop, call the next () method to obtain each element.

Boolean hasNext () returns true if more elements exist. Otherwise, falseObject next () returns the next element. If no next element exists, the NoSuchElementException exception void remove () is thrown to delete the current element. If you try to call the remove () method after the next () method is called, this causes IllegalStateException void add (Object obj) to insert obj into an element in the list. boolean hasNext () is returned when the next call to the next () method is called () if the next element exists, true is returned. Otherwise, false seboolean hasPrevious () is returned. If the previous element exists, true is returned. Otherwise, falseObject next () is returned. If the next element does not exist, A NoSuchElementException exception is thrown. int nextIndex () returns the subscript of the next element. If the next element does not exist, the Object previous ( ) Returns the previous element. If the previous element does not exist, a NoSuchElementException exception is thrown. int previusindex () returns the subscript of the previous element. If the previous element does not exist, -1 void remove () is returned to delete the current element from the list. If the remove () method is called before the next () method or previous () method is called, an IllegalStateException exception void set (Object obj) is thrown and obj is assigned to the current element. This is the last element returned after calling the next () method or the previous () method and the C ++ STL has a very obvious difference that the J-STL doesn't need ++ and -- to move, instead, use next and previous. Iterator it = l. iterator (); // get an iterator while (it. hasNext () {String s = (String) it. next (); System. out. println (s );}

 

Map Interface

Map Interface

The ing loop uses two basic operations: get () and put (). You can use the put () method to specify

Value ing. To get the value, you can use the keyword as a parameter to call the get () method. The call returns

Value. As mentioned above, ing is not a class set, but you can obtain the ing Class Set "View ". To achieve this

Yes. You can use the entrySet () method to return a Set containing elements in the ing ). To get the key

You can use the keySet () method. To obtain the value of the class set "View", you can use values ()

Method. The class set "View" is a means of integrating ing into the class set framework.

SortedMap Interface

The SortedMap interface extends Map, which ensures that all items are sorted in ascending order by keywords. The methods described by SortedMap are summarized in table 15-7. When no items in the ing are called, several of the Methods raise a NoSuchElementException exception. When the object is incompatible with the elements in the ing, A ClassCastException is thrown. An NullPointerException exception is thrown when a null object cannot be used for ing.

 

A Dictionary is an abstract class that represents a keyword/value repository. Its operations are similar to maps. Hashtable is the original java. A part of util is also a specific implementation of Dictionary.

 

TreeMap class

The TreeMap class uses the tree to implement the Map interface. TreeMap provides the ability to store keyword/value pairs in order.

And allows quick retrieval. It should be noted that, unlike hash ing, tree ing ensures that its elements follow

Sort key words in ascending order. Obviously, the TreeMap class shows excellent sorting.

Note that the keywords are sorted. However, in this case, they are sorted by name instead of by surname.

You can change the sorting by specifying a comparison function when creating the ing.

 

Comparison Functions

Both TreeSet and TreeMap store elements in order. However, to precisely define what sort order is used

Is a comparison function. By default, these classes are stored in a sequence called "natural order" by Java.

Store their elements, and this order is usually what you need (A is in front of B, 1 is in front of 2, and so on ). For example

If you want to sort elements in different ways, you can specify a Comparator pair when constructing a set or ing.

Image. This provides you with the ability to precisely control how to store elements in the sorting class set and ing.

The Comparator interface defines two methods: compare () and equals ().

class MyComp implements Comparator<Object> {public int compare(Object a, Object b) {//String aStr, bStr;//aStr = (String) a;//bStr = (String) b;//return bStr.compareTo(aStr);Integer ai, bi;ai = (Integer) a;bi = (Integer) b;return bi.compareTo(ai);}// no need to override equals}Map<Integer, String> treemap = new TreeMap<Integer, String>(new MyComp());

 

HashMap class

The HashMap class uses a hash to implement the Map interface. This allows some basic operations such as get () and put () running time.

It remains constant, even for large sets.

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.