Java Collection class

Source: Internet
Author: User
Tags addall set set

A Java Collection class is like a container used to hold Java class objects. Some items in the container can be operated, while others cannot.
Java Collection classes are provided by java. util packages. Commonly Used Collection interfaces are Map, Set, and List, while Set and List interfaces are implemented.
1. Understand the Collection Interface
The Collection interface is the parent interface of the Set interface and the List interface. It is usually not directly used, but some general methods are defined. These methods can be used to perform basic operations on the Set.
Common methods for the Collection interface are as follows:
1) add (): add an object to the set
2) remove (): remove an object from the collection
3) isEmpty (): determines whether the current set is empty.
4) iterator (): Return iterator, used to traverse objects in the Set
5) size (): gets the number of elements in the set.
6) clear (): clears the set.
7) contains (): determines whether a specified object exists in the collection.
8) addAll (): Adds all objects in the specified set to the set.
The common method of the Collection interface is as follows:
[Java]
/**
* Examples of common Colletion Interfaces
*/
 
Import java. util .*;
 
Public class CollectionDemo {
/**
* Method Description: outputs the elements of the Collection class.
* Input parameter: Collection <String> list
* Return type: void
*/
Public static void printCollectionElem (Collection <String> list ){
If (! List. isEmpty () {// use of the isEmpty () method
System. out. println ("the set is not empty, element in the Set :");
Iterator <String> it = list. iterator (); // use of the iterator () method
 
While (it. hasNext ()){
System. out. println (it. next ());
}
}
}
 
Public static void main (String [] args ){
String str1 = "Shenzhen University ";
String str2 = "Sun Yat-sen University ";
String str3 = "Jinan University ";
 
Collection <String> list = new ArrayList <String> ();
List. add (str1); // use of the add () method
List. add (str2 );
List. add (str3 );

PrintCollectionElem (list );
 
Collection <String> list1 = new ArrayList <String> ();
List1.addAll (list); // use the addAll () method
PrintCollectionElem (list1 );
 
List1.remove (str1); // use of the remove () method
PrintCollectionElem (list1 );
 
System. out. println ("number of elements in list:" + list. size (); // use of the size () method
 
Collection <String> list2 = new ArrayList <String> ();
List2.add (str1 );
List2.add (str3 );
List. removeAll (list2); // use the removeAll Method
PrintCollectionElem (list2 );
PrintCollectionElem (list );
 
List. clear (); // It is also equivalent to list. removeAll (list );
PrintCollectionElem (list );
}
}
The running result is as follows:

2. Understanding the Map set
The Map set includes the Map interface and the class that implements the Map interface.
The map interface does not inherit the Collection interface and provides key-to-value ing. The common method of map interface is as follows:
1) clear (): clear
2) isEmpty (): determines whether the set element is null.
3) size (): gets the number of elements in the set.
4) put (key k, value v): Add key-value ing to the set
5) get (Object key): returns the value corresponding to the specified key Object.
6) keySet (): return the Set formed by all key objects in the Set.
7) values (): return the Collecion set formed by all value objects in the set.
The implementation classes of the Map interface include the HashMap class and the TreeMap class. If you often need to add, delete, and locate ing relationships, we recommend that you use the HashMap class to implement Map sets. However, when traversing a set, the obtained ing relationships may be unordered, you can use the TreeMap class. To traverse a Map set, you must first obtain the Key set and Value set, and then traverse them separately.
The sample code for the Map set is as follows:
[Java]
/**
* Map set instances
*/
Import java. util .*;
 
Class User {
Public User (String id, String name ){
This. id = id;
This. name = name;

}
 
Public void setName (String name ){
This. name = name;
}
Public String getName (){
Return this. name;
}
Public void setId (String id ){
This. id = id;
}
Public String getId (){
Return this. id;
}
 
Private String id;
Private String name;

}
 
Public class MapDemo {
Public static void main (String [] args ){
Map map = new HashMap ();
User user1 = new User ("001", "tom ");
User user2 = new User ("002", "jack ");
User user3 = new User ("003", "steven ");

Map. put (user1.getId (), user1.getName ());
Map. put (user2.getId (), user2.getName ());
Map. put (user3.getId (), user3.getName ());

Set set = map. keySet ();
If (! Set. isEmpty ()){
Iterator it = set. iterator ();
System. out. println ("Map set implemented by the HashMap class, the content is as follows :");
While (it. hasNext ()){
String str = (String) it. next ();
String name = (String) map. get (str );
System. out. println (str + "" + name );
}
}

TreeMap treeMap = new TreeMap ();
TreeMap. putAll (map );
Iterator it1 = treeMap. keySet (). iterator ();
System. out. println ("Map set implemented by the TreehMap class, the content is as follows :");
While (it1.hasNext ()){
String str = (String) it1.next ();
String name = (String) map. get (str );
System. out. println (str + "" + name );
}
}
}
The running structure is as follows:

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.