Java-Common advanced data Types __java

Source: Internet
Author: User

Attached: Java Common advanced data types--collection type

There are three types of collections in the Java language: set (set), list (lists), map (map), and each type of collection consists of three parts: interface, implementation, and algorithm.

A the set interface to achieve the operation of the set and the specific functions of the set to achieve the details of the mutual separation--set interface, List

interface, MAP interface

b The specific function implementation class of a set is essentially a concrete representation of various reusable data structures

The implementation class of the list interface has collection classes such as ArrayList, LinkedList, Stack, and vector, vector class

Provides the ability to implement a scalable array, and the array becomes larger as more elements are added. In the Delete

After some elements, the array becomes smaller.

The implementation class of set interface has a collection class such as HashSet, Linkedhashset and TreeSet

The implementation classes of the map interface are HashMap, Hashtable, Linkedhashmap, properties, and

TreeMap and other collection classes.

c) A set of algorithms that can be used to implement the collection interface of the various sets of functional implementation classes to provide such as sorting, lookup, intersection

and replacement and other aspects of the implementation of the function.

Second, List interface

The 1.List interface represents an ordered set, which controls precisely the insertion position of each element in an ordered set represented by the list interface, and accesses the members of the element by means of an integer index of the element (representing the position of the element in the collection), which is a collection that allows duplicate elements to appear.

2.List interface Principal Member Method:

1) voidadd (intindex,eelement) inserts the specified element at the specified position in the list

2) eget (intindex) returns the element at the specified position in the binding

3) Eremove (intindex) removes the element at the specified position in the collection

4) Eset (intindex,eelment) replaces the element at the specified position in the collection with the specified element

5) Boolean (O) appends the specified element to the tail of the list

6 Boolean (O) returns true if the list contains the specified element.

7 Boolean () returns True if the list contains no elements.

8 int () returns the number of elements in the list

9) Iteratoriterator () returns an iterator that iterates through the elements of a list in the correct order.

Implementation class for 3.List

List is represented in data structures in the form of arrays (ArrayList), vectors (vector), list (LinkedList), stacks (stack), and queues, respectively.

Vector sets and ArrayList sets are used as arrays to hold objects, except that the ArrayList collection itself does not have the characteristics of thread synchronization and cannot be used in multi-threaded environments, and the ArrayList set can be used to save on the overhead of system performance resulting from synchronization. The vector collection implements the support for thread synchronization, so in the context of multithreaded concurrent access, the collection itself can guarantee thread safety. In multithreaded concurrent access, you can design an object instance of a vector collection as a member property in a class, and you should design an object instance of the ArrayList collection as a local object.

publicclassuserinfo{

Listonevector=newvector ();

Publicvoidexecute () {

Listonearraylist=newarraylist ();

}

}

4.example

importjava.util.arraylist;importjava.util.list;publicclasslistexample{

Publicstaticvoidmain (String[]args) {//todoauto-generatedmethodstublistonelist=newarraylist (); OneList.add (" Zhangsan "); Onelist.add (" Lisi "); Onelist.add (Newinteger (" 1234 "));//Allow collection to contain different types of element Onelist.add (" Lisi "); Allows the collection to contain the same element System.out.print (onelist);

Practice: Create a teacher class teacher whose attributes are teacher number no, name names, gender sex and salary salary; then create three teacher objects and assign values, store the teacher object with the ArrayList implementation of the list interface, and output the corresponding teacher information.

Three, set interface

The 1.Set interface represents a disorder and does not allow elements to be duplicated in the set 2.Set interface Primary member method (O) If the specified element does not already exist in the set, add this element 2 Boolean (o) if the specified element exists in the set, it is removed. 3 int () returns the number of elements in the set (its capacity) of 4) Boolean () returns True if the set contains no elements.

6 Boolean (O) returns True if the set contains the specified element.

7) iterator () returns an iterator that iterates over the elements in this set.

Implementation of the 3.Set interface Class 1 Booleanset interface implementation class has HashSet, Linkedhashset and Tressset collection classes

The element objects in the HashSet collection are identified with the Hashcode code, so the contents of the member element objects that are placed are not repeatable; the element objects in the HashSet collection are unordered-the order in which the member elements are stored and added is not exactly the same. You need to override the Hashcode () method and the Equals () method for a custom type.

TreeSet collection: To use the TreeSet subclass if you want to arrange the entered data in an orderly fashion. The ordering here is the order in which objects are sized, not the order in which they are inserted into the collection.

Linkedhashset is the support for adding a two-way chain based on the set of HashSet.

4.example

Importjava.util.hashset;importjava.util.set;publicclasshashsetdemo01{publicstaticvoidmain (String[]args) {

Setallset=newhashset ();

Allset.add ("A");

Allset.add ("B");

Allset.add ("C");

Allset.add ("C");//add element//Add element//Add element//Repeat element, cannot join

Allset.add ("C");

Allset.add ("D");

Allset.add ("E"); System.out.println (Allset);

Output collection object, call ToString ()}

}//repeating element, cannot join//Add element//Add Element

Program run Result: [D,a,c,b,e] Practice: Create a teacher class teacher, whose attributes are teacher number no, name name, gender sex and salary salary; then create three teacher objects and assign values, use set sets to store the teacher objects, and output the corresponding teacher information.

Each member element in the map interface 1.Map is made up of a keyword (key) and a value (value). The keyword for the member element in the collection cannot be duplicated, and each key can only correspond to one value.

2.Map interface Principal Member method

1) V (key,value) associates the specified value with the specified key in this map

2 V (key) If there is a mapping relationship for a key, remove it from this map 3) V (key) returns the value mapped by the specified key, or null if the mapping does not contain a mapping relationship for the key.

4) Booleanisempty () returns True if the mapping does not contain a key-value mapping relationship. 5 int () returns the number of key-value mapping relationships in this map.

6 Boolean (key) returns TRUE if this map contains a mapping relationship for the specified key.

7 Boolean (value) returns true if the mapping maps one or more keys to a specified value.

Implementation classes for 3.MAP interfaces

The implementation classes of the map interface include collection classes such as HashMap, Hashtable, Linkedhashmap, properties, and TreeMap.

Unlike ArrayList and vectors, HashMap and Hashtable are designed in a way that is not thread-safe and more efficient than Hashtable collections. Applying a Hashtable collection object in a multithreaded, concurrent access environment does not require the developer to implement synchronous locking for its methods alone-because all the methods in the Hashtable collection are the methods that are defined synchronously. In multi-threaded, concurrent access applications, developers can design an object instance of a Hashtable collection as a member Property object of a class, and the object instance of the HashMap collection should be designed as a local object of the method.

publicclassuserinfo{

Maponehashtable=newhashtable ();

Publicvoidexecute () {

Maponehashmap=newhashmap ();

......

}

}

4.exampleimportjava.util.hashmap;importjava.util.map;publicclassmapexample{

Publicstaticvoidmain (String[]args) {//todoauto-generatedmethodstubmapcityhashmap=newhashmap (); CityHashMap.put (" Beijing "," Beijing "); Cityhashmap.put (2,3);//allow the collection to include different types of keywords and values cityhashmap.put (" Nanchang "," Nanchang ");

element of a name

}system.out.print (Cityhashmap); Cityhashmap.put ("Nanchang", "Jiangxi");//The back element overrides the same key previously

}

Iv. Iterator iterator

1. Iterator the collection for iteration (traversal). 2.Iterator commonly used Method 1 Boolean () returns 2 if there are still elements that can be iterated Enext () returns the next element of the iteration

3. Example1.listl=newarraylist ();

2.l.add ("AA");

3.l.add ("BB");

4.l.add ("CC"); true

5.for (Iteratoriter=l.iterator (); Iter.hasnext ();) {

6.

7.stringstr= (String) iter.next (); System.out.println (str);

8.}

9./* iterators for while loops

10.iteratoriter=l.iterator ();

11.while (Iter.hasnext ()) {

12.stringstr= (String) iter.next ();

13.system.out.println (str);

14.}

15.*/

Practice: Using iterators to export teacher Information Learn Java partners with Java live Open class thinking Exchange Group: 175161984 (← long copy) to obtain learning materials can be

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.