The data structures in the upgraded version of the collections class--java in Java Java

Source: Internet
Author: User
Tags array sort map data structure

In general, the data structures on textbooks include arrays, single-linked lists, stacks, trees, and graphs. The data structure I'm referring to here is a question of how to represent an object, and sometimes a single variable declaration can be overwhelming, such as a int,string,double or even a one-dimensional array, a two-dimensional array that cannot fully express what you are expressing, and defining a class is too troublesome You can consider using the collections class in Java. With the collections class, import java.util.* must be declared in the file header;


A dynamic, orderly, variable-sized one-dimensional array vector and ArrayList

The collections class includes a dynamic, ordered, variable-sized, one-dimensional array of vectors and ArrayList.

The only difference between vectors and ArrayList is that the vector is self-exclusive, that multiple threads throw exceptions to their read and write, while ArrayList allows multiple threads to read and write, the other parts are identical, in other words, if a single thread is read-write, There is no difference between using vectors and ArrayList, but now programming is mostly ArrayList, using vectors is a bit non-mainstream,

1, the use of vector is as follows:

public static void Vectortest () {//vector<double> means the Vector can only hold double//vector<string> Indicates that the vector can only exist string//although vector<object> vector=new vector<object> (), which is equivalent to vector vector=new//vector (); This write in eclipse warns you that this vector is not canonical, ╮(╯▽╰)╭ vector<object> vector = new vector<object> (); Vector.add (1.6); Vector.add (2.06); Vector.add (1); System.out.println ("Simple add means adding elements from the end:" + vector); SYSTEM.OUT.PRINTLN ("size") can find the number of elements contained in a vector: "+ vector.size ()); Vector.remove (1); System.out.println ("Remove (1)" means to delete the 1th element, since the count starts from 0, which is 2.06 of this element: "+ vector"; Vector.remove (Vector.lastelement ()); System.out.println ("The vector of the last element is deleted:" + vector); Vector.add (0, 1.8888); System.out.println ("Add 1.8888 to this element in the No. 0 position:" + vector); Vector.set (0, "a"); SYSTEM.OUT.PRINTLN ("Change the No. 0 position of this element to a:" + vector);}
This method if called in the main function:

System.out.println ("======vector data structure test begins ======"); Vectortest (); SYSTEM.OUT.PRINTLN ("Test end of ======VECTOR data Structure ======");

The results of the operation are as follows:

Test of ======VECTOR data structure begins ======
Simple add means adding elements from the end: [1.6, 2.06, 1]
Size () to find out the number of elements contained in the vector: 3
Remove (1) means to delete the 1th element, since the count starts at 0, which is the element 2.06: [1.6, 1]
The vector of the last element is deleted: [1.6]
Add 1.8888 to this element in the No. 0 position: [1.8888, 1.6]
Change the No. 0 position of this element to A:[a, 1.6]
Test end of ======VECTOR data structure ======

2, ArrayList

public static void Arraylisttest () {arraylist<double> ArrayList = new arraylist<double> (); Arraylist.add ( 1.0); Arraylist.add (4.0); Arraylist.add (5.0); Arraylist.add (2.3); System.out.println ("Simple add means adding elements from the end:" + ArrayList); SYSTEM.OUT.PRINTLN ("size () can find out the number of elements contained in:" + arraylist.size ()); Arraylist.remove (1); System.out.println ("Remove (1)" means to delete the 1th element, since the count starts from 0, which is 4 of this element: "+ ArrayList); Arraylist.remove (Arraylist.size ()-1); System.out.println ("ArrayList of the last element to be deleted:" + ArrayList); Arraylist.add (0, 1.8888); System.out.println ("Add 1.8888 this element in the No. 0 position:" + ArrayList); Arraylist.set (0, 9.0); SYSTEM.OUT.PRINTLN ("Change the No. 0 position of this element to a:" + ArrayList); Collections.sort (ArrayList); System.out.println ("Sort is supported if ArrayList is not an abstract type" + ArrayList);}

Here you can see that the way ArrayList removes the last element is different from the vector, mainly ArrayList no lastelement () This method to remove the last element remove (), only with arraylist.size ()- One to determine the position of the last element. If this method is called in the main function:

System.out.println ("======arraylist data structure test begins ======"); Arraylisttest (); SYSTEM.OUT.PRINTLN ("Test end of ======ARRAYLIST data Structure ======");

You get the following result:

Test of ======ARRAYLIST data structure begins ======
Simple add means adding elements from the end: [1.0, 4.0, 5.0, 2.3]
Size () to find out the number of elements contained: 4
Remove (1) means to delete the 1th element, since the count starts at 0, which is the element 4: [1.0, 5.0, 2.3]
The arraylist of the last element is deleted: [1.0, 5.0]
Add 1.8888 to this element in the No. 0 position: [1.8888, 1.0, 5.0]
Change the No. 0 position of this element to a:[9.0, 1.0, 5.0]
If ArrayList is not abstract type, the sort is supported [1.0, 5.0, 9.0]
Test end of ======ARRAYLIST data structure ======

From the above two examples, you can see that the vector and ArrayList than a normal array, that is, the textbook teaching of a one-dimensional array of int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 7}; Powerful, can insert elements anywhere, or do not need to go through The enumeration group is able to delete the elements of the specified position in a single method, but you still need to know how the array is traversed for your exams. In fact, ArrayList and ordinary one-dimensional arrays can be fully interoperable, and the use of ArrayList can also be used to sort the array directly, instead of writing to the array a bubble sort, such as the direct use of Collections.sort (), you can sort the array, Then use Collections.reverse (), you can achieve the inverse sort, of course, the sentence, for your exam you still need to know how this array is sorted.

such as the following method, implemented on a one-dimensional array of int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 7}; Sort and reverse order, first convert the array into ArrayList Collections.sort (), and collections. Reverse (); Sort, and then convert the ArrayList content back to a one-dimensional array:

public static void Arraylistsort () {int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 7}; arraylist<integer> ArrayList = new arraylist<integer> (); for (int i = 0; i < array.length; i++) System.out.pr Int (Array[i] + ","); for (int i = 0; i < array.length; i++) Arraylist.add (Array[i]); Collections.sort (ArrayList); for (int i = 0; i < array.length; i++) Array[i] = Arraylist.get (i); System.out.print ("sorted array:"); for (int i = 0; i < array.length; i++) System.out.print (Array[i] + ","); Collections.reverse (ArrayList); for (int i = 0; i < array.length; i++) Array[i] = Arraylist.get (i); System.out.print ("Inverse sorted array:"); for (int i = 0; i < array.length; i++) System.out.print (Array[i] + ",");// After sorting the ArrayList destroyed ArrayList = null;//This is suggested that Java immediately recycle garbage, of course, this sentence is not OK, Java in the process of running will automatically clear the garbage System.GC ();}
This method is called in the main function:

System.out.println ("======java array sort begins ======"); Arraylistsort (); System.out.println ("======java array sort ends ======");
You will be able to get the following running results:

======java Array Sort start ======
8,7,100,88,6,4,5,33,7, sorted array: 4,5,6,7,7,8,33,88,100, inverse sorted array: 100,88,33,8,7,7,6,5,4,

======java Array Sort End ======

In addition, the previous "Java" list Use "(Click to open the link) is the same reason


Second, set HashSet

In addition, the set Hashset,hashset is identical to the mathematical set concept. Consisting of one or more elements is called a collection. HashSet has:

1. Certainty, the elements in the collection must be deterministic, this is nonsense, it must be determined, can I put an indeterminate thing inside?

2. Cross-specific, the elements in the collection are different. For example: Set A={1,a}, then a cannot equal 1, that is, if you put two 1 into hashset will automatically become a 1

3. Disorder, the elements in the collection have no successive points. So HashSet is not allowed to sort operations

For example, the following method:

public static void Hashsettest () {hashset<object> HashSet = new hashset<object> (); Hashset.add (1); Hashset.add (1); Hashset.add (5); Hashset.add (2.3); System.out.println ("Simple add means adding elements from the end:" + hashset); SYSTEM.OUT.PRINTLN ("size () can find out the number of elements contained in:" + hashset.size ()); Hashset.remove (1); System.out.println ("remove (1) means delete ' 1 ' this element:" + hashset); Hashset.remove ("ASD"); SYSTEM.OUT.PRINTLN ("If there is no ' ASD ' element then remove does nothing:" + hashset); Hashset.add (1.8888); System.out.println ("Add 1.8888 this element:" + hashset);}
In the main function, call this method:

System.out.println ("======hashset data structure test begins ======"); Hashsettest (); SYSTEM.OUT.PRINTLN ("Test end of ======HASHSET data Structure ======");

The results are as follows:

Test of ======HASHSET data structure begins ======
Simple add means adding elements from the end: [1, 5, 2.3]
Size () to find out the number of elements contained: 3
Remove (1) means delete the element ' 1 ': [5, 2.3]
If there is no element of ' ASD ' then remove does nothing: [5, 2.3]
Add 1.8888 to this element: [5, 1.8888, 2.3]
Test end of ======HASHSET data structure ======

HashSet has the Add () method and the Remove () method, the Add () element does not have the order, each time the result of printing with System.out.println () may not be the same order, nor to the above vector and ArrayList, The Collections.sort (ArrayList) can be used as long as the stored element is not an object;

Three or two-tuple hashmap

The use of this method and the above data is basically the same, it is very simple, is put method to the object into the map, and get can take the object in the map, but it is wrong to try to use the two-tuple hashmap, if an object contains too many elements, then you should consider using classes. Instead of being obsessed with Java mediations, the collections class between common variables and class classes.

For example, the following method shows an attempt to change the HashMap to three-tuple error operation:

public static void Maptest () {System.out.println ("======map error is used to start ======"); Hashmap<string,string> map=new hashmap<string, string> (); Hashmap<string,hashmap<string, string>> bigmap=new hashmap<string, HashMap<String, String>> (); Map.put ("Key1", "1"), Map.put ("Key2", "2"), Bigmap.put ("Test1", map), Map.clear (); Map.put ("Key1", "3"); Map.put (" Key2 "," 4 "); Bigmap.put (" Test2 ", map); System.out.println (BIGMAP); System.out.println (Bigmap.get ("Test1"). Get ("Key1")); System.out.println (Bigmap.get ("Test1"). Get ("Key2")); System.out.println (Bigmap.get ("Test2"). Get ("Key1")); System.out.println (Bigmap.get ("Test2"). Get ("Key2")); System.out.println ("End of use of ======map error ======"); System.out.println ("======map correct use begins ======"); Map.clear (); Bigmap=null;map.put ("Key1", "1"); Map.put ("Key2", "2"); Map.put ("Key3", "3"); SYSTEM.OUT.PRINTLN (map); System.out.println ("======map Correct use End ======");}
Call this code in the main function to get the following result:

Test of ======MAP data structure begins ======
======map the use of the error begins ======
{test1={key2=4, key1=3}, Test2={key2=4, key1=3}}
3
4
3
4
======map End of Use error ======
======map the correct use begins ======
{key3=3, key2=2, key1=1}
======map correct end of use ======
Test end of ======MAP data structure ======

This procedure was intended to be obvious, trying to construct a {test1,key1,1},{test1,key2,2},{test2,key3,3},{test2,key4,4}

However, every bigmap still has that map, and once you clear the map, the map in Test1 is also emptied.

Someone is trying to create multiple MAP1,MAP2,... Then you might as well use a simple class that looks more explicit, and the class can later write methods that are inherited


Iv. remarks

In creating a new vector,arraylist good, hashset or HashMap, can be written:

collection<string> a= new arraylist<string> (); list<string> a= new vector<string> ();
such as

Because there are ArrayList, vectors, LinkedList, HashSet, and TreeSet inherited from the collection interface, there are HashMap, Hashtable, which inherit from the map interface, which is the inheritance, polymorphism, Encapsulation and so on, of course, for your companion to see more clearly, here do not play what fancy named, write a clear arraylist<integer> arraylist = new arraylist<integer> (); No one dares to say that you don't understand classes in Java.


The data structures in the upgraded version of the collections class--java in Java Java

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.