2017-03-23-过滤器-集合
Xplorist Lv6

2017-03-23-过滤器-集合

要求:将基本常用类的方法熟悉一下。

知识点1:

过滤器:

过滤器是一个接口,实现这个接口的类中自有其拦截的实现。

过滤实际就是一个判断,判断之后将结果返回,如果为true,则说明被筛选的对象满足了条件。将结果返回给调用的地方,调用里面再将这个对象放进容器或数组里面。

知识点2:

集合

集合的产生,因为使用数组存储对象必须先分配数组长度,也就是确定对象的个数,实际情况中很多时候我们不知道对象的个数到底是多少,但又必须使用一个东西来存储,使用数组必然会造成不便,于是java提供了一个集合的数据结构,用于存储个数不确定的对象。

集合与泛型:

集合中如果不限定泛型的话,一般集合中存储的类型为Object类型,

虽然Object可以存储各种类型,但是通过向上转型之后,

Object的对象无法访问到

重写equals()方法

重写toString()方法

重写hascode()方法

泛型:

知识点3:

迭代器

Iterator是一个接口,每个Collection中的

安全操作,锁住集合对象的元素,即使是集合对象本身都不能访问元素

实例1:

有一个类person,有两条属性name,age
 在第一个方法中定义一个person对象添加到集合里面
 输出集合
在另一个方法中 删除person对象

思路:

重写equals()方法

重写hascode()方法

通过名字来将对象删除,那么就需要将对象的匹配识别方式改成对象的name相同的方式,就需要改写equals方法,来实现比较两个对象相同通过对象的属性name相同来比较,

但是系统里面默认是识别hascode相同的对象是同一对象,所以就需要将重写hascode()方法。

重写hascode()方法的原因:

java设计原则:当两个对象相同时,他们的哈希值应该相同

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Person {
    private String name;
    private int 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;
    }
    
    public Person(){
        
    }
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    
    @Override
    public boolean equals(Object obj) {
        Person p=(Person)obj;
        return this.name.equals(p.name);
    }
    @Override
    public int hashCode() {
        return name.hashCode();
    }
    
}

public class Main {
    public static void main(String[] args) {
        Collection<Person> c=new ArrayList<Person>();
        
        test1(c);
        test2(c);
        show(c);
    }
    
    public static void test1(Collection<Person> c){
        Person p1=new Person("张三",14);
        c.add(p1);
        for(Person p:c){
            System.out.println(p);
        }
    }
    
    public static void test2(Collection<Person> c){
        Person p=new Person("张三",14);
        boolean bool=c.remove(p);
        System.out.println(bool);
    }
    public static void show(Collection<Person> c){
        System.out.println("1111");
        for(Person p:c){
            System.out.println(p);
        }
    }
}

实例2:

将一个原文件夹里的所有内容复制到另一个地址里面

思路:

通过递归找到原文件夹中的每个文件和其子文件夹,在找到后将文件或文件名创建出来。

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void test() {
        File oldFile = new File("C:\\Users\\Administrator\\Desktop\\养殖场");
        File file = new File("d:");
        find(oldFile,file);
    }

    public static void find(File oldFile, File file) {
        if (oldFile.isDirectory()) {
            File[] fileArray = oldFile.listFiles();
            for (File f : fileArray) {
                File file1 = new File(file, oldFile.getName());
                file1.mkdirs();
                 find(f,file1);
            }
        }else{
            File file1 = new File(file, oldFile.getName());
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
 评论