Java from getting started to giving up: set,
Let's talk about the use of collections and common collection types in Java today.
What is a set?
Just recently, during military training in the school, I only heard the instructor say, "Gathering !!!" All of you, Tom and the new guys are running neatly. This is a collection ···
The set in Java also means the same. Java shouted: "set !!!", Then we put the data that needs to be put together into a collection. Some people will say, "Isn't this function available for arrays ". Yes, array has this function, but the set has more functions than the array, and different sets have different focuses. What are the advantages of this function.
The biggest difference between a set and an array: The length of the set is not fixed. You can add or delete any element. The length of the array is fixed. If the length of the array exceeds the limit, the data is deleted but the position is still there.
Java provides a series of interfaces and classes in the java. util package:
1. General methods in the Collection interface are as follows:
The Collection interface is divided into the List interface and the Set interface. Their differences are as follows:
List interface: stores duplicate elements in order.
Set interface: The storage is out of order and duplicate elements cannot be saved.
List usage:
The most common subclass of list is ArrayList.
12345678910111213 |
public static void main(String[] args) { List list = new ArrayList(); list.add( "111" ); list.add( "aaa" ); list.add( "222" ); list.add( "bbb" ); list.add( "555" ); list.add( "abc" ); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } } |
Set usage:
HashSet, the most common subclass of Set.
12345678910111213 |
public static void main(String[] args) { Set list = new HashSet(); list.add( "111" ); list.add( "aaa" ); list.add( "222" ); list.add( "bbb" ); list.add( "555" ); list.add( "abc" ); for (Object object : list) { System.out.println(object); } } |
The List is ordered, so there is a corresponding get method to read data based on the subscript, and Set is unordered, so you can only traverse to get data. The result is the List traversal result on the left and the Set traversal result on the right.
As for deleting data and adding the same data, you can test the data yourself.
II. General methods in the Map interface are as follows:
The most common subclasses of Map are HashMap and Hashtable.
The Map set usesKey-value pairs to save dataObviously, it has no order. This is similar to the dictionary we use in our life (query corresponding words based on pinyin or radicals ).
The difference between HashMap and Hashtable: HashMap allows null keys or null values. Hashtable is the opposite.
1234567891011 |
public static void main(String[] args) { Map map = new HashMap(); map.put( "wang" , "Wang jiuegao" ); map.put( "yang" , "Yang yangyang" ); map.put( "li" , null ); map.put( null , "Qian Lixian" ); for (Object key : map.keySet()) { System.out.println(key+ ":" +map.get(key)); } } |
Running result:
Hashtable effect:
During implementation and development, we useGeneric setCompared with a common set, it has the advantage of specifying the data type before saving the data, otherwise you will not be allowed to put it in !!!
Okay, today's collection is getting started here. We can use the set in this article to implement the task that was previously arranged (no longer using arrays ). How to use the specific set, and then carefully analyze it later in the comprehensive exercise.
"Software thinking" blog address:51CTO,BlogInterested friends can visit other related blog posts.