On the Java Foundation of the article, I think it can also be written in my other blog, is definitely original, and now share to everyone out.
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------
I. Overview
Objects are used to encapsulate unique data, and objects need to be stored if the number of objects is indeterminate. is stored using the collection container.
The Java Collection class is primarily responsible for saving and holding other data, so the collection class is also called the container class. Java collection classes are divided into: set, list, map three systems.
Where set represents an unordered, non-repeatable set;
The------list represents an ordered, repeatable collection.
-----Map Represents a collection that has a mapping relationship.
features of the collection in Java:
1, a container for storing objects.
2, the length of the collection is variable.
3, the base data type value cannot be stored in the collection.
Ii. sets of Frames
The origin of the collection framework:
The collection container has several specific containers because of the internal data structure.
The collection frame is formed by continuous upward extraction.
This part of the knowledge is still more, if you have not touched the text narrative must have seen you ignorant, first look at the overall framework diagram
The Java Collection class is primarily derived from two interfaces: collection and map, which are the root interfaces of the collection framework. Let's start by introducing the collection interface
Third, collection interface
1. Overview
Collection is the most basic set interface, and a collection represents a set of object, the collection element. Some collection allow the same elements while others do not. Some can sort and others can't.
The two interfaces that are derived from the collection interface are list and set.
2. Iteration
About the common operation of the collection do not say, we look at the API document will be very clear, here focus on the iteration.
How do I traverse every element in the collection? As we can see from the above set-frame diagram, it supports a iterator () method regardless of the actual type of collection, which
The method returns an iteration that uses the iteration to access each element of the collection one at a time.
Example:
<span style= "FONT-SIZE:14PX;" ><pre name= "code" class= "java" >Collection<String> C = new arraylist<string> ();// Define a collection ccollection<string> C1 = new arraylist<string> ();//define a collection C1c.add ("Jin");//add Element C.add ("Fulin"); C1.add ("UUU"); C1.add ("fff"); C.addall (C1);//Add C1 to c iterator<string> it = C.iterator ();//define an iterator Itwhile ( It.hasnext ())//read to the next element? Print System.out.println (It.next ()) for true;</span>
Iv. List
1. Overview
The list is an ordered collection, using this interface to precisely control where each element is inserted. The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript), similar to an array of java.
It includes three subcategories of ArrayList, linklist, vectors, and their relationships, such as
2. Listiterator interface
In an iteration, you cannot manipulate the elements in the collection through the methods of the collection object.
If you want to add or modify an operation, you need to use its sub-interface: Listiterrator.
Note: Only the list collection has this iteration capability.
Example:
<span style= "FONT-SIZE:14PX;" >List<String> List = new arraylist<string> (), List.add ("Jin"), List.add ("Fulin"); Listiterator<string> it = List.listiterator (); SYSTEM.OUT.PRINTLN (list), while (It.hasnext ()) {Object obj = It.next (), if (obj = = "Jin") {It.set ("BBB");//note is it's modification and addition, Instead of Listit.add ("AAA");}} SYSTEM.OUT.PRINTLN (list);} </span>
Five, Set
1. Overview
Set is a collection that does not contain duplicate elements, including two kinds, one is hashset, one is TreeSet, and the hash table and the structure corresponding to the data structure.
2, HashSet
HashSet: The internal data structure is a hash table and is out of sync.
A, how to guarantee the element uniqueness of the collection?
is to accomplish object uniqueness through the hashcode of the object and the Equals method. ‘
B. How do hash tables determine the same elements?
1, Judge Hashcode is the same?
2, if the ture, Judge equals is the same?
3, if also for ture, as the same element does not exist.
Therefore: if the element is to be stored in the HashSet collection, the Hashcode method and the Equals method must be overridden.
<span style= "FONT-SIZE:14PX;" ><pre name= "code" class= "java" >package cn.jinfulin.p.bean;/** * @author Jinfoling * */public class person implements C omparable<person>{//inherits comparable interface private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public person (String name, int.) {super (); this.name = Name;this.age = age;} Public person () {}public int hashcode () {<span style= "white-space:pre" ></span>//replication Hashcode encoding, which is generated automatically, can be customized. final int prime = 31;int result = 1;result = Prime * result + Age;result = Prime * result + (name = = null)? 0:name.has Hcode ()); return result;} public boolean equals (Object obj) {//Replication Equals method, if Hashcode is the same before judging if (this = = obj) return true;if (obj = = null) return false;if (GetClass ()! = Obj.getclass ()) return false; person other = (person) obj;if (age! = other.age) return false;if (name = = null) {if (other).name = NULL) return false;} else if (!name.equals (other.name)) return False;return true;}} </span>
Call:
<span style= "FONT-SIZE:14PX;" >private static void Myhashcodedemo () {hashset<person> hs = new hashset<person> (); Hs.add (New Person ("Jin Hs.add ("Fu"), Hs.add (New Person ("Fu"), "Hs.add" ("Lin"), and/*for (person p:hs) {///). Another form of the For loop, and the following iterator loops to express a meaning. System.out.println (P.getname () + ":" + p.getage ()); */iterator<person> it = Hs.iterator (); while (It.hasnext ()) {person P = It.next (); System.out.println (P.getname () + ":" + p.getage ());}} </span>
3, TreeSet
TreeSet: You can sort the elements in the set collection. is out of sync.
The way to judge the uniqueness of an element:
Is whether the return result of the CompareTo () or compare () method is 0, is 0, is the same element, does not exist.
TreeSet how the elements are sorted one:
1, so that the elements themselves have a comparative function, it is necessary to implement the comparable interface. Overrides the CompareTo method.
2, let the set itself have a comparative function, define a class implementation comparator interface, covering the Compare method. Pass the class object as a parameter to the constructor of the TreeSet collection.
<span style= "FONT-SIZE:14PX;" ><pre name= "code" class= "Java" >//method a public class person implements comparable<person>{// Implement the Cmopareable interface, define a generic type of personprivate string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} /** constructor * @param name * @param age */public person (String name, int age) {super (); this.name = Name;this.age = age;} Public person () {}public int compareTo (person p) {//Overwrite CompareTo method, because add generic, all arguments can be person type//person P = (person) O;int temp = This.age-p.age;return temp = = 0?this.name.compareto (p.name): temp;} </span>
called
<span style= "FONT-SIZE:14PX;" >private static void Mytreesetdemo () {treeset<person> ts = new treeset<person> (); Ts.add (New Person ("Jin" , Ts.add ("Fu"), Ts.add (New Person ("Fu"), "Ts.add" ("Lin"), and "New Person" ("a"); System.out.println (P.getname () + ":" + p.getage ());}} </span>
<span style= "FONT-SIZE:14PX;" ><pre name= "code" class= "Java" >//method two: public int compare (person O1, person O2) {int result = O1.getname (). Compare to (O2.getname ()); return result = = 0? O1.getage ()-o2.getage (): result;/* equals the above statement if (result = = 0) {if (O1.getage ()-o2.getage () ==0) {return 0;}} Return result;*/}</span>
called
<span style= "FONT-SIZE:14PX;" >private static void MyTreeSetDemo2 () {treeset<person> ts = new treeset<person> (new Comparebyname ()); Ts.add (New person ("Jin"), Ts.add ("Fu"), Ts.add (New Person ("Fu")), Ts.add ("Lin", 15)); for (person P:ts) {System.out.println (P.getname () + ":" + p.getage ());}} </span>
VI. MAP
1. Overview
The Map<k,v> collection is an interface, unlike the list collection and set set, which is a double-column collection and can give the object a name, i.e. (key).
In fact, the map collection is stored in key-value pairs.
key uniqueness must be guaranteed in the Map collection.
2. The difference between map and collection
Map: Add a pair of elements at once. Collection Adds an element at a time.
A Map is also called a double-column collection, which is called aCollection collection .
3. Examples of common methods:
<pre name= "code" class= "java" ><pre name= "code" class= "java" ><span style= "FONT-SIZE:14PX;" >public static void Main (string[] args) {map<integer, string> m = new Hashmap<integer, string> ();//Initialize M.PU T (1, "Jin"); M.put (2, "Fu"); M.put (3, "Lin"); M.put (4, "AAA");//M.clear (); Empty map//set<entry<integer, string>> sm = M.entryset (); One way to read the map is set<integer> SM = M.keyset ();//The second way to read the map iterator<integer> it = Sm.iterator (); while ( It.hasnext ()) {Integer key = It.next (); String value = M.get (key); System.out.println ("key:" + key + "," + "value:" + value ");} System.out.println (M.get (1)); The Get method obtains the value by key}</span>
4, map commonly used sub-categories:
|--hashtable: The internal structure is a hash table, which is synchronous. Null is not allowed as a key, and Null is used as a value.
|--properties: Information that is used to store key-value pairs of profiles, which can be combined with IO technology.
|--hashmap: The internal structure is a hash table, not synchronous. Null is allowed as the key, and null as the value.
|--treemap: The internal structure is a two-fork tree, not synchronous. You can sort the keys in the map collection.
Seven, the last
The content of this chapter is more, thank you can see here friends, want to learn, but also need everyone control chart, more finishing, more thinking, diligent review.
Java Foundation 5: Collections