JAVA learning notes-Basic JAVA syntax (6)

Source: Internet
Author: User

Collection container

1. First, we need to understand why there are containers in JAVA?

We know that once the length of the array is specified, the program cannot be changed during execution. It is precisely because the array has such a defect that the collection container appears, the container size can be changed at will. The container size increases accordingly based on the amount of data you actually put.

2. What is the difference between Set and List in containers?

Set is unordered and unique, and Map is implemented at the underlying layer. List is not ordered and not unique at the underlying layer. Therefore, you can use indexes to obtain values.

Note: When deleting data in the list, you must be aware of the use of the packaging type to delete the integer data type in the list, we need to know that JAVA will automatically pack and split the box, but you do not need to delete the package type in the Set, because there is no index in the Set.

3. What is the difference between List and Arrylist? What are the applicable situations?

The memory address of the ArrayList (array) is continuous, and the data in this container is often used for searching; the memory address of the linked list (list) is not sequential, and the data in this container is often used for updating

4. Iterator (container Iterator)

All classes that implement the Collection interface have an iterator method. This method will obtain an object that implements the Iterator class. Although the List has an index, however, you can also use the enhanced for loop after JAVA5.0, which is actually equivalent. net, but because it is not convenient to operate the index, it is only used for simple traversal, if the for loop iteration is enhanced by containers, the Iterator is used internally by default. But it must also be implemented using the iterator, because this is the only way to secure the county seat.

5. Collections class

This class provides a series of methods to operate the List. For example, if we want to compare the size or sort the classes we write, all the rules related to it need to be defined by ourselves, for example, if you want to compare whether objects are equal or determine the size of a rule, you must implement the Comparable interface and then override the public int compareTo (Object o) method, we recommend that you rewrite the public String toString () method for better output to the console.

The following program calls the sort method in the collections class to sort the list, and specifies the sorting method in the class you write:

1 import java. util. *; 2 3 public class test {4 public static void main (String [] args) {5 6 List <Person> list = new ArrayList <Person> (); 7 list. add (new Person ("jack", 23); 8 list. add (new Person ("tom", 19); 9 list. add (new Person ("rose", 22); 10 list. add (new Person ("mack", 21); 11 list. add (new Person ("twins", 34); 12 13 Collections. sort (list); // you can directly use the class name because the method is static. method Name directly calls 14 System. out. println (list); 15} 16} 17 18 class Person implements Comparable {19 20 public Person (String name, int age) {21 this. name = name; 22 this. age = age; 23} 24 String name; 25 int age; 26 public String toString () {27 return "name" + this. name + "age:" + this. age; 28} 29 public int compareTo (Object o) {30 Person p = (Person) o; 31 if (this. age> p. age) {32 return 1; 33} else if (this. age <p. age) {34 return-1; 35} else {36 return 0; 37} 38 39} 40 41}

6. generics after JAVA5.0

Concept: specify the types that can only be placed in the current container, and there is no reference to the parent class in the generic class pointing to the subclass object.

7. Why does Map appear in the container?

Because the Set is unordered and unique, the hashSet has no index, and it is not convenient to operate the container through the index. Therefore, the Map can create an index, in addition, this index is customized. Map stores data in the form of Key-value pairs. Map is used to add data. put (index, value); index is a custom type, which can be defined by yourself; Map is used to obtain data. in fact, get (index) Map is equivalent to List. Only Map adds the index function.

  There are two Map Iteration Methods:

1) use map first. the keySet () method obtains all the Key values in the map and encapsulates them into the Set type. Then, the iterator of the set is used for iteration. The following code obtains the key value:

import java.util.*;public class test{public static void main(String[] args){                  Map<String,String> map = new HashMap<String,String>();          map.put("1","zhong");          map.put("2","ke");          map.put("3","wen");          map.put("4","good");          map.put("5","night !!");          Set<String> set = map.keySet();          for(Iterator i = set.iterator();i.hasNext();){                    String key = (String)i.next();                    System.out.println(map.get(key));          }}       }

 

2) use the EntrySet method in Map to obtain the key-Value Pair Entry and wrap it into a Set to try to iterate using the Iterator method in Set. The Entry represents a pair of key-value. We can regard it as a Map attribute (member variable)

The following code is used:

import java.util.*;public class test{public static void main(String[] args){                 Map<String,String> map = new HashMap<String,String>();          map.put("1","zhong");          map.put("2","ke");          map.put("3","wen");          map.put("4","good");          map.put("5","night !!");           Set<Map.Entry<String,String>> entrys = map.entrySet();          for(Iterator<Map.Entry<String,String>> i = entrys.iterator();i.hasNext();){                    Map.Entry<String,String> entry = i.next();                    System.out.println(entry.getValue());          }    }       }

 

 

 

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.