JavaEE组第七周考核任务
==主要部分:Iterator接口和迭代器、泛型、List集合、Set集合、Map集合==
一、概念题
-
什么是集合,请列举集合中常用的类和接口,这些集合和普通数组相比有什么不同?
Java 集合框架主要构图:
-
什么是泛型?
public class User<T>{ private T name; }
//泛型测试方法 public static void main(String[] args) { User user1 = new User(); user1.setName("zwz"); System.out.println(user1.getName() instanceof String); User user2 = new User(); user2.setName(123456); System.out.println(user2.getName() instanceof Integer); User user3 = new User(); user3.setName(123.456); System.out.println(user3.getName() instanceof Double); }
-
了解一下栈结构和队列结构,这两个结构有什么区别,这两种结构有哪些应用场景?
-
Collection和Collections的有什么区别?
-
熟悉迭代器的使用
其中实现Collection子接口的(List ,Set)的类都可以直接使用 iterator方法获取 Iterator对象,用迭代器进行遍历,下面以ArrayList类举例:
package jihe.Collection_.com; import java.util.ArrayList; import java.util.Iterator; import java.util.List; // Collection 迭代器 Iterator 遍历集合 @SuppressWarnings({"all"}) public class CollectionIterator { public static void main(String[] args) { List col =new ArrayList(); col.add(new Book("三国演义","罗贯中",10.2)); col.add(new Book("小李飞刀","古龙",7.4)); col.add(new Book("红楼梦","曹雪芹",15.7)); Iterator iterator= col.iterator(); //2. 使用 while 循环遍历集合 col while(iterator.hasNext()){//判断是否还有数据 //返回下一个元素,类型是Object Object obj=iterator.next();//编译类型为 Object System.out.println("obj="+obj);// 运行类型为Book 多态问题 // 或者直接 System.out.println(iterator.next()); 也没有问题 } } } @SuppressWarnings({"all"}) class Book{ private String name; private String author; private double price; public Book(String name, String author, double price) { this.name = name; this.author = author; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } }
而 实现HashMap接口的类需要用 keySet()方法或者entrySet()方法转化为Set 类型的集合,下面以HashMap为例:
package jihe.Map_.com;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"all"})
public class HashMapTest {
public static void main(String[] args) {
Map map=new HashMap();
/* 创建 Preson 类,包括 name, slo, id, 要求 添加三个员工对象
键 id
值 员工对象
用两种不同的遍历方法 输出工资大于等于18000的员工
*/
map.put(20221001,new Preson("jact",40002.23,202221001));
map.put(20221012,new Preson("tom",3000.45,202221012));
map.put(20222023,new Preson("majunyi",20005.65,20222023));
//获取 Value
// map.keySet() 方法
System.out.println("map.keySet()方法:");
// 1.增强 for
Set set = map.keySet();//得到的是 Key 通过 map.get(Key)=Value
for (Object o :set) {
Preson preson=(Preson) map.get(o);
if(preson.getSlo()>=18000){
System.out.println(preson);
}
}
System.out.println("\n\n\n");
// 2. 迭代器
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Preson preson=(Preson) map.get(next);
if(preson.getSlo()>=18000){
System.out.println(preson);
}
}
// map.entrySet() 方法
System.out.println("\n\n\nmap.entrySet() 方法:");
Set set1 = map.entrySet();//得到 Node 键值对
// 1.增强 for
for (Object o :set1) {
Map.Entry entry=(Map.Entry) o;
Preson preson=(Preson) entry.getValue();
if(preson.getSlo()>=18000){
System.out.println(preson);
}
}
System.out.println("\n\n\n");
// 2. 迭代器
Iterator iterator1 = set1.iterator();
while (iterator1.hasNext()) {
Object next = iterator1.next();
Map.Entry entry=(Map.Entry) next;
Preson preson=(Preson) entry.getValue();
if(preson.getSlo()>=18000){
System.out.println(preson);
}
}
}
}
class Preson{
private String name;
private double slo;
private long id;
public Preson(String name, double slo, long id) {
this.name = name;
this.slo = slo;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSlo() {
return slo;
}
public void setSlo(double slo) {
this.slo = slo;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Preson{" +
"name='" + name + '\'' +
", slo=" + slo +
", id=" + id +
'}';
}
}
二、编程题
用for循环遍历ArrayList、foreach循环遍历LinkedList、Iterator遍历HashSet(这三个集合里的内容可以自己定义,只要遍历输出即可。)
for循环遍历ArrayList
package jihe.Collection_.com;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@SuppressWarnings({"all"})
public class CollectionIterator {
public static void main(String[] args) {
List col =new ArrayList();
col.add(new Book("三国演义","罗贯中",10.2));
col.add(new Book("小李飞刀","古龙",7.4));
col.add(new Book("红楼梦","曹雪芹",15.7));
for (int i = 0; i < col.size(); i++) {
Object o=col.get(i);
System.out.println("o="+o);
}
}
}
@SuppressWarnings({"all"})
class Book{
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
运行示例:
foreach循环遍历LinkedList
package jihe.zuoye;
import java.util.LinkedList;
import java.util.List;
@SuppressWarnings({"all"})
public class LinkedList_Itertor {
public static void main(String[] args) {
List list = new LinkedList();
list.add("majunyi");
list.add("tom");
list.add(1457);
list.add(new B("小黄",8));
list.add(new B("smith",12));
for (Object o :list) {
System.out.println("o="+o);
}
}
}
class B{
private String name;
private int age;
public B(String name, int age) {
this.name = name;
this.age = age;
}
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 "B{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
运行示例:
Iterator遍历HashSet
package jihe.zuoye;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSet_Iterator {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new C("小张","2001-10-23"));
set.add(new C("康康","2002-6-14"));
set.add(new C("jact","2000-3-25"));
set.add("西游记");
set.add("天龙八部");
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("next="+next);
}
}
}
class C{
private String name;
private String brithday;
public C(String name, String brithday) {
this.name = name;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
C c = (C) o;
return Objects.equals(name, c.name) && Objects.equals(brithday, c.brithday);
}
@Override
public int hashCode() {
return Objects.hash(name, brithday);
}
@Override
public String toString() {
return "C{" +
"name='" + name + '\'' +
", brithday='" + brithday + '\'' +
'}';
}
}
运行示例:
控制台输入数字1~7,程序运行输出对应的星期(星期一至星期日)。
package jihe.zuoye;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class Daytime {
public static void main(String[] args) {
List list = new ArrayList();
list.add("星期一");
list.add("星期二");
list.add("星期三");
list.add("星期四");
list.add("星期五");
list.add("星期六");
list.add("星期日");
System.out.println(list.get((new Scanner(System.in)).nextInt() -1));
}
}
运行示例:
定义一个学生类(Student),它包含private成员变量name(姓名),id(学号),age(年龄)。每个属性都要写get,set方法,并重写 toString 方法输出 name, id,age,balance
要求:
创建Student类的 10个对象,并把这些对象放入 TreeSet 集合中(成员变量自定义)
按下面的要求对集合中的元素进行排序,并遍历输出:
-
按年龄从小到大排序
-
当年龄相同时,按学号从大到小排序
package jihe.zuoye;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
@SuppressWarnings({"all"})
public class Tree_Set {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (((Student) o1).getAge() != ((Student) o2).getAge()) {
return ((Student) o1).getAge() - ((Student) o2).getAge();
} else
return ((Student) o2).getId() - ((Student) o1).getId();
}
});
treeSet.add(new Student("tom", 202101, 15));
treeSet.add(new Student("jact", 2021021, 16));
treeSet.add(new Student("zhangfei", 202203, 17));
treeSet.add(new Student("风煞", 2022012, 18));
treeSet.add(new Student("smith", 20221025, 16));
treeSet.add(new Student("bhfdhjs", 2021045, 19));
treeSet.add(new Student("dfdjst", 202109, 14));
treeSet.add(new Student("nfhafrojiti", 2000156, 19));
treeSet.add(new Student("王刚", 2002031, 17));
treeSet.add(new Student("dfreejwgru", 2003051, 18));
Iterator iterator = treeSet.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("Student=" + next);
}
}
}
class Student {
private String name;
private int id;
private int age;
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
运行示例: