I. Set frame
Collections are very important APIs in the AVA language;
Used to store multiple data;
Different data structures are implemented.
Three main interfaces:
Collection: The root interface of all collection classes, List, set is a common sub-interface, ArrayList, LinkedList, HashSet are the three commonly used classes.
Map: Mapping interfaces, storing key-value pairs ; HashMap is the class that is used below.
Iterator: Iterates through the collection's iteration interface.
The Java Collection Framework consists of many interfaces, abstract classes, and concrete classes, all in the Java.util package.
Second, collection interface
1. Concept
Collection<e> interface
Collection is the collection, which is the root interface of all collection classes, while the collection interface is a generic interface.
Method:
Add (E): adding objects;
Iterator<e> Iterator (): Generates an Iterator object, which allows you to iterate over the elements in the collection;
int size (); Gets the number of elements in the collection (the size is actual ).
2, Collection sub-interface
List: ordered collection, the elements are stored in order;
Set: unordered No duplicates of the set, that is, the deposited elements are not duplicated;
Queue: Queues, jdk1.5 version new interface, queue implements the "FIFO" storage structure, with all the basic operations of the collection, queue also provides some new methods of inserting, extracting, querying and so on.
Third, MAP interface
1. Map Interface Application
The map holds key- value pairs map<key,value> , the key value is not allowed to repeat , and if repeated, overrides.
Common methods:
Put (K key,v value) This method can save the key and value to the map object;
Get (Object Key) The method can return the corresponding valueaccording to the key value;
Size () returns the number of key-value pairs in the Map object.
2, the implementation of the Map interface class
HashMap the more commonly used map collection class, the key value of hashcode and equals guarantees element uniqueness ;
TreeMap can not only ensure that key is not duplicated, but also the value data can be sorted;
The classes defined in the HashTablejdk1.0 are implemented synchronously.
Iv. List Interface
1. Concept
Inheriting the collection interface and extending its own method, the elements in the list collection are related to the index , so the method of the list collection extension is related to the index.
Example: Add (int index,e) ... Adds an element at the corresponding index position.
2, List of the implementation of the class
ArrayList: Array list, the data is stored in array mode (the most used collection );
LinkedList: Linked list;
A collection in vector:jdk1.0, which enables synchronization.
A, ArrayList
ArrayList is called an array list, and the data is stored in an array, using contiguous memory storage, which is the implementation of variable-length arrays in the Java language .
Common methods of ArrayList:
Add data to the;-> collection of the Add (Object o);
Remove (Object o) to delete data from the collection;
Add (int index, Object Element) adds a data to an index position in the collection;
Get (int index), gets the information for a location in the collection.
Application of ArrayList:
Basic use: Do not use generics and are seldom used at present
public class Testarraylist {
public static void Main (string[] args) {
List list= new ArrayList ();
List.add ("string");
List.add (12);
List.add (' W ');
}
you can store any type of object in the .
Use of generics: objects can only hold the same type of object , or a compilation error will occur.
Test: (Run need to create user class, write out its argument constructs, get, set method)
public class Genericslist {
public static void Main (string[] args) {
. Create a User Object
User User=new User ("Zhang San");
User User1=new User ("John Doe");
Create a collection object that holds the user object
List<user> userlist=new arraylist<user> ();
Userlist.add (user);
Userlist.add (user1);
}
}
B, LinkedList
LinkedList are stored in a linked list , with data information stored on each node.
Common methods of LinkedList:
Add (E Element)-Adds a new node to the end of the list, and the data in that node is the object specified by the parameter element;
Add (int index, E Element)-Adds a new node to the list at the specified location, and the data in that node is the object specified by the parameter element;
Extension methods:
AddFirst (E Element)-Adds a new node to the head of the linked list, which is the object specified by the parameter element;
AddLast (E Element)-Adds a new node to the end of the list, where the data is the object specified by the parameter element.
Application of LinkedList:
For example:
public class Testlinkedlist {
public static void Main (string[] args) {
Create a User Object
User User=new User ("Uranus");
Create a collection object that holds the user object
List<user> list=new linkedlist<user> ();
List.add (user);
}
}
Traversal of C, ArrayList, and LinkedList (iterator traversal (Iterator)-Rarely used: Returns an iterator through a collection)
Enhanced for Loop: Information is also obtained by means of iterators
public class Genericslist {
public static void Main (string[] args) {
Create a User Object
User User=new User ("Zhang San");
User User1=new User ("John Doe");
Create a collection object that holds the user object
list<user> userlist=new linkedlist<user> ();
userlist. Add (user);
userlist. Add (user1);
For(User userinfo:userlist) {
System.out.println (userInfo);
}
}
}
For loop traversal: Gets the corresponding data information by the index value
public class Genericslist {
public static void Main (string[] args) {
Create a User Object
User User=new User ("Zhang San");
User User1=new User ("John Doe");
Create a collection object that holds the user object
List<user> userlist=new arraylist<user> ();
Userlist.add (user);
Userlist.add (user1);
for (int i=0;i<userlist.size(); i++) {//(actual number of stores)
System.out.println (userlist. Get (i));
}
}
}
Five, set interface
1. Concept
Inherits the collection interface, the elements stored in set are not duplicates, but are unordered, and the elements in the set are not indexed.
2. Implementation class of SET interface
HashSet: The underlying is a hash code value, based on the HashMap implementation, calling the Equals method to compare, all return false. ( compared to a virtual address, you can override the Equals method to make two objects of its class, and assign the property values to the same value, to the same object)
TreeSet: The element is not duplicated and the element is sorted (sorted by implementing the additional sub-interface of the set SortedSet interface)
You can sort the elements in the set collection in a specified manner. The stored object must implement the comparable interface.
3, HashSet and TreeSet
Common methods of HashSet: Add (E o) adds the specified element to the set.
For example: (Same as ArrayList)
public class Treesetexemple {
public static void Main (string[] args) {
User User1=new User ("Zhang Hua");
User User2=new User ("Zhang Xing Zhaoyuan");
Set userset=new TreeSet ();
Userset.add (user1);
Userset.add (User2);
Set<user> userset1=new hashset<user> ();
Userset1.add (user1);
Userset1.add (User2);
}
}
Common methods of TreeSet: Add (E O): Adds the specified element to the set (if it does not already exist in the set);
First (): Returns the current (smallest) element in a sorted set;
Last (): Returns the current final (maximum) element in the sorted set.
Objects stored in the TreeSet need to be implemented comparable
For example:
public class UserInfo implements comparable<userinfo> {
private String name;
private int age;
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
@Override
public int compareTo (UserInfo o) {( Judging by the conditions of their own definition )
if (O.getname (). Equals (name)) {
return 0;
}else return 1;
}
}
public class Treesetexemple {
public static void Main (string[] args) {
UserInfo user1=new UserInfo ();
User.setname ("Zhang Hua");
UserInfo user2=new UserInfo ();
User2.setname ("Wang Hua");
Set<userinfo> userset =new treeset<userinfo> ();
Userset.add (user1);
Userset.add (User2);
}
}
4. Traversal of HashSet and TreeSet
Enhanced for Loop
public class Setexemple {
public static void Main (string[] args) {
User User1=new User ("Wang Hui");
User User2=new User ("QW");
Set<user> userset=newhashset<user> ();
Set<user> userset=new treeset<user> ();
Userset.add (user1); Userset.add (User2);
Enhanced for Loop traversal
for (User User:userset) {
System.out.println (User.getusername ());
}
}
}
Vi. common implementation classes for map collections
1, HashMap
The key value of an element in HashMap cannot be repeated, that is, calling the Equals method to each other and returning to false. The order of arrangement is not fixed .
A, the common method of HashMap
Put<key,value>-> storage object;
Get (key);-> get the data corresponding to key;
KeySet () returns a set view of the keys contained in this map.
b, the use of HashMap
public class Hashmaptest {
public static void Main (string[] args) {
User User1=new User ("Wang Min");
User User2=new User ("Wang Hui");
Hashmap<string,user> map=new hashmap<string, user> ();
Map.put ("001", user1);
Map.put ("002", User2);
}
} (you need to create the user class, write a parameter construct set, GET, ToString method)
2, TreeMap
A, TreeMap common method and HashMap similar
b, the application of TreeMap
Suitable for traversing keys (key) in natural order or in a custom order;
TreeMap is sorted by key value, the key value needs to implement the comparable interface to implement the CompareTo method. TreeMap sort keys According to CompareTo logic
User User1=new User ("Wang Min");
User User2=new User ("Wang Hui");
Treemap<string,user> tree=new treemap<string,user> ();
Tree.put ("001", user1);
Tree.put ("002", User2);
(Based on the dictionary order of the strings)
TreeMap Sort by:
public class Scortinfo implements comparable<scortinfo> {
private int num; public int Getnum () {
return num;
}
public void setnum (int num) {
This.num = num;
}
@Override
public int compareTo (Scortinfo o) {
Return new Integer (This.num). CompareTo (O.getnum ());
}
public scortinfo (int num) {
This.num = num;
}
}
public class Treemaptest {
public static void Main (string[] args) {
User User1=new User ("Wang Min");
User User2=new User ("Wang Hui");
Treemap<scortinfo,user> tree=new treemap<scortinfo,user> ();
Tree.put (New Scortinfo (), User1);
Tree.put (new Scortinfo, user2);
}
}
3. Traversal of TreeMap and HashMap
public static void Main (string[] args) {
User User1=new User ("Wang Min");
User User2=new User ("Wang Hui");
Hashmap<string,user> map=new hashmap<string, user> ();
Map.put ("001", user1);
Map.put ("002", USE2);
Set Set=map.entryset ();
Iterator<entry<string, user>> it=set.iterator ();
while (It.hasnext ()) {
System.out.println (It.next (). GetValue (). GetUserName ());
}
Treemap<string,user> tree=new treemap<string,user> ();
Tree.put ("001", user1);
Tree.put ("002", User2);
Set Se=tree.entryset ();
Iterator<entry<string, user>> iter=se.iterator ();
while (Iter.hasnext ()) {
System.out.println (Iter.next (). GetValue (). GetUserName ());
}
} (pseudo code)
Summary of the collection framework in the Java language