Collection Framework in Java

Keywords: Java

  • Concept

Collection classes in Java: A tool class, like a container, that stores any number of objects with common properties

  • The Role of Sets

 

  • Types of collection frameworks:

collection and map are interfaces and cannot be instantiated

List s and Queue s are ordered, repeatable, and Set s are disordered and non-repeatable

 

  • list Adding Elements: Two Adding Methods

1. Add directly and add elements at the end of the team.

Objects stored in collections become object types, and type conversion is required when they are taken out.

2. Adding a specified location, the specified location (starting from 0) should not exceed the length of the queue, otherwise an error will be reported (array subscripts crossing the bounds).

  • Two addAll methods of list: adding an array of classes
  • public void testAdd(){
            //add Method 1
            Course cr1 = new Course("1", "Course one");
            coursesToSelect.add(cr1);
            Course temp = (Course)coursesToSelect.get(0);
            System.out.println("Courses were added:"+temp.id+":"+temp.name);
            //add Method 2, add to the specified location
            Course cr2 = new Course("2", "Course two");
            coursesToSelect.add(0, cr2);;
            Course temp2 = (Course)coursesToSelect.get(0);
            System.out.println("Courses were added:"+temp2.id+":"+temp2.name);
            //addAll Array addition method 1
            Course[] cr34 = {new Course("3", "Course three"), new Course("4", "Course four")};
            coursesToSelect.addAll(Arrays.asList(cr34));//Method of adding arrays
            Course temp3 = (Course)coursesToSelect.get(2);
            Course temp4 = (Course)coursesToSelect.get(3);
            System.out.println("Two courses were added.:"+temp3.id+":"+temp3.name+
                    ";"+temp4.id+":"+temp4.name);
            //addAll Array addition method 2, added to the specified location
            Course[] cr56 = {new Course("5", "Course five"), new Course("6", "Course six")};
            coursesToSelect.addAll(2, Arrays.asList(cr56));
            Course temp5 = (Course)coursesToSelect.get(2);
            Course temp6 = (Course)coursesToSelect.get(3);
            System.out.println("Two courses were added.:"+temp5.id+":"+temp5.name+
                    ";"+temp6.id+":"+temp6.name);
            
            
        }
  • Traversing List

1.for loop traversal

/**
     * Method of obtaining elements in List
     * @param args
     */
    public void testGet(){
        int size = coursesToSelect.size();
        System.out.println("The following courses are to be selected:");
        for(int i=0; i<size;i++){
            Course cr = (Course)coursesToSelect.get(i);
            System.out.println("Course:"+cr.id+":"+cr.name);
        }
    }

 

2. Iterator traverses List through iterator. Iterator is only used to traverse elements in the set, and it does not have the function of storing elements. It can be said that it depends on a set of existence, can not exist independently.

/**
     * Traversing List s through Iterators
     * @param args
     */
    public void testIterator(){
        Iterator it = coursesToSelect.iterator();
        System.out.println("There are the following courses to be chosen(iterator): ");
        while(it.hasNext()){
            Course cr = (Course)it.next();//Iterator Of next Method
            System.out.println("Course:"+cr.id+":"+cr.name);
        }
    }

3. Accessing Collection Elements through the for each Method

/**
     * Accessing collection elements through the for each method
     * @param args
     */
    public void testForEach(){
        System.out.println("There are the following courses to be chosen(for each): ");
        for(Object obj:coursesToSelect){
            Course cr = (Course)obj;//All the elements taken out are identical. Object Type, need to be strong
            System.out.println("Course:"+cr.id+":"+cr.name);
        }
    }
  • Modify the elements in the List. There is a Set method in List
/**
     * Modify the elements in List
     * @param args
     */
    public void testModify(){
        coursesToSelect.set(4, new Course("7", "Course seven"));
    }
  • Delete the elements in the List. Similar to add, there are remote and remote All
/**
     * Delete the elements in the List
     * @param args
     */
    public void testRemore(){
        Course cr = (Course)coursesToSelect.get(4);
        System.out.println("I am a course:"+cr.id+": "+cr.name+",I'm about to be deleted.");
        coursesToSelect.remove(cr);
        System.out.println("Delete.");
        testForEach();//Method internal call method
    }

The above remove() can also be directly put into the index subscript, which can be deleted directly. Such as remove(4)

removeAll deletes all elements from one collection from another.

public void testRemore(){
        Course[] courses={(Course)coursesToSelect.get(3),(Course)coursesToSelect.get(4)};
        System.out.println("I am a course: 3,4,I'm about to be deleted.");
        coursesToSelect.removeAll(Arrays.asList(courses));    
        System.out.println("Delete.");
        testForEach();//Method internal call method
    }

 

 

** The best attributes in each class in the actual code are private, and getXX or setXX are used when needed

private String id;

public String getId(){
    return id;
}

public String setId(){
    this.id=id;
}

Posted by jackmn1 on Mon, 08 Apr 2019 11:33:31 -0700