Simple Application of Common Methods of Set Interface

Keywords: Java Attribute

1. Set interface features: no duplicate elements, disorderly and unique.

2. Common methods are as follows:

 

boolean  add (E  e ) If the set does not contain the specified element, add the specified element.
void clear ()   Remove all elements from the set.
boolean contains( Object o) If this set contains the specified element, return true.
boolean remove( Object o)  If the specified element exists in this set, it is removed.
int size() Returns the number of elements (set capacity) in this set.

3. How to traverse Set collections?

(1) traversal through iterator Iterator

Iterator's method:

boolean hasNext(): Determines whether the next accessible element exists.

Object next(): Returns the next element to be accessed

(2) Enhanced for loop traversal

 

4. When traversing a collection in List, there is another element accessed by subscripts. Why can't Set s be accessed by subscripts?

Set interface: Elements are not placed in order, elements are not repeatable. List's get method requires subscripts, which is actually put in order. Because Set does not put in order, it cannot be accessed by subscripts.

 

5. A simple application of Set's common methods:

(1) Student class (there is only one attribute to store the name of the student)

package day13;
/**
 * Student class, store the name of the student
 */
public class Student1{
	private String name;
	
	public Student1() {
		super();
	}
	public Student1(String name) {
		super();
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + "]";
	}
	
}

2. StudentService class contains four methods, which are used to add new students, get the total number of students, print all the names of students, and delete a student.

package day13;

import java.util.Iterator;
import java.util.Set;

public class StudentService1{
	
	/**
	 * Added students
	 * @param set
	 * @param student
	 */
	public void add(Set<Student1>set,Student1 student) {
		set.add(student);
		System.out.println("New success!");
	}
	
	
	/**
	 * Get the total number of students
	 * @param set
	 * @return
	 */
	public int getToal(Set<Student1>set) {
		return set.size();
	}
	
	
	/**
	 * Print all student names
	 * @param set
	 */
	public void printName(Set<Student1>set) {
		System.out.println("-------------Print all student names-----------------\n");
		
		//Method 1: Enhanced for cycle traversal
		System.out.println("----Method 1: Enhancement for Cyclic method traversal----:");
		for(Student1 stu:set) {
			if(stu==null) {
				continue;
			}
			System.out.println(stu.getName());
		}
		
		
		//Method 2: Iterator traversal
		System.out.println("------Method 2: Iterator traversal-------:");
		Iterator<Student1> iter = set.iterator();
		Student1 stu = null;
		while(iter.hasNext()) {
			stu = iter.next();
			if(stu == null) {
				continue;
			}
			System.out.println(stu.getName());
		}
		
		//Normal way to increase student//set can not be accessed with subscripts! Make mistakes
		
		for(int i =0 ;i<set.size();i++) {
		}			
	}
	
	/**
	 * Delete a student
	 * @param set
	 * @param studnet
	 * @param str
	 */
	public void delete(Set<Student1>set,String str) {
		
		Iterator<Student1> iter = set.iterator();
		Student1 student = null;
		while(iter.hasNext()) {
			student = iter.next();
			if(student.getName().equals(str)) {
				iter.remove();
				continue;
			}
			System.out.println("The deleted student names are:"+student.getName());
		}
	}
}

3. Test class, mainly for a simple application of set common methods.

package day13;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Test_set {

	public static void main(String[] args) {
		
		Set<Student1>set = new HashSet<Student1>();
		
		StudentService1 service = new StudentService1();
		
		Student1 student1 = new Student1("Great beauty");
		Student1 student2 = new Student1("Tutu");
		
		//Add a student by using the add method of set
		set.add(student1);
		set.add(student1);         //Two student1 were added, and only one student1 was found in the result. (Set does not allow duplication)
				 
		set.add(new Student1("Li Hua"));	
		
		//Call the add method of StudentService 1 to add a student
		service.add(set, student2);
		
		System.out.println("The current total number of students is:"+set.size());
		System.out.println("The current total number of students is:"+service.getToal(set));
		
		//Invoke the printName method of StudentService 1 to print the names of all students
		service.printName(set);
		
		System.out.println("Does the current student include'Xiaomei'?"+set.contains("Xiaomei"));
		System.out.println("Does the current student include'Li Hua'?"+set.contains(student1));
		
		//Delete student1
		set.remove(student1);
		System.out.print("Delete a student (Damei)");
		System.out.println("The current total number of students is:"+service.getToal(set));
		
		//Call the delete method of StudentService 1 to delete a student
		service.delete(set,"Li Hua");
		
		service.printName(set);
		
		//clear list
		set.clear();
		System.out.println("The current total number of students is:"+set.size());
		
	}
}

The results are as follows:

New success!
The current total number of students is: 3
The current total number of students is: 3
Print the names of all the students - -------------------------------------------------------------------------------------------------------------------------

Method 1: Enhance for cycle traversal
Great beauty
Tutu
Li Hua
Method 2: Iterator traversal
Great beauty
Tutu
Li Hua
Does the current student include Xiaomei? false
Does the current student include Li Hua? true
Delete a student (Damei), the current total number of students is: 2
The deleted student name is: graph
Print the names of all the students - -------------------------------------------------------------------------------------------------------------------------

Method 1: Enhance for cycle traversal
Tutu
Method 2: Iterator traversal
Tutu
The current total number of students is: 0
 

Posted by chapm4 on Fri, 09 Aug 2019 22:47:38 -0700