1.java.util 2.队列先进先出,栈堆先进后出 3.链表 4.LinkedList 5.TreeSet 6.Comparable 7.Map 8.next()
1.AC 2.A 3.D 4.B 5.D 6.C 7.C 8.C 9.CD
1.× 2.√ 3.× 4.× 5.√ 6.√ 7.√ 8.× 9.√ 10.× 11.× 12.√
2.List是有序的集合,使用此接口能够精确控制每个元素插入的位置,用户能够用索引来访问List中的元素,这类似于java的数组。
Set是一种不包含重复的元素的集合,即任意的两个元素都有equals()方法,Set最多有一个null元素。
Map接口,将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
3.区别:Vector是线程安全的,效率低。ArrayList是线程不安全的,效率高。
联系:底层都是数组实现的。
4.Hashtable是JDK1.0版本出现的,是线程安全的,效率低;
HashMap是JDK1.2版本出现的,是线程不安全的,效率高;
Hashtable不可以存储null键和null值;
HashMap可以存储null键和null值(目的是为了让后续代码可以继续执行)
package com.zuikc.bean; public class Book { private int id; private String name; private double price; private String press; public Book() { super(); // TODO Auto-generated constructor stub } public Book(int id, String name, double price, String press) { super(); this.id = id; this.name = name; this.price = price; this.press = press; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((press == null) ? 0 : press.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Book other = (Book) obj; if (id != other.id) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (press == null) { if (other.press != null) { return false; } } else if (!press.equals(other.press)) { return false; } if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) { return false; } return true; } } package com.zuikc.kehoutest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.zuikc.bean.Book; public class Test6 { public static void main(String[] args) { // demo1(); Map<Integer, Book> m = new HashMap<>(); m.put(100, new Book(100,"b1",20.5,"aaa")); m.put(101, new Book(101,"b2",21.5,"bbb")); m.put(102, new Book(102,"b3",22.5,"ccc")); m.put(103, new Book(103,"b4",23.5,"ddd")); for(Entry<Integer, Book> entry : m.entrySet()) { System.out.println(entry.getKey() + "..." + entry.getValue()); } } private static void demo1() { List<Book> list = new ArrayList<>(); list.add(new Book(100,"b1",20.5,"aaa")); list.add(new Book(101,"b2",21.5,"bbb")); list.add(new Book(102,"b3",22.5,"ccc")); list.add(new Book(103,"b4",23.5,"ddd")); for (Book book : list) { System.out.println(book.toString()); } } }
package com.zuikc.bean; public class Book { private int id; private String name; private double price; private String press; public Book() { super(); // TODO Auto-generated constructor stub } public Book(int id, String name, double price, String press) { super(); this.id = id; this.name = name; this.price = price; this.press = press; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((press == null) ? 0 : press.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Book other = (Book) obj; if (id != other.id) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (press == null) { if (other.press != null) { return false; } } else if (!press.equals(other.press)) { return false; } if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) { return false; } return true; } } package com.zuikc.kehoutest; import java.util.HashMap; import java.util.HashSet; import java.util.TreeSet; import com.zuikc.bean.Book; public class Test7 { public static void main(String[] args) { // demo1(); TreeSet<Book> ts = new TreeSet<>(); ts.add(new Book(100,"b1",20.5,"aaa")); ts.add(new Book(100,"b1",20.5,"aaa")); ts.add(new Book(101,"b2",21.5,"bbb")); ts.add(new Book(101,"b2",21.5,"bbb")); ts.add(new Book(102,"b3",22.5,"ccc")); ts.add(new Book(103,"b4",23.5,"ddd")); for(Book book : ts) { System.out.println(book); } } private static void demo1() { HashSet<Book> hs = new HashSet<>(); hs.add(new Book(100,"b1",20.5,"aaa")); hs.add(new Book(101,"b2",21.5,"bbb")); hs.add(new Book(102,"b3",22.5,"ccc")); hs.add(new Book(103,"b4",23.5,"ddd")); for(Book book : hs) { System.out.println(book); } } }
package com.zuikc.bean; public class StudentEntry { private int key; private Student2 s; public StudentEntry() { super(); // TODO Auto-generated constructor stub } public StudentEntry(int key, Student2 s) { super(); this.key = key; this.s = s; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public Student2 getS() { return s; } public void setS(Student2 s) { this.s = s; } } package com.zuikc.kehoutest; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.zuikc.bean.Student2; import com.zuikc.bean.StudentEntry; public class Test8 { public static void main(String[] args) { // listToMap(); Map<Integer, Student2> m = new HashMap<>(); m.put(100, new Student2(100, "张三", 23, "男")); m.put(101, new Student2(101, "李四", 24, "男")); m.put(102, new Student2(100, "王五", 25, "男")); m.put(103, new Student2(100, "赵六", 26, "男")); List<StudentEntry> list = new ArrayList<>(); for(Entry<Integer, Student2> entry : m.entrySet()) { StudentEntry se = new StudentEntry(); se.setKey(entry.getKey()); se.setS(entry.getValue()); list.add(se); } for(StudentEntry se : list) { System.out.println(se.getKey() + "..." + se.getS()); } } private static void listToMap() { List<Student2> list = new ArrayList<>(); list.add(new Student2(100, "张三", 23, "男")); list.add(new Student2(101, "李四", 24, "男")); list.add(new Student2(100, "王五", 25, "男")); list.add(new Student2(100, "赵六", 26, "男")); for (Student2 s : list) { System.out.println(s); } Map<Integer, Student2> m = new HashMap<>(); Iterator<Student2> it = list.iterator(); while (it.hasNext()) { Student2 s1 = it.next(); m.put(s1.getId(), s1); } Set<Entry<Integer, Student2>> entrySet = m.entrySet(); for (Entry<Integer, Student2> entry : entrySet) { System.out.println(entry.getKey() + "..." + entry.getValue()); } } }
package com.zuikc.kehoutest; import java.util.HashMap; import java.util.Map; public class Test9 { public static void main(String[] args) { String str = "aa@sohu.com,bb@163.com,cc@sina.com"; String strs[] = str.split(","); Map<String, String> emailMap = new HashMap<String, String>(); for (String email : strs) { String temp[] = email.split("@"); emailMap.put(temp[0], temp[1]); } System.out.println(emailMap.toString()); } }
package com.zuikc.bean; public class Student3 implements Comparable<Student3>{ private int id; private String name; private int age; public Student3() { super(); // TODO Auto-generated constructor stub } public Student3(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student3 [id=" + id + ", name=" + name + ", age=" + age + "]"; } @Override public int compareTo(Student3 s) { return this.age - s.age; } } package com.zuikc.kehoutest; import java.util.Scanner; import java.util.Set; import javax.swing.text.html.HTMLDocument.Iterator; import com.zuikc.bean.Student3; public class Test10 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Set<Student3> stuSet = saveStudentInfo(); Iterator<Student3> it = stuSet.iterator(); while (it.hasNext()) { String info = it.next().toString(); System.out.println(info); } } private static void saveStudentInfo() { while (true) { System.out.println("请输入学生信息(编号#姓名#年龄)"); String line = sc.nextLine(); if ("exit".equals(line)) { break; } String[] info = line.split("#"); Student3 stu = new Student3(Integer.parseInt(info[0]), info[1], Integer.parseInt(info[2])); stuSet.add(stu); } return stuSet; } }