1. 请定义一个Collection类型的集合,存储以下字符串:
“JavaEE企业级开发指南”,”Oracle高级编程”,”MySQL从入门到精通”,”Java架构师之路”
public class Test1 {
public static void main(String[] args) {
//多态创建一个集合
Collection<String> strings = new ArrayList<>();
//添加字符串
strings.add("JavaEE企业级开发指南");
strings.add("Oracle高级编程");
strings.add("MySQl从入门到精通");
strings.add("Java架构师之路");
//1. 使用迭代器遍历所有元素,并打印
printAL(strings);
//2. 使用迭代器遍历所有元素,筛选书名小于10个字符的,并打印;
printScreen(strings, 10);
//3. 使用迭代器遍历所有元素,筛选书名中包含“Java”的,并打印
printContains(strings, "Java");
//4. 如果书名中包含“Oracle”,则删掉此书。删掉后,遍历集合,打印所有书名。
printRemove(strings, "Oracle");
}
private static void printRemove(Collection<String> arrayList, String string) {
if (arrayList.isEmpty()) {
return;
}
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
if (iterator.next().contains(string)) {
iterator.remove();
}
}
System.out.println(arrayList);
}
private static void printContains(Collection<String> arrayList, String string) {
if (arrayList.isEmpty()) {
return;
}
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.contains(string)) {
System.out.println(next);
}
}
}
private static void printScreen(Collection<String> arrayList, int bound) {
if (arrayList.isEmpty()) {
return;
}
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.length() < bound) {
System.out.println(next);
}
}
}
public static void printAL(Collection<String> arrayList) {
if (arrayList.isEmpty()) {
return;
}
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
2. 现有一个集合,里面有一堆Student对象
张三,22 李四,26 张翠山,38 赵六,19 张三丰,103 张无忌,17 赵敏,16
**要求:**
集合中的人需要以对象的形式存在,人只要有姓名和年龄两个成员变量即可;
请定义方法找出集合中所有姓张的人并且年龄大于18岁的并全部返回, 然后在main方法中输出返回的人;
public class Test2 {
public static void main(String[] args) {
//多态创建Student类型的数组
Collection<Student> students = new ArrayList<>();
students.add(new Student("张三", 22));
students.add(new Student("李四", 26));
students.add(new Student("张翠山", 38));
students.add(new Student("赵六", 19));
students.add(new Student("张三丰", 103));
students.add(new Student("张无忌", 17));
students.add(new Student("赵敏", 16));
Collection<Student> list = conformance(students, "张", 18);
System.out.println(list);
}
private static Collection<Student> conformance(Collection<Student> students, String name, int age) {
Collection<Student> list = new ArrayList<>();
students.forEach(new Consumer<Student>() {
@Override
public void accept(Student student) {
if (student.getName().startsWith(name) && student.getAge() > 18) {
list.add(student);
}
}
});
return list;
}
}
class Student{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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 "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3. 目前有一个Book类,属性有:编号,名称,单价,出版社
1)使用HashSet存储多个商品信息(属性值相同的只存一本),遍历并输出
2)使用TreeSet存储多个商品信息(要求按照价格升序排列),遍历并输出
public class Test3 {
public static void main(String[] args) {
creatHashSet();
System.out.println("——————————————————————————————————————————————————————");
creatTreeSet();
}
public static void creatTreeSet() {
TreeSet<Book> books = new TreeSet<>((o1, o2) -> o1.getPrice() - o2.getPrice());
books.add(new Book(1, "JavaEE企业级开发指南", 128, "新华出版社"));
books.add(new Book(2, "Oracle高级编程", 88, "新华出版社"));
books.add(new Book(3, "MySQL从入门到精通", 38, "中华出版社"));
books.add(new Book(4, "Java架构师之路", 98, "晨光出版社"));
books.add(new Book(1, "JavaEE企业级开发指南", 128, "新华出版社"));
books.add(new Book(5, "重生之我在黑马学Java", 198, "中华出版社"));
for (Book book : books) {
System.out.println(book);
}
}
public static void creatHashSet(){
HashSet<Book> set = new HashSet<>();
set.add(new Book(1, "JavaEE企业级开发指南", 128, "新华出版社"));
set.add(new Book(2, "Oracle高级编程", 88, "新华出版社"));
set.add(new Book(3, "MySQL从入门到精通", 38, "中华出版社"));
set.add(new Book(4, "Java架构师之路", 98, "晨光出版社"));
set.add(new Book(1, "JavaEE企业级开发指南", 128, "新华出版社"));
set.add(new Book(5, "重生之我在黑马学Java", 198, "中华出版社"));
for (Book book : set) {
System.out.println(book);
}
}
}
class Book{
private int id;
private String name;
private int price;
private String publishingHouse;
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 getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getPublishingHouse() {
return publishingHouse;
}
public void setPublishingHouse(String publishingHouse) {
this.publishingHouse = publishingHouse;
}
public Book(int id, String name, int price, String publishingHouse) {
this.id = id;
this.name = name;
this.price = price;
this.publishingHouse = publishingHouse;
}
public Book() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return id == book.id && price == book.price && Objects.equals(name, book.name) && Objects.equals(publishingHouse, book.publishingHouse);
}
@Override
public int hashCode() {
return Objects.hash(id, name, price, publishingHouse);
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", publishingHouse='" + publishingHouse + '\'' +
'}';
}
}