2016.3.9-3.10 (Java Collection Framework)

Source: Internet
Author: User
Tags set set

3.9

What is the role of the collection framework?

The main function of the collection framework is to organize the stored data in a certain structure and to access the data in a specific way.


What are the storage methods in the Java collection framework?

The object container in the Java Collection framework is divided into set, List, and map three types according to how the object is stored in it.

Set type objects are not sequential and cannot be duplicated;

Objects in the list type Object container are sorted by index order and can have duplicate objects;

The elements in the Map type Object container contain a pair of "key Object-Value object" mappings where the key object cannot be duplicated and the value object can be duplicated.

The above three storage methods correspond to the set, list, and map three interfaces in the Java Collection framework.


What are the functions of the Collection interface and the Iterator interface respectively?

The Collection Interface library is located at the top of the set interface and the list interface, is the parent interface of the set interface and the list interface, and the collection interface sets some of the bases that are common to collection objects.
This method, these methods are divided into basic operations, bulk operations and array operations

The iterator interface is an interface for iterating through a collection.


What are the characteristics of the list interface that inherits the collection interface?

The elements in the list are in order

List usually allows repeating elements

The implementation class of the list usually supports NULL elements,

The elements in the List object container can be accessed through an index.

ArrayList: Use the most extensive geometry element to increase or remove operations infrequently when used, most suitable for querying.
LinkedList: Used when you need to increase or remove elements frequently in the middle of a collection.
Vectors: Similar to ArrayList, but vectors are thread-safe, so performance is lower than ArrayList.

Above efficiency: Efficiency sort: linkedlis > ArrayList >vector.


What is a generic type? How do I use generics?

Generics are allowed in the definition class, the interface is the specified type form parameter, and the formal parameter of this type will be determined when declaring the variable, creating the object (that is, the actual argument passed in).

Type-formal parameters can be defined in the class name, followed by < >.

3.10

The map interface is another important interface in the Java Collection framework that differs from the collection interface, which corresponds to a collection of correspondence between a key (key) and a value (value). That is
said that the map type Object container holds two sets of objects, one for saving the key in the map, and the other for saving value.

Key and value can be data of any reference type. Key cannot be duplicated, but value can be repeated.

Enter student's name, school number, grade, and grade according to grades
list<student> stus = new arraylist<student> ();

for (int i = 0; i <; i++) {
Student stu = new Student ("Zhang" + i, ten + I, + I,
New Random (). Nextint (100));
Stus.add (Stu);
}

map<string, list> map = new hashmap<string, list> ();

for (Student st:stus) {
if (St.getscore () >= 90) {
List List = Map.get ("A");

if (null = = list) {
List = new ArrayList ();
List.add (ST);
Map.put ("A", list);
} else {
List.add (ST);
Map.put ("A", list);
}

} else if (St.getscore () >= 80) {
List List = Map.get ("B");

if (null = = list) {
List = new ArrayList ();
List.add (ST);
Map.put ("B", list);
} else {
List.add (ST);
Map.put ("B", list);
}
}
}


HashMap class speed, no sorting
TreeMap class slow, sort

The Iteara interface is an interface for iterating through a collection. Traversal refers to the process of extracting each element from a set. In the collection interface, there is a iterator method,
This method allows you to return a iterator object. With this object, you can iterate through all the elements in the collection.

Lis uses iterator traversal
list<student> stus = new arraylist<student> ();

for (int i = 0; i <; i++) {
Student stu = new Student ("Zhang" + i, ten + I, + I,
New Random (). Nextint (100));
Stus.add (Stu);
}

Iterator<student> iter = Stus.iterator ();
while (Iter.hasnext ()) {
Student stu = Iter.next ();
System.out.println (Stu);
}


Set uses iterator traversal
set<student> stus = new hashset<student> ();

for (int i = 0; i <; i++) {
Student stu = new Student ("Zhang" + i, ten + I, + I,
New Random (). Nextint (100));
Stus.add (Stu);
}

Iterator<student> iter = Stus.iterator ();
while (Iter.hasnext ()) {
Student stu = Iter.next ();
System.out.println (Stu);
}

The traversal method of map
map<string, integer> map = new hashmap<string, integer> ();
Map.put ("A", 1);
Map.put ("B", 2);
Map.put ("C", 3);
Map.put ("D", 4);

Convert a map's key-value mapping to a set set using EntrySet
Set<map.entry<string, integer>> set = Map.entryset ();

Call set's iterator method to get its iterator
iterator<map.entry<string, integer>> mapiter = Set.iterator ();

while (Mapiter.hasnext ()) {
Next gets a entry object, not a direct key and value.
map.entry<string, integer> Entry = Mapiter.next ();

String key = Entry.getkey ();
Integer val = Entry.getvalue ();
System.out.println ("key:" + key + "\ T" + "value:" + val);
}

For (String Key:map.keySet ()) {
System.out.println (Map.get (key));
//}

2016.3.9-3.10 (Java Collection Framework)

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.