Java Collection Framework

Source: Internet
Author: User

The Java Collection framework (Java Collection Framework, referred to as JCF) is made up of a set of classes and interfaces. Its primary function is to organize and access stored data in a particular structure and in a specific way. The goal is to provide a common framework for working with object collections, reducing the amount of code that programmers handle for different sets of objects.

Some of the differences in the collection class are whether duplicate elements are supported, whether the contained elements are ordered, and whether the elements in the collection class can be null elements. So the collection divides the objects into three types:

Set: Objects in an object container have no order and cannot be duplicated.

List: Objects in the object container are sorted by index order and can have duplicate objects.

Map: The elements in the object container contain a pair of "key object (key)-value object" mappings where key objects cannot be duplicated and value objects can be duplicated.

The set interface inherits the collection interface. The Set collection contains many methods. Here we use HashSet to illustrate the features of set:

 PackagePkg;ImportJava.util.*; Public classhashsetdemo{ Public Static voidMain (string[] args) {
/*new a HashSet object, generics are <string>*/Set<String> set =NewHashset<string>(); String str1= "123"; String str2= "234"; String STR3= "345"; String STR4= "456"; Set.add (STR1); Set.add (STR2); Set.add (STR3); Set.add (STR4);
Traversal via foreach for(String string:set) {System.out.println (string); } }}

The result of the traversal in the code is random, and the elements traversed in the code are not sequential. This embodies the features in set.

The list interface is also inherited from the collection interface, because the elements in the list are ordered, so we can refer to the elements directly by their location (that is, the index). The ArrayList and arrays in the list interface are similar, representing a set of elements for an input index, except that ArrayList has no fixed size and is variable in length. Let's take a look at the operation of ArrayList:

ImportJava.util.*; Public classArraylistdemo { Public Static voidMain (string[] args) {//new a ArrayList objectList List =NewArrayList (); //adding elementsList.add (1); List.add ("ABC"); List.add (10); List.add (NewInteger (4)); //Delete an element based on an indexList.remove (0); //Reset ElementList.set (0, 22);  for(intI=0;i<list.size (); i++) {//size is the length of the ArrayListSYSTEM.OUT.PRINTLN (i + "=" + List.get (i));//get is a display of elements in ArrayList        }    }}

Then there is the map interface, in the map key (key) and the value is one by one corresponding, through the specified key, you can find the corresponding unique value. HashMap is implemented based on the hash algorithm's map interface, which is maintained in a hash table and the key is unique. However, HashMap does not guarantee that keys are arranged in a particular order. Let's look at the basic usage of HashMap:

ImportJava.util.*; Public classHashmapdemo { Public Static voidMain (string[] args) {//new A HashMap objectmap<string,string> map =NewHashmap<string,string>(); //in the Map interface, you add a key-value pair using the Put method.Map.put ("A1", "ABC"); Map.put ("B1", "12"); Map.put ("S0001", "Zhang San" + "male"); //DeleteMap.Remove ("A1"); //using iterator traversalset<string> keys =Map.keyset ();  for(Iterator<string> i =keys.iterator (); I.hasnext ();) {String key=I.next (); String value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "=" +value); }    }}

These are the basic concepts and operations of Set, list, and map in the collection. And finally, generics, generics are represented in code <E> (where E is just a placeholder), generics are allowed to specify a type-formal parameter when defining a class or interface, which is determined when declaring a variable, creating an object (that is, the actual argument passed in). By using generics to type parameters, we can force the program to check the data types obtained from the collection at compile time, for type safety purposes, and to eliminate the movement of the data from the collection in the transformation.

Java Collection Framework

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.