Chapter 1 collection class and Chapter 2 Collection

Source: Internet
Author: User

Chapter 1 collection class and Chapter 2 Collection

 

Java provides a data structure that can store a set of data. It provides a variety of methods, which are generally more widely used than arrays in actual development. This data structure becomes a Collection: Collection.

Collection is an interface that defines functions and methods of a Collection.

 

The List interface is a sub-interface of Collection and is used to define the data structure of linear tables;

The List can be understood as an array for storing objects, but the number of elements can be dynamically increased or decreased. And List is a repeatable set.

The List interface has two common implementation classes:ArrayList and rule list, RespectivelyDynamic Array and linked listTo implement the List interface.

We can think that the methods of ArrayList and ArrayList are exactly the same in logic, but there are some differences in performance,ArrayList is more suitable for random access while rule list is more suitable for insertion and deletion.You can ignore this difference when the performance requirements are not extremely demanding.

 

4.1 List interface -- ordered collection class 279


4.1.1 add method -- insert element 279 to the list

 

 List<String> list = new ArrayList<String>();        list.add("java");                list.add("cpp");                list.add("php");        list.add("c#");                list.add("objective-c");


4.1.2 addAll Method -- add the specified collection to the list 280

 

List <String> list = new ArrayList <String> (); list. add ("java"); list. add ("cpp"); list. add ("php"); list. add ("c #"); list. add ("objective-c"); // The addAll method is used to add all elements in a given set to the List in the current set <String> list2 = new ArrayList <String> (); list2.addAll (list); System. out. println (list2 );

 

4.1.3 clear method -- remove all elements from the list 282
4.1.4 contains Method -- determines whether the list contains the specified Element 283
4.1.5 containsAll Method -- determine whether the list contains all elements of the specified collection 283
4.1.6 equals Method -- compare whether the specified object and list are equal 285

 


4.1.7 get Method -- get the element 285 at the specified position in the list
4.1.8 set Method -- replace element 286 at the specified position in the list

 

List <String> list = new ArrayList <String> (); list. add ("java"); list. add ("cpp"); list. add ("php"); list. add ("c #"); list. add ("objective-c"); // get method traversal List for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I);} String value = list. set (1, "c ++"); System. out. println (value); // cpp System. out. println (list); // [java, c ++, php, c #, objective-c] // exchange the list of elements on positions 1 and 3. set (1, list. set (3, list. get (1); System. out. println (list); // [java, c #, php, c ++, objective-c]

 

4.1.9 hashCode Method -- return the hash code value of the List 287
4.1.10 indexOf Method -- returns the first occurrence of the specified Element location 287
4.1.11 lastIndexOf Method -- returns the position 288 of the last occurrence of the specified element.
4.1.12 isEmpty Method -- judge whether the set is empty 289

 


4.1.13 iterator Method -- Return iterator 290
4.1.14 listIterator Method -- return list iterator 290

Collection provides a generic way to traverse a set, Iterator ). The method for obtaining the iterator is defined using Collection:

The Iterator is an interface that provides the implementation of the iterator when overwriting the Iterator () method of the Collection.

Iterator provides a unified way to traverse set elements.

The iterator is used to traverse the set in two main ways:

  • Boolean hasNext (): determines whether the set has any elements that can be traversed.
  • E next (): returns the next element of the iteration.

The traversal of a set should follow the "First question, then fetch" method. That is to say, the elements should be retrieved through the next () method when the returned value of the hasNext () method is determined to be true.

It can be seen that using the iterator to traverse a set is driven by a boolean value, so it is more suitable to use a while loop to traverse.

 

 Collection<String> c = new HashSet<String>();        c.add("java");                c.add("cpp");                c.add("php");        c.add("c#");                c.add("objective-c");        Iterator<String> it = c.iterator();        while (it.hasNext()) {            String str = it.next();            System.out.println(str);        } 

 

 


4.1.15 remove Method -- remove the specified Element 292 from the list
4.1.16 removeAll Method -- remove all elements contained in the specified collection from the list 293

 

 List<String> list = new ArrayList<String>();        list.add("java");        list.add("c#");        System.out.println(list); // [java, c#]                         list.add(1, "cpp");        System.out.println(list); // [java, cpp, c#]                         list.remove(2);        System.out.println(list); // [java, cpp]               list.removeAll(list);        System.out.println(list);


4.1.17 retainAll Method -- retain all elements contained in the specified collection 294
4.1.18 size method -- returns 294 elements in the list


4.1.19 subList Method -- Obtain the subList 295 in the specified range in the list

 

The subList method of List is used to obtain the subList. Note that the List obtained by subList occupies the same storage space as the original List, and the operations on the subList will affect the original List.

List <Integer> list = new ArrayList <Integer> (); for (int I = 0; I <10; I ++) {list. add (I);} System. out. println (list); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] List <Integer> subList = list. subList (3, 8); System. out. println (subList); // [3, 4, 5, 6, 7] // The List obtained by subList occupies the same data space as the source List (int I = 0; I <subList. size (); I ++) {subList. set (I, subList. get (I) * 10);} System. out. println (subList); // [30, 40, 50, 60, 70] System. out. println (list); // [0, 1, 2, 30, 40, 50, 60, 70, 8, 9] // you can delete a list of continuous elements. subList (3, 8 ). clear (); System. out. println (list );


4.1.20 toArray Method -- returns the array 296 of all elements

The toArray method of List is used to convert a set into an array. But in fact this method is defined in the Collection, so all sets have this function.

We can input an array of the specified type. The element type of the array should be consistent with the element type of the set. The returned value is the converted array, which stores all elements in the set.

List <String> list = new ArrayList <String> (); list. add ("a"); list. add ("B"); list. add ("c"); // The String [] strArr = list is not required for the input array. toArray (new String [] {}); System. out. println (Arrays. toString (strArr); // [a, B, c]

 

Convert array to list

// Method 1 String [] strArr = {"a", "B", "c"}; List <String> list = Arrays. asList (strArr); System. out. println (list); // [a, B, c] // list. add ("d"); // will throw UnsupportedOperationException // java. util. arrays $ ArrayList System. out. println (list. getClass (). getName (); // method 2 List <String> list1 = new ArrayList <String> (); list1.addAll (Arrays. asList (strArr); list1.add ("d"); System. out. println (list1 );

 

 


4.2 Map interface -- ing set class 297


4.2.1 clear method -- remove all mappings 297
4.2.2 containsKey Method -- determines whether to include the specified key name 298
4.2.3 containsValue Method -- determines whether to include the specified key value 298
4.2.4 equals Method -- determine whether it is the same as the specified object 299
4.2.5 get Method -- returns the value 300 mapped to the specified key
4.2.6 isEmpty Method -- judge whether it is empty 300
4.2.7 keySet Method -- get all keys of the Map set 301
4.2.8 put Method -- add object 301 to the specified index location
4.2.9 putAll Method -- append another Map object to the current Map set 302
4.2.10 remove Method -- remove the content of the specified key name in the Map set 303
4.2.11 size method -- get the size of the Map Collection class 304
4.2.12 values Method -- get all key value objects in the Map set 304


4.3 Set interface -- non-repeating element Set class 305


4.3.1 add method -- add object 305 to Set
4.3.2 addAll Method -- add all content of another Set to Set 306
4.3.3 clear method -- remove all content from the Set 307
4.3.4 contains Method -- determines whether the Set contains the specified object 307
4.3.5 containsAll Method -- judge whether the Set contains all objects in another Set 308
4.3.6 equals Method -- compare whether the specified object is equal to this Set by 309
4.3.7 isEmpty Method -- judge whether the Set is empty 309
4.3.8 iterator Method -- get the iterator 310 of the Set
4.3.9 remove Method -- remove the specified object 311 from the Set
4.3.10 removeAll Method -- remove all content contained in another set 311
4.3.11 retainAll Method -- keep all content contained in another set 312
4.3.12 size method -- get the size of the Set class 313
4.3.13 toArray Method -- use all objects in the Set to create an array 313

 

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.