Java-collection framework List and java framework list

Source: Internet
Author: User
Tags addall

Java-collection framework List and java framework list

  • Concept of a set

In real life: a lot of things come together

Set in mathematics: Sum of things with common attributes

Java Collection class: A Tool class, such as a container, stores any number of objects with common attributes.

  • Functions of a set
  • Compared with Arrays
  • Java Collection framework architecture

The Collection and Map interfaces are two root interfaces.

Collection has three sub-interfaces: List (sequence), Queue (Queue), and Set (Set). The elements stored in List and Queue are ordered and repeatable, the elements stored in the Set are unordered and repeatable. List and Set are commonly used in these three subinterfaces. List has a very common and important implementation class ArrayList (array sequence). Queue has a very important implementation class called List (linked List), which is also the implementation class of the List interface, set also has an important implementation class HashSet ).

The Map interface also has many subinterfaces, which are commonly used as its implementation classes. It has a very important implementation class HashMap (hash table ).

Objects stored in collections are independent objects. In Map, two objects, one Key and one Value, are used as a ing to store data. Such a ing is an instance of the Entry class. The Entry class (Key-Value Pair) is an internal class of Map. Keys and values can be any type of objects.

  

  • Collection interface, subinterface, and implementation class

The Collection interface is the parent interface of the List, Queue, and Set interfaces. It defines the operations that can be used to add, delete, modify, and query the List, Set, and Queue operations.

List is a set of ordered and repeated elements. It is called a sequence. It can precisely control the insert position of each element or delete a certain position element. ArrayList is an array sequence, the underlying layer is implemented by arrays.

Take the course selection as an example to introduce the use of List:

1.1 add

Course. java

Package com. test. collection;/*** Course class * @ author Administrator **/public class Course {public String id; public String name; public Course (String id, String name) {this. id = id; this. name = name ;}}

Student. java

Package com. test. collection; import java. util. hashSet; import java. util. set;/*** Student class ** @ author Administrator **/public class Student {public String id; public String name; public Set courses; // The public Student (String id, string name) {this. id = id; this. name = name; this. courses = new HashSet (); // instantiate sourses (Set is an interface and cannot be directly instantiated )}}

ListTest. java

Package com. test. collection; import java. util. arrayList; import java. util. arrays; import java. util. iterator; import java. util. list;/*** Optional Course classes ** @ author Administrator **/public class ListTest {/*** used to store the List of optional courses */public List coursesToSelect; public ListTest () {this. coursesToSelect = new ArrayList ();}/*** used to add alternative courses to coursesToSelect */public void testAdd () {Course c1 = new Course ("1 ", "Data Structure"); // create an instance of the course object coursesToSelect. add (c1); // call the add method to add a Course to the List of optional courses Course temp = (Course) coursesToSelect. get (0); // The objects stored in the collection are all converted to objects, ignoring the previous type of objects. During retrieval, the types must be converted to System. out. println ("added course:" + temp. id + ":" + temp. name); Course c2 = new Course ("2", "C Language"); coursesToSelect. add (0, c2); // another add () method that is overloaded in List. You can specify the location where the element is added: Course temp2 = (Course) coursesToSelect. get (0); System. out. println ("added course:" + temp2.id + ":" + temp2.name); coursesToSelect. add (c1); // the elements in the List can be repeated Course temp0 = (Course) coursesToSelect. get (2); System. out. println ("added Course:" + temp0.id + ":" + temp0.name); // The following method throws an array subscript out-of-bounds exception // Course c3 = new Course ("3 ", "Java"); // coursesToSelect. add (4, c3); Course [] course = {new Course ("3", "Discrete Mathematics"), new Course ("4", "assembly language ")}; coursesToSelect. addAll (Arrays. asList (course); Course temp3 = (Course) coursesToSelect. get (3); Course temp4 = (Course) coursesToSelect. get (4); System. out. println ("added two courses:" + temp3.id + ":" + temp3.name + temp4.id + ":" + temp4.name); Course [] course2 = {new Course ("5 ", "Advanced Mathematics"), new Course ("6", "Computer Network")}; coursesToSelect. addAll (2, Arrays. asList (course2); Course temp5 = (Course) coursesToSelect. get (2); Course temp6 = (Course) coursesToSelect. get (3); System. out. println ("added two courses:" + temp5.id + ":" + temp5.name + temp6.id + ":" + temp6.name);} public static void main (String [] args) {ListTest lt = new ListTest (); lt. testAdd ();}}

Execution result:

Added Course: 1: Data Structure
Added Course: 2: C Language
Added Course: 1: Data Structure
Two courses added: 3: discrete mathematics 4: Assembly Language
Added two courses: 5: Advanced Mathematics 6: Computer Network

1.2 Query

Package com. test. collection; import java. util. arrayList; import java. util. arrays; import java. util. iterator; import java. util. list;/*** Optional Course classes ** @ author Administrator **/public class ListTest {/*** used to store the List of optional courses */public List coursesToSelect; public ListTest () {this. coursesToSelect = new ArrayList ();}/*** Method for retrieving elements in the List */public void testGet () {int size = coursesToSelect. size (); System. out. println ("select the following courses:"); for (int I = 0; I <size; I ++) {Course c = (Course) coursesToSelect. get (I); System. out. println ("Course:" + c. id + ":" + c. name) ;}}/*** access the collection element through the Iterator */public void testIterator () {Iterator it = coursesToSelect. iterator (); System. out. println ("have the following course options (accessed via iterator):"); while (it. hasNext () {Course c = (Course) it. next (); System. out. println ("Course:" + c. id + ":" + c. name) ;}}/*** use the for each method to access the set element */public void testForEach () {System. out. println ("you have the following Course options (accessed through for each):"); for (Object obj: coursesToSelect) {Course c = (Course) obj; System. out. println ("Course:" + c. id + ":" + c. name) ;}} public static void main (String [] args) {ListTest lt = new ListTest (); lt. testGet (); lt. testIterator (); lt. testForEach ();}}

Execution result:

You can select the following courses:
Course: 2: C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Computer Network
Course: 1: Data Structure
Course: 3: discrete mathematics
Course: 4: Assembly Language

1.3 modify

Public class ListTest {public void testModify () {Course c = new Course ("7", "Mao"); coursesToSelect.Set(4, c );}}

After modification, you can select the following courses:
Course: 2: C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 7: Mao Summary
Course: 3: discrete mathematics
Course: 4: Assembly Language

1.4 Delete

Public class ListTest {public void testDelete () {/* coursesToSelect.Remove(4); * // * Course c = (Course) coursesToSelect. get (4); coursesToSelect.Remove(C); */Course [] courses = {(Course) coursesToSelect. get (4), (Course) coursesToSelect. get (5)}; coursesToSelect.RemoveAll(Arrays. asList (courses); System. out. println ("course deleted ");}}

After deletion, you can select the following courses:
Course: 2: C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 4: Assembly Language

 

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.