One) Map
The author says the ability to map objects to other objects is the killer of programming problems.
Indeed, for example, to see the distribution of random numbers, if it is a random number, then 10,000 times to produce a random number within 20, each number should be similar to the number of occurrences.
public class TestMap {public static void Main (string[] args) { map<integer,integer> Map = new Hashmap<in Teger,integer> (); Random r = new random (+); The map is empty System.out.println (Map.get (1)); for (int i = 0; i < 10000; i++) { int j = R.nextint (ten); Integer temp = Map.get (j); Map.put (J, temp = = null? 1:temp+1); } SYSTEM.OUT.PRINTLN (map);} } result:null//{0=994, 1=1033, 2=1010, 3=1014, 4=958, 5=1000, 6=1052, 7=980, 8=946, 9=1013}
The numbers are indeed randomly distributed, and the map is very useful.
Map is also used in multidimensional.
map<person,list<tv>>;
) Queue
First-out, buy the fund, due to the late purchase, but the early fund has been full 1 months, so do not redeem fees, and then asked the customer service found, first buy the first redemption, and then his big pat thigh, this is not so-called queue design?
LinkedList implements the queue interface.
Yes, often used to random, unexpectedly forget to say why the author used 47, in fact, the random parameter is passed in a calculated seed, the default is the system time, 47 in his view has been "magic number."
public class Testqueue {public static void Main (string[] args) { queue<integer> q = new linkedlist< Integer> (); Random r = new random (+); for (int i = 0; i < i++) { //Inserts an element into the tail q.offer (r.nextint); } Return to Team head System.out.println (Q.peek ()); SYSTEM.OUT.PRINTLN (q); }}
First, Priorityqueue
See examples directly:
public class Testpriorityqueue {public static void Main (string[] args) { String s = "What ' s your favorite number , 1 or 2? "; list<string> L = arrays.aslist (S.split ("")); Priorityqueue<string> PQ = new priorityqueue<string> (l); while (Pq.peek ()!=null) { System.out.print (pq.remove () + "");}}} Result:[,,,,, B,, 1,, I, E, N, H, ',, A, O, 2,?, Y, T, T, O, U, u, M, R, E, R, F,,, S, A, V, R, O, W, R]
First-class highest pop-up first, in the priority queue the smallest value has the highest priority, if it is a string, from the above, the space is the highest priority.
The direct output is not sorted, which is different from what I had expected.
To sequentially output, you need to use the Peek method, return to the team header, NULL to return NULL, and then remove the high priority will come out.
public static void Main (string[] args) { String s = "Werwerwer"; list<string> L = arrays.aslist (S.split ("")); Priorityqueue<string> PQ = new priorityqueue<string> (l); while (pq.peek () = null) { System.out.print (pq.remove () + ""); } PQ = new priorityqueue<string> (l); System.out.println (Pq.peek ()); Pq.remove (); System.out.println (Pq.peek ()); Pq.remove (); System.out.println (Pq.peek ()); } Result:e e E R r W W W EE
What made me wonder was that there were spaces in the string. This looks at the string and comes back to the solution.
) foreach and iterator
foreach Traversal:
public class Box {public static void Main (string[] args) { collection<string> c = new linkedlist<string& gt; (); String s = "You is so great"; Collections.addall (C,s.split ("")); for (string string:c) { System.out.println (string);}} }
The original foreach was introduced by Java SE5 because it also introduces the Iterable interface, which generates iterator iterator method, iterable is used by foreach to move through the sequence. So classes that implement iterable interfaces can be used with foreach statements.
In fact, Iterable has so many sub-interfaces, the next implementation of the collection interface naturally also realized the Iterable interface, so also applicable.
Summarize:
first look at the container classification chart :
The point line is the interface, the solid line is the concrete class, the hollow arrow refers to implements the interface, the solid arrow refers to a class can produce the object that the arrow points to the class.
1, the array can hold objects, storage of basic types of data, can be multidimensional, that is, capacity can not be changed.
2, collection save a single element, map save key value pairs.
3, list and array similar, but list can automatically expand capacity. A large number of random accesses using ArrayList, often insert delete operations, with LinkedList.
public class Box {public static void Main (string[] args) { long start = System.currenttimemillis (); Random r = new random (+); linkedlist<integer> L = new linkedlist<integer> (); for (int i = 0; i< 1000000; i++) { l.add (R.nextint ()); } Long end = System.currenttimemillis (); System.out.println (End-start); Arraylist<integer> a = new arraylist<integer> (); for (int i = 0; i< 1000000; i++) { a.add (R.nextint ()); } Long End2 = System.currenttimemillis (); System.out.println (end2-end); }}
4, Map,hashmap used for quick access, treemap to maintain the sorting of keys, the speed is not hashmap fast, Linkedhashmap maintain the ordering of elements, also through the hash can also be accessed quickly, in the middle.
5. Set element is not duplicated, Treeset,hashset,linkedhashset is similar to map.
Finally, you will find that these containers can hold objects, but also can be unlimited size, but also to help you count, so the magic of the class is how to design, this is the importance of learning data structure.
Java Programming Idea (ix)--holding Objects (2)