Java Collection programming focus

Source: Internet
Author: User

Java Collection programming focus

I. Common Array Operations

Import java. io. *; import java. math. *; import java. util. *; public class Main {private static int [] a = new int [20]; public static void print () {for (int I = 0; I <20; I ++) System. out. print (a [I] + ""); System. out. println ("");} public static void main (String [] args) {partition cin = new partition (new BufferedInputStream (System. in); Arrays. fill (a, 1); // assign a value to array a as 1 print (); for (int I = 0; I <20; I ++) a [I] = I % 10; print (); Arrays. sort (a); // print () in ascending order; // no function in descending order. You can customize the sorting method. // However, in descending order, int index = Arrays. binarySearch (a, 4); // subscript System with the search value of 4. out. println (index );}}
Ii. set

HashSet: there are no duplicates. The types added can be different.

TreeSet: there are no duplicates. The added fields must be of the same type, because they are automatically sorted.


HashSet:

Import java. io. *; import java. math. *; import java. util. *; public class Main {public static void main (String [] args) {using cin = new using (new BufferedInputStream (System. in); HashSet s = new HashSet (); s. add (2); s. add ("liwei"); s. add (1.23); s. add (true); // add the System element. out. println (s. size (); // set length if (s. contains ("liwei") // find whether a System exists. out. println ("found"); s. remove ("liwei"); Iterator I = s. iterator (); while (I. hasNext () // iterator to traverse System. out. println (I. next ());}}
TreeSet:

Import java. io. *; import java. math. *; import java. util. *; public class Main {public static void main (String [] args) {using cin = new using (new BufferedInputStream (System. in); TreeSet s = new TreeSet (); s. add (12); s. add (5); s. add (100); s. add (1); // add an element of the same type as System. out. println (s. size (); // The length of the treeSet if (s. contains (5) // find whether a System exists. out. println ("find 5"); s. remove (5); Iterator I = s. iterator (); while (I. hasNext () // iterator to traverse System. out. println (I. next ());}}

Iii. List Common Operations
ArrayList: replaces vector in C ++ to implement dynamic arrays. Common methods include add (), get (int index), remove (int index ),

Int indexOf (ArrayList a), int lastIndexOf (ArrayList ).

Upload list: instead of the stack and queue in C ++, the common method is: The ArrayList method can be used as the upload list, and the more methods are addFirst (),

AddLast (), removeFirst (), and removeLast () to implement stackt and queue functions.


ArrayList:

Import java. io. *; import java. math. *; import java. util. *; public class Main {public static void main (String [] args) {using cin = new using (new BufferedInputStream (System. in); ArrayList list = new ArrayList (); list. add (2); list. add ("liwei"); list. add (1, 1.25); list. add (true); Iterator I = list. iterator (); while (I. hasNext () System. out. println (I. next (); ArrayList list2 = new ArrayList (); list2.add (2); list2.add (-1); list2.add (100); list2.add (500); Collections. sort (list2); // use the Collections class to sort the list. Iterator ii = list2.iterator (); while (ii. hasNext () System. out. println (ii. next ());}}


Shortlist:

Import java. io. *; import java. math. *; import java. util. *; public class Main {public static void main (String [] args) {using cin = new using (new BufferedInputStream (System. in); items list = new items list (); list. add (2); list. add ("liwei"); list. add (1, 1.25); list. add (true); list. addFirst ("abcd"); list. addLast (123); Iterator I = list. iterator (); while (I. hasNext () System. out. print (I. next () + ""); System. out. println (""); System. out. println ("first element" + list. getFirst (); // This method is used to implement the stack and queue operations System. out. println ("last element" + list. getLast (); list. removeFirst (); list. removeLast (); I = list. iterator (); while (I. hasNext () System. out. print (I. next () + "") ;}}or define the struct list type (similar to others) as follows: public class Main {public static void main (String [] args) {partition cin = new partition (new BufferedInputStream (System. in); returns list
 
  
List = new external list
  
   
(); List. add (2); list. add (5); list. add (1); list. add (-5); list. addFirst (100); list. addLast (200); Iterator I = list. iterator (); while (I. hasNext () System. out. print (I. next () + ""); System. out. println (""); System. out. println ("first element" + list. getFirst (); // This method is used to implement the stack and queue operations System. out. println ("last element" + list. getLast (); list. removeFirst (); list. removeLast (); I = list. iterator (); while (I. hasNext () System. out. print (I. next () + "");}}
  
 

4. Common Map operations

HashMap: The insertion types can be different. Common functions: Add put (key, value), remove (key), clear () elements to get the ing content get (key ),

ContainsKey (key) indicates whether the current key exists, and containsValue (value) indicates whether the current ing value exists. isEmpty (), size (), keySet () returns the set.

TreeMap: The insert type must be the same, because it will be automatically sorted, and the function is the same as above.


HashMap:

Import java. io. *; import java. math. *; import java. util. *; public class Main {public static void main (String [] args) {using cin = new using (new BufferedInputStream (System. in); HashMap m = new HashMap (); m. put ("Zhang San", 10); m. put ("Li Si", 11); m. put ("Wang Wu", 12); if (m. containsKey ("Li Si") System. out. println (""); System. out. println (m. get ("Li Si"); if (m. containsValue (m. get ("Li Si") System. out. println ("existing value" + m. get (" Li Si "); if (! M. isEmpty () System. out. println ("not empty"); Iterator it = m. keySet (). iterator (); while (it. hasNext () {String key = (String) it. next (); System. out. println (key + "" + m. get (key ));}}}
TreeMap and HashMap are similar, so the code will not be written.


5. Common Collections operations

The main function is to operate the list set. The main function of the list set operation is as follows:

Int binarySearch (List list, Object key );

Void copy (List one, List two );

Void fill (List list, Object obj );

Void sort (List list );

Void reverse (List list); // you can flip the list array.

Vois swap (List list, int I, int j );


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.