"Java Basic Introduction 2nd Edition"-dark horse programmers after class answers and detailed explanation Chapter 6 collection

1. Fill in the blanks

1、Comparator
2、hashNext()、next()
3、键、值
4、ArrayList、LinkedList,HashSet、TreeSet,HashMap、TreeMap
5、forEach(Consumer action)

2. True or false

1、错   2、对   3、对   4、错   5、对

Three, multiple choice questions

1、BC      2、D      3、C      4、D     5、ABC   

Four, short answer questions

1. In order to enable the program to conveniently store and manipulate a set of data with a variable number, JDK provides a set of class libraries. These classes are located
in the java.util package and are collectively referred to as collections. Commonly used interfaces and classes in the collection framework include List, Set, ArrayList, HashSet, Map, HashMap, and TreeMap.

2. The characteristic of List is that the elements are orderly and repeatable. The main implementation classes of the List interface are ArrayList and LinkedList. The characteristic of Set is that the elements are disordered and cannot be repeated. The main implementation classes of the Set interface are HashSet and TreeSet. The characteristic of Map is that the stored elements are key (Key) and value (Value) mapping relations, and the elements appear in pairs. The main implementation classes of the Map interface are HashMap and TreeMap.

3. Collection is a singleton collection interface. It provides general methods for basic operations on collection objects. Collections is a tool category. It contains various methods related to collection operations.

Five, programming questions

1.import java.util.*;
public class Test02 {
    
    
	public static void main(String[] args) {
    
    
		HashSet hashSet = new HashSet();
		Person p1 = new Person("Jack",25);
		Person p2 = new Person("Rose",23);
		Person p3 = new Person("Jack",27);
		hashSet.add(p1);
		hashSet.add(p2);
		hashSet.add(p3);
		for(Object obj:hashSet){
    
    
			Person p=(Person)obj;
			System.out.println(p.name+":"+p.age);
		}
	}
}
class Person{
    
    
	String name;
	int age;
	public Person(String name, int age) {
    
    
		super();
		this.name = name;
		this.age = age;
	}
	public int hashCode() {
    
    
	   return name.hashCode();
	}
	public boolean equals(Object obj) {
    
    
		if (this == obj)
			return true;
		if (obj == null)
return false;
		Person p = (Person) obj;
		return p.name.equals(this.name);
	}
}	
2.import java.util.*;
public class Test03 {
    
    
	public static void main(String[] args) {
    
    
		TreeMap map = new TreeMap(new MyComparator());
		map.put("1", "Lucy");
		map.put("2", "Lucy");
		map.put("3", "John");
		map.put("4", "Smith");
		map.put("5", "Amanda");
		for (Object key : map.keySet()) {
    
    
			System.out.println(key + ":" + map.get(key));
		}
	}
}
class MyComparator implements Comparator {
    
    
	public int compare(Object obj1, Object obj2) {
    
    
		String ele1 = (String) obj1;
		String ele2 = (String) obj2;
		return ele2.compareTo(ele1);
	}
}

Six, the original question and its analysis

Nothing.

Guess you like

Origin blog.csdn.net/hypertext123/article/details/109297609