容器--Set(集合)
package Day12_21_00;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
* Collection
* -Set
* -HashSet
* -TreeSet-自动去重的同时还可以进行排序
*/
public class Test01
{
public static void main(String[] args)
{
Set<String> set=new HashSet<>();
set.add("apple");
set.add("grape");
set.add("blueberry");
set.add("pitaya");
set.add("apple");
System.out.println(set.size());//打印结果:4
/*
* -注意这里虽然有两个apple,但是hashSet不允许重复,因此大小仍然为4
* -集合在存放元素的时候不是按顺序存放的,因此在遍历hashSet的时候不能用
* 常规for循环(没有set.get(i)这个方法)
*/
System.out.println("====forEach====");
for (String string : set)
{
System.out.println(string);
}
//删除元素也只能删除对象名
set.remove("pitaya");
System.out.println("====itreter=====");
Iterator<String> iterator=set.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
//清空集合
System.out.println("=================");
set.clear();
System.out.println(set.size());
}
package Day12_21_00;
//import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
//哈希存储只要能设计出一个好的哈希函数就是存取性能都特别好的存储方案
//好的哈希函数就是:让不同的对象尽可能产生不同的哈希码
//哈希存储/哈希函数有的地方也称之为散列存储/散列函数
//如果要使用HashSet,那么一定要重写两个方法(用eclipse自动生成):
// - hashCode() ----> 让相同的对象产生相同的哈希码,让不同的对象尽可能产生不同的哈希码
// - equals() ----> 定义比较两个对象的规则(比引用、类型、属性)
// equals方法满则的四个特征: - 自反性 - 对称性 - 传递性 - 一致性
@Override
public int hashCode() {
final int prime = 31;
int result = 17;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return name + ":" + age;
}
//要使用TreeSet,那么对象必须是课比较的,因为TreeSet底层是一颗排序树
//可以通过实现Comparable接口并重写compareTo方法来实现比较的功能
@Override
public int compareTo(Student other) {
if (this.name.compareTo(other.name) == 0) {
return this.age - other.age;
}
else {
return this.name.compareTo(other.name);
}
}
}
class Test02 {
public static void main(String[] args) {
//Set<Student> set = new HashSet<>();
Set<Student> set = new TreeSet<>();
Student s1 = new Student("王大锤", 18);
set.add(s1);
set.add(new Student("Jack", 36));
Student s2 = new Student("王大锤", 18);
set.add(s2);
System.out.println(set.size());
for (Student student : set) {
System.out.print(student.hashCode() + "--->");
System.out.println(student);
}
}
}
}
模糊查询
package Day12_21_00;
//indexOf是查找第一次出现的位置
class Test03 {
public static void main(String[] args) {
String string = "hello,world";
System.out.println(string.indexOf("lo"));
}
}
案例--通讯录
重点知识:使用接口来实现解耦合---函数式接口的使用
package Day12_21_00;
import java.util.List;
import java.util.Scanner;
import Day12_21_00.ContactInfo;
import Day12_21_00.ContactManager;
class Test04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ContactManager manager = new ContactManager();
boolean isRunning = true;
System.out.println("===我的通讯录===");
while (isRunning) {
int choice;
do {
System.out.println("\n1. 添加联系人");
System.out.println("2. 联系人列表");
System.out.println("3. 查找联系人");
System.out.println("4. 退出\n");
System.out.print("请选择: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 4);
switch (choice) {
case 1: {
System.out.print("姓名: ");
String name = scanner.next();
System.out.print("电话: ");
String tel = scanner.next();
System.out.print("邮箱: ");
String email = scanner.next();
manager.add(new ContactInfo(name, tel, email));
break;
}
case 2:
showContactInfo(manager.findAll());
break;
case 3:
System.out.print("请输入姓名: ");
String keyword = scanner.next();
List<ContactInfo> list = manager.findByName(keyword);
showContactInfo(list);
if (list.size() > 0) {
System.out.print("要删除联系人吗? (1. 是; 2. 否)");
int needsDelete = scanner.nextInt();
if (needsDelete == 1) {
int index;
do {
System.out.print("请输入联系人编号: ");
index = scanner.nextInt();
} while (index < 1 || index > list.size());
manager.delete(list.get(index - 1));
}
}
break;
case 4:
//回调方法
manager.save(new CloudSave());
manager.save(new Storage()
{
@Override
public void saveDate(List<ContactInfo> infor)
{
System.out.println("闪存");
}
});
manager.save(infor->{
System.out.println("本地储存");
});
System.out.println("\n=====再见=====");
isRunning = false;
break;
}
}
scanner.close();
}
private static void showContactInfo(List<ContactInfo> list) {
System.out.println("总共有" + list.size() + "位联系人");
for (int i = 0; i < list.size(); i++) {
System.out.println("\n=======联系人" + (i + 1) + "=======");
System.out.println(list.get(i));
System.out.println("===================\n");
}
}
}
package Day12_21_00;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ContactManager {
private List<ContactInfo> infoList;
public ContactManager() {
infoList = new ArrayList<>();
infoList.add(new ContactInfo("骆昊", "13812345678", "jackfrued@126.com"));
infoList.add(new ContactInfo("王大锤", "13100229900", "dcwang@qq.com"));
infoList.add(new ContactInfo("女王大人", "13501230123", "bigqueen@yahoo.com"));
infoList.add(new ContactInfo("张大炮", "13606162636", "zhangcannon@163.com"));
}
public void add(ContactInfo info) {
infoList.add(info);
}
public void delete(ContactInfo info) {
infoList.remove(info);
}
public List<ContactInfo> findByName(String name) {
List<ContactInfo> resultList = new ArrayList<>();
for (ContactInfo info : infoList) {
if (info.getName().indexOf(name) != -1) {
resultList.add(info);
}
}
return resultList.size() > 0 ? resultList : Collections.emptyList();
}
public List<ContactInfo> findAll() {
return infoList;
}
public void save(Storage storage)
{
storage.saveDate(infoList);
}
}
package Day12_21_00;
import java.util.List;
@FunctionalInterface//函数式接口--只能拥有一个方法
//若要在函数式接口里再添加方法,要用默认实现
public interface Storage
{
public void saveDate(List<ContactInfo > infor);
public default void foo(){
}
}
package Day12_21_00;
import java.util.List;
public class FileStorage implements Storage
{
@Override
public void saveDate(List<ContactInfo> infor)
{
System.out.println("文件存储");
}
}
package Day12_21_00;
import java.util.List;
public class CloudSave implements Storage
{
@Override
public void saveDate(List<ContactInfo> infor)
{
System.out.println("云存储");
}
}