Java Base Collection framework (Collection interface and list interface)

Source: Internet
Author: User

First of all we talk about the role of a set.

The function of a set

1, in the interior of the class, the data are organized;

2, simple and fast search large number of entries;

3, some set interface, provide a series of orderly elements, and can quickly insert or delete the relevant elements in the middle of the sequence;

For example: When doing Guangbo Cao, students can be inserted into a row of a column, conversely, you can also call a row in a column of the students out.

4, some set interface, provides a mapping relationship, you can use the keyword (key) to quickly find the corresponding unique object, and this keyword can be any type.

For example: In the meal, how many aluminum lunch boxes in the difference is their own? Engrave a unique logo or note on a lunch box, which is the key word we call.

The difference between a set and an array

1, the array length and capacity is fixed, and the collection is dynamically extended

2, the array can only be accessed through the subscript element, the type is fixed, and the collection may find the specific object mapped by any type

Three, set frame

The Java Collection framework is more commonly used in 2 main interfaces: The collection interface and the map interface.

Three primary subinterfaces of the collection interface:

1, List: Orderly, repeatable, its implementation class is ArrayList, LinkedList

2, Queue: Orderly, repeatable, its implementation class is LinkedList

3, Set: unordered, non-repeatable, its implementation class is HashSet

Iv. demonstration of the case

We build a student to choose the course of the project, show the function of adding a course, so to build 3 classes, respectively, student class, Course class, Listtest class

Course Class

Package com.zm.collection;

public class Course {

public String ID;

public String name;

Public Course (string ID, string name) {

This.id=id;

This.name=name;

}

}

Student class

Package com.zm.collection;

Import Java.util.HashSet;

Import Java.util.Set;

public class Student {

public String ID;

public String name;

public Set courses;

Public Student (String id,string name) {

This.id=id;

This.name=name;

This.courses=new HashSet ();//cannot instantiate a set directly because set is an interface that is instantiated by HashSet ().

}

}

Listtest class

Package com.zm.collection;

/**

* Selected Courses

*/

Import java.util.ArrayList; Import java.util.List;

public class Listtest {

/**

* For the list of selected courses

*/

Public List Coursestoselect;

Public Listtest () {

This.coursestoselect=new ArrayList ();

}

public void Testadd () {

The first method added

Course cr1=new Course ("1", "Data Structure");

Coursestoselect.add (CR1);

Course temp= (Course) coursestoselect.get (0);

System.out.println ("added course:" +temp.id+ ":" +temp.name);

The second method added

Course cr2=new Course ("2", "C language");

Coursestoselect.add (0, CR2);

Course temp2= (Course) coursestoselect.get (0);//The object is stored in a collection that becomes the object type, and the Obeject type is removed, so the type is converted to the Course class.

System.out.println ("added course:" +temp2.id+ ":" +temp2.name);

}

public static void Main (String[]args) {

Listtest lt =new Listtest ();

Lt.testadd ();

}

}

V. Summary

As we can see from the above example, it is broadly divided into the following steps:

1. Create a class

2. Add properties for this class

3. Create a parameter constructor (construction method: To create the object and initialize the value of the object)

①, pass the property created by step 2nd to the construction method, for example:

Public Student (String id,string name) {

This.id=id;

This.name=name;

②, assigns the ID value passed to the constructor method to the new object's ID value.

4. How to create

①, create an instance object for this method

②, using the object name. Method Name () to pass the instance in

5. Create Main Master method

①, create an instance object for this method

②, call the 4th step method

Java Base Collection framework (Collection interface and list interface)

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.