Java Learning Diary Collection Framework

Source: Internet
Author: User

Collection Frame

There are two main interfaces, one is Collection (class set) and map (map);

There are two big interfaces under collection one is List (list) and the other is set (collection)

List: ArrayList dynamic array of dynamic lists based on array implementation;

LinkedList list of doubly-linked circular linked lists

Vector------stack stack is thread dependent;

Set (SET): TreeSet an ordered set of collections implemented by a tree

HashSet Set unordered collection----->linkedhashset based on the chain storage structure;

Map (map) Hashtable hash table

HASHMAP map unordered map by Hash bucket

TREEMAP Map-ordered mapping through tree implementation

Map (map) is the alias of any object

HashMap data is characterized by a disorder: every data stored in a hash bucket has their name but the order is uncertain, that is, the order in which the map data and output is placed may be different;

The image on the right is not difficult to see, internal storage is unordered.

About the meaning of <string,string >: is generic; give the data a contract. Everyone put a string inside.

Type of data, the back of the <> can be omitted without writing, the default and the front of the same.

In the code, you see that we put the data in the Put method. Two parameters in the method: The first is the key used to give the data a name, special because the names will be overwritten by the data he represents. The second is value, which represents the object (data). Together, they're called key-value pairs.

We can get the corresponding value value by the key value, the Get (key) method;

How much data in a map is obtained using the size () method

Clears the data in the map, but the map reservation method is clear ();

A map can be merged.

According to the corresponding usage we can see;

private static void Maptest1 () {map<string,string> map=new hashmap<> (); Map.put ("Name", "emblem"); Map.put ("id" , Map.put ("Class", "the Most"), Map.put ("number", "250"); Map<string,string> map2=new hashmap<> () map2.put ("1", "123"), Map2.put ("2", "250"); SYSTEM.OUT.PRINTLN ("map" +map); System.out.println ("Map2" +map2); int size=map.size (); SYSTEM.OUT.PRINTLN ("Size:" +size); String id=map.get ("id"). toString (); SYSTEM.OUT.PRINTLN ("ID:" +id); Map.putall (MAP2); SYSTEM.OUT.PRINTLN ("Map merged map2" +map);

Delete method Remove (key), before java1.7 key does not exist will error, now will not

Replace the method put;

Factors that affect the performance of HashMap:

One is: initial memory (initial 16)

Another number: Load factor (initial 0.75) This is determined from time and space, without modification.

The mechanism of HASHMAP is the internal storage. He has a hash bucket to store, initialize 16 when you reach 12 (16*0.75), in addition will hash internal refactoring to expand. Capacity doubled.

So the wit we do when we call the constructor to initialize his initial memory number we need to/0.75+1 this reduces the memory loss;

List of lists;

The creation of lists and the addition of elements and the acquisition of basic methods of presentation;

We can see that the data is nameless and orderly;

Element Insert Add () method has two, one is sequential insert Add (value) one parameter represents data, the other specifies insert two parameter add (index,value), index is the subscript to be inserted, note that when inserting to the end of the Suitable for the same effect as add (value);

Element substitution method Set (index,value); Index to replace the subscript, value replaced by the content;

The list null Judgment Method IsEmpty () returns a Boolean value;

The method to get the length is size ();

Delete Remove () method This has two, one is the content, one is the index, according to the two deletions, but when the list is an int type, the content and index are int Java is the index first to delete.

List Conversion array:

private static void Listtest1 () {list<integer> date=new arraylist<> (); System.out.println (date);d Ate.add (1);d Ate.add (2);d Ate.add (3);d Ate.add (4); SYSTEM.OUT.PRINTLN (date); int a=date.get (2); System.out.println (a);d ate.set (2, 100); System.out.println (date);d ate.remove (2); System.out.println (date), Integer arr[]=new integer[date.size ()];d Ate.toarray (arr), and for (integer B:arr) { System.out.println (b);}}

  

Iterators (iterative interfaces)

Private Static voiditeratortest () {List<Integer> date=NewArraylist<>();        SYSTEM.OUT.PRINTLN (date); Date.add (1); Date.add (2); Date.add (3); Date.add (4); Date.add (5); Date.add (6); Date.add (7); intSize=date.size (); /*** Gets the iterator * Hasnext (); Determines whether it contains data; Returns a Boolean value, * Next () Gets the current value,*/Iterator<Integer> iter=Date.iterator ();  while(Iter.hasnext ()) {System.out.println (Iter.next ()); }            }
iterators

A list-specific iterator exists;

Private Static voidlistiteratortest () {List<Integer> date=NewArraylist<>();        SYSTEM.OUT.PRINTLN (date); Date.add (1); Date.add (2); Date.add (3); Date.add (4); Date.add (5); Date.add (4); Date.add (4); intSize=date.size (); Listiterator<Integer> iter=Date.listiterator (); System.out.println ("----Positive sequence Iteration------");  while(Iter.hasnext ()) {System.out.println (Iter.nextindex () )+":"+Iter.next ()); }        /*** Element Modifies set * and removes remove; * Note that after remove is removed, the iteration is broken and we go to the end to verify the deletion*/Date.set (3, 100); System.out.println ("----Reverse Iteration------");  while(Iter.hasprevious ()) {System.out.println (Iter.previousindex () )+":"+iter.previous ()); }        /*** Start iteration from the specified position index truncation, note that the stage can only be iterated once, and then recover * As far as I know it should be the equivalent of a pointer in the walk, you go to the end of the walk, continuous iterative two times unified direction, the second no result * and then from         The truncation begins to iterate to the head, then goes in the opposite direction, and then goes all the way again and goes to the head. */Listiterator<Integer> Iter2=date.listiterator (3); System.out.println ("----Positive sequence Iteration------");  while(Iter2.hasnext ()) {System.out.println (Iter2.nextindex () )+":"+Iter2.next ()); } System.out.println ("----Reverse Iteration------");  while(Iter2.hasprevious ()) {System.out.println (Iter2.previousindex () )+":"+iter2.previous ()); }         while(Iter.hasnext ()) {if(Iter.next () ==4) Iter.remove (); }        /*** After the deletion we can only print date, because the Listiterator was destroyed, * after printing we found that 4 of all were deleted;*/System.out.println (date); }
Listiterator

Factors affecting ArrayList Performance: initial capacity, default value of 10

Just like we did with HashMap, when we instantiate it, when we call the constructor, we start with the size we need.

His expansion multiplier is 1.5. The expansion is full.

The initial capacity of the vector is 10, and his magnification is twice times.

Java Learning Diary Collection Framework

Related Article

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.