一.填空题
1. java.util
2. 队列先进先出,栈先进后出
3. 链表
4. LinkedList
5. TreeSet
6. Comparable
7. HashMap
8. next()
二.选择题
1. AC
2. A
3. D
4. B
5. D
6. C
7. C
8. C
9. CD
三.判断题
1. F
2. T
3. F
4. F
5. T
6. T
7. T
8. F
9. T
10. F
11. F
12. T
四.简答题
1.熟练掌握Collection集合和Map集合的体系图
Collection:
List:
ArrayList
LinkedList
Vector
Set:
HashSet
TreeSet
Map:
Hashtable
HashMap:
LinkedHashMap
TreeMap
2.List Set Collection Map的特点和区别以及什么时候使用该集合
Collection和Map:
Map是双列的,Collection是单列的;
Map中的键是唯一的,Collection的子接口Set的实现类中的元素是唯一的;
Map集合的数据结构只对键有效,Collection集合的数据结构对元素有效;
Set的底层依赖Map实现.
List和Set:
List存取有序,有索引,可以重复
Set存取无序,无索引,不可以重复
3.Vector和ArrayList的区别和联系
Vector: 1.0出现,数组实现,线程安全,增删查改都比较慢;
ArrayList: 1.2出现,数组实现,线程不安全,查改快,增删慢.
4.简述HashMap和Hashtable的区别
HashMap: 1.2出现,线程不安全,效率高; 可以存储null键null值
Hashtable: 1.0出现,线程安全,效率低; 不可以存储null键null值
五.编码题
1.
1 public class BookCatalog { 2 private int num; 3 private String name; 4 private double price; 5 private String publisher; 6 public BookCatalog() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public BookCatalog(int num, String name, double price, String publisher) { 11 super(); 12 this.num = num; 13 this.name = name; 14 this.price = price; 15 this.publisher = publisher; 16 } 17 public int getNum() { 18 return num; 19 } 20 public void setNum(int num) { 21 this.num = num; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public double getPrice() { 30 return price; 31 } 32 public void setPrice(double price) { 33 this.price = price; 34 } 35 public String getPublisher() { 36 return publisher; 37 } 38 public void setPublisher(String publisher) { 39 this.publisher = publisher; 40 } 41 @Override 42 public String toString() { 43 return "BookCatalog [num=" + num + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; 44 } 45 @Override 46 public int hashCode() { 47 final int prime = 31; 48 int result = 1; 49 result = prime * result + ((name == null) ? 0 : name.hashCode()); 50 result = prime * result + num; 51 long temp; 52 temp = Double.doubleToLongBits(price); 53 result = prime * result + (int) (temp ^ (temp >>> 32)); 54 result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); 55 return result; 56 } 57 @Override 58 public boolean equals(Object obj) { 59 if (this == obj) 60 return true; 61 if (obj == null) 62 return false; 63 if (getClass() != obj.getClass()) 64 return false; 65 BookCatalog other = (BookCatalog) obj; 66 if (name == null) { 67 if (other.name != null) 68 return false; 69 } else if (!name.equals(other.name)) 70 return false; 71 if (num != other.num) 72 return false; 73 if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) 74 return false; 75 if (publisher == null) { 76 if (other.publisher != null) 77 return false; 78 } else if (!publisher.equals(other.publisher)) 79 return false; 80 return true; 81 } 82 }
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 4 public class Books { 5 public static void main(String[] args) { 6 ArrayList<BookCatalog> bookList = new ArrayList<>(); 7 bookList.add(new BookCatalog(22, "西游记", 63.8, "大唐出版")); 8 bookList.add(new BookCatalog(35, "红楼梦", 43.8, "贾府出版")); 9 bookList.add(new BookCatalog(18, "水浒传", 163.8, "梁山出版")); 10 bookList.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 11 bookList.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 12 13 for (BookCatalog b : bookList) { 14 System.out.println( 15 "书号:" + b.getNum() + " 书名:" + b.getName() + " 价格:" + b.getPrice() + " 出版社:" + b.getPublisher()); 16 } 17 18 HashMap<Integer, BookCatalog> bookMap = new HashMap<>(); 19 for (BookCatalog b : bookList) { 20 bookMap.put(b.getNum(), b); 21 } 22 23 for (BookCatalog b : bookMap.values()) { 24 System.out.println( 25 "书号:" + b.getNum() + " 书名:" + b.getName() + " 价格:" + b.getPrice() + " 出版社:" + b.getPublisher()); 26 } 27 } 28 }
2.
1 public class BookCatalog { 2 private int num; 3 private String name; 4 private double price; 5 private String publisher; 6 public BookCatalog() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public BookCatalog(int num, String name, double price, String publisher) { 11 super(); 12 this.num = num; 13 this.name = name; 14 this.price = price; 15 this.publisher = publisher; 16 } 17 public int getNum() { 18 return num; 19 } 20 public void setNum(int num) { 21 this.num = num; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public double getPrice() { 30 return price; 31 } 32 public void setPrice(double price) { 33 this.price = price; 34 } 35 public String getPublisher() { 36 return publisher; 37 } 38 public void setPublisher(String publisher) { 39 this.publisher = publisher; 40 } 41 @Override 42 public String toString() { 43 return "BookCatalog [num=" + num + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; 44 } 45 @Override 46 public int hashCode() { 47 final int prime = 31; 48 int result = 1; 49 result = prime * result + ((name == null) ? 0 : name.hashCode()); 50 result = prime * result + num; 51 long temp; 52 temp = Double.doubleToLongBits(price); 53 result = prime * result + (int) (temp ^ (temp >>> 32)); 54 result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); 55 return result; 56 } 57 @Override 58 public boolean equals(Object obj) { 59 if (this == obj) 60 return true; 61 if (obj == null) 62 return false; 63 if (getClass() != obj.getClass()) 64 return false; 65 BookCatalog other = (BookCatalog) obj; 66 if (name == null) { 67 if (other.name != null) 68 return false; 69 } else if (!name.equals(other.name)) 70 return false; 71 if (num != other.num) 72 return false; 73 if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) 74 return false; 75 if (publisher == null) { 76 if (other.publisher != null) 77 return false; 78 } else if (!publisher.equals(other.publisher)) 79 return false; 80 return true; 81 } 82 }
1 import java.util.HashSet; 2 3 public class Books02 { 4 public static void main(String[] args) { 5 HashSet<BookCatalog> bookHashSet = new HashSet<>(); 6 bookHashSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 7 bookHashSet.add(new BookCatalog(22, "西游记", 63.8, "大唐出版")); 8 bookHashSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 9 bookHashSet.add(new BookCatalog(35, "红楼梦", 43.8, "贾府出版")); 10 bookHashSet.add(new BookCatalog(18, "水浒传", 163.8, "梁山出版")); 11 bookHashSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 12 bookHashSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 13 bookHashSet.add(new BookCatalog(35, "红楼梦", 43.8, "贾府出版")); 14 bookHashSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 15 bookHashSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 16 17 for (BookCatalog b : bookHashSet) { 18 System.out.println(b); 19 } 20 } 21 }
1 import java.util.Comparator; 2 import java.util.TreeSet; 3 4 public class Books03 { 5 public static void main(String[] args) { 6 TreeSet<BookCatalog> bookTreeSet = new TreeSet<>(new Comparator<BookCatalog>() { 7 8 @Override 9 public int compare(BookCatalog b1, BookCatalog b2) { 10 int num = b1.getNum() - b2.getNum(); 11 return num; 12 } 13 }); 14 15 bookTreeSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 16 bookTreeSet.add(new BookCatalog(22, "西游记", 63.8, "大唐出版")); 17 bookTreeSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 18 bookTreeSet.add(new BookCatalog(35, "红楼梦", 43.8, "贾府出版")); 19 bookTreeSet.add(new BookCatalog(18, "水浒传", 163.8, "梁山出版")); 20 bookTreeSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 21 bookTreeSet.add(new BookCatalog(212, "三国演义", 3.8, "东汉出版")); 22 bookTreeSet.add(new BookCatalog(35, "红楼梦", 43.8, "贾府出版")); 23 bookTreeSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 24 bookTreeSet.add(new BookCatalog(6, "从入门到精通", 9.8, "chang4出版")); 25 26 for (BookCatalog b : bookTreeSet) { 27 System.out.println(b); 28 } 29 } 30 }
3.
1 public class Student { 2 private int id; 3 private String name; 4 private int age; 5 private String sex; 6 public Student() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public Student(int id, String name, int age, String sex) { 11 super(); 12 this.id = id; 13 this.name = name; 14 this.age = age; 15 this.sex = sex; 16 } 17 public int getId() { 18 return id; 19 } 20 public void setId(int id) { 21 this.id = id; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public int getAge() { 30 return age; 31 } 32 public void setAge(int age) { 33 this.age = age; 34 } 35 public String getSex() { 36 return sex; 37 } 38 public void setSex(String sex) { 39 this.sex = sex; 40 } 41 @Override 42 public String toString() { 43 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; 44 } 45 @Override 46 public int hashCode() { 47 final int prime = 31; 48 int result = 1; 49 result = prime * result + age; 50 result = prime * result + id; 51 result = prime * result + ((name == null) ? 0 : name.hashCode()); 52 result = prime * result + ((sex == null) ? 0 : sex.hashCode()); 53 return result; 54 } 55 @Override 56 public boolean equals(Object obj) { 57 if (this == obj) 58 return true; 59 if (obj == null) 60 return false; 61 if (getClass() != obj.getClass()) 62 return false; 63 Student other = (Student) obj; 64 if (age != other.age) 65 return false; 66 if (id != other.id) 67 return false; 68 if (name == null) { 69 if (other.name != null) 70 return false; 71 } else if (!name.equals(other.name)) 72 return false; 73 if (sex == null) { 74 if (other.sex != null) 75 return false; 76 } else if (!sex.equals(other.sex)) 77 return false; 78 return true; 79 } 80 }
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map.Entry; 4 5 public class List2Map { 6 public static void main(String[] args) { 7 ArrayList<Student> stuList = new ArrayList<>(); 8 stuList.add(new Student(12, "张三", 23, "女")); 9 stuList.add(new Student(6, "李四", 12, "男")); 10 stuList.add(new Student(32, "赵六", 53, "男")); 11 stuList.add(new Student(122, "孙三", 72, "男")); 12 stuList.add(new Student(51, "刘八", 34, "女")); 13 stuList.add(new Student(17, "王二", 45, "男")); 14 15 for (Student s : stuList) { 16 System.out.println(s); 17 } 18 19 listToMap(stuList); 20 } 21 22 public static void listToMap(ArrayList<Student> stuList) { 23 HashMap<Integer, Student> stuMap = new HashMap<>(); 24 for (Student s : stuList) { 25 stuMap.put(s.getId(), s); 26 } 27 28 for (Entry<Integer, Student> entry : stuMap.entrySet()) { 29 System.out.println(entry.getKey() + "..." + entry.getValue()); 30 } 31 } 32 }
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map.Entry; 4 5 public class StudentEntry { 6 public static void main(String[] args) { 7 Student s1 = new Student(12, "张三", 23, "女"); 8 Student s2 = new Student(6, "李四", 12, "男"); 9 Student s3 = new Student(32, "赵六", 53, "男"); 10 Student s4 = new Student(122, "孙三", 72, "男"); 11 Student s5 = new Student(51, "刘八", 34, "女"); 12 Student s6 = new Student(17, "王二", 45, "男"); 13 14 HashMap<Integer, Student> stuMap = new HashMap<>(); 15 stuMap.put(s1.getId(), s1); 16 stuMap.put(s2.getId(), s2); 17 stuMap.put(s3.getId(), s3); 18 stuMap.put(s4.getId(), s4); 19 stuMap.put(s5.getId(), s5); 20 stuMap.put(s6.getId(), s6); 21 22 mapToList(stuMap); 23 } 24 25 public static void mapToList(HashMap<Integer, Student> stuMap) { 26 ArrayList<Entry<Integer, Student>> stuList = new ArrayList<>(); 27 for (Entry<Integer, Student> entry : stuMap.entrySet()) { 28 stuList.add(entry); 29 } 30 for (Entry<Integer, Student> entry : stuList) { 31 System.out.println(entry); 32 } 33 } 34 }
六.可选题
1.
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map.Entry; 4 5 public class EmailAddress { 6 public static void main(String[] args) { 7 ArrayList<String> email = new ArrayList<>(); 8 email.add("[email protected]"); 9 email.add("[email protected]"); 10 email.add("[email protected]"); 11 email.add("[email protected]"); 12 email.add("[email protected]"); 13 email.add("[email protected]"); 14 15 HashMap<String, String> emailDeparted = depart(email); 16 17 print(emailDeparted); 18 } 19 20 public static HashMap<String, String> depart(ArrayList<String> email) { 21 HashMap<String, String> emailDeparted = new HashMap<>(); 22 for(String s : email) { 23 int at = s.indexOf('@'); 24 int dot = s.indexOf('.'); 25 emailDeparted.put(s.substring(0, at), s.substring(at + 1, dot)); 26 } 27 return emailDeparted; 28 } 29 30 public static void print(HashMap<String, String> emailDeparted) { 31 for (Entry<String,String> entry : emailDeparted.entrySet()) { 32 System.out.println("id:" + entry.getKey() + " domain:" + entry.getValue()); 33 } 34 } 35 }
2.
1 public class Student01 { 2 private String id; 3 private String name; 4 private String age; 5 public Student01() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 public Student01(String id, String name, String age) { 10 super(); 11 this.id = id; 12 this.name = name; 13 this.age = age; 14 } 15 public String getId() { 16 return id; 17 } 18 public void setId(String id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public String getAge() { 28 return age; 29 } 30 public void setAge(String age) { 31 this.age = age; 32 } 33 @Override 34 public String toString() { 35 return "Student01 [id=" + id + ", name=" + name + ", age=" + age + "]"; 36 } 37 }
1 import java.util.Comparator; 2 import java.util.Scanner; 3 import java.util.TreeSet; 4 5 public class StudentInfo { 6 public static void main(String[] args) { 7 TreeSet<Student01> stu = new TreeSet<>(new Comparator<Student01>() { 8 public int compare(Student01 s1, Student01 s2) { 9 int num = s1.getAge().compareTo(s2.getAge()); 10 return num == 0 ? 1 : num; 11 } 12 }); 13 getInfo(stu); 14 print(stu); 15 } 16 17 public static void getInfo(TreeSet<Student01> stu) { 18 Scanner sc = new Scanner(System.in); 19 while (true) { 20 System.out.println("请输入学生信息(格式:学号#姓名#年龄 输入exit退出):"); 21 String info = sc.nextLine(); 22 if ("exit".equals(info)) { 23 break; 24 } else { 25 int pound1 = info.indexOf('#'); 26 int pound2 = info.indexOf('#', pound1 + 1); 27 String id = info.substring(0, pound1); 28 String name = info.substring(pound1 + 1, pound2); 29 String age = info.substring(pound2 + 1); 30 stu.add(new Student01(id, name, age)); 31 } 32 } 33 sc.close(); 34 } 35 36 private static void print(TreeSet<Student01> stu) { 37 for (Student01 s : stu) { 38 System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 年龄:" + s.getAge()); 39 } 40 } 41 }