个人笔记(杂项)

1. HashSet与TreeSet区别:

1、TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值

2、HashSet 是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个null,两者中的值都不能重复,就如数据库中唯一约束

 2、Collections 排序方法的应用;

package com.imooc;
/*
 * description: add a description [描述信息]
 * @author  wu-weixin [作者]
 * @date 2022/07/10 04:47:07 [时间,这里是年/月/日 时:分:秒的格式]
 * @version 1.0.0 [版本信息]
 **/

import java.util.*;

public class ListSortor {
    public static void main(String[] args) {
        List emps=new ArrayList<>();
        emps.add(new Employee("小李",18,1200f));
        emps.add(new Employee("小张",19,1300f));
        emps.add(new Employee("小王",20,1400f));
        System.out.println(emps);
        Collections.sort(emps, new Comparator() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return o2.getAge()-o1.getAge();
            }
        });
        System.out.println(emps);
    }
}

3、遍历Set里面的元素

    public static void main(String[] args) {
        Set set=new TreeSet();
        set.add("a");
        set.add("b");
        System.out.println(set);
        Iterator itset=set.iterator();
        while (itset.hasNext()){
            System.out.println(itset.next());
        }
    }

4、对TreeSet进行排序:

package com.imooc;
/*
 * description: add a description [描述信息]
 * @author  wu-weixin [作者]
 * @date 2022/07/10 04:47:07 [时间,这里是年/月/日 时:分:秒的格式]
 * @version 1.0.0 [版本信息]
 **/

import java.util.*;

public class ListSortor {
    public static void main(String[] args) {
        Set emps=new TreeSet<>(new Comparator() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return (int) (o2.getSalary()-o1.getSalary());
            }
        });
        emps.add(new Employee("小李",18,1200f));
        emps.add(new Employee("小张",19,1300f));
        emps.add(new Employee("小王",20,1400f));
        System.out.println(emps);
    }
}

5、java中==和equals和hashCode的区别

如果两个不同对象的hashCode相同,这种现象称为hash冲突。简单来说就是hashCode相同但是equals不同的值。

java中==和equals和hashCode的区别

 个人笔记(杂项)_第1张图片

6、个人笔记(杂项)_第2张图片

7、 三元运算符

public class wwx {
    public static void main(String[] args) {
        int a= 1!=1?1:2;

        System.out.println(a);
    }

}

 8、求数组的最大值;

package com.imooc.servlet;
/*
 * description: 求数组的最大值;
 * @author  wu-weixin [作者]
 * @date 2022/07/11 14:29:43 [时间,这里是年/月/日 时:分:秒的格式]
 * @version 1.0.0 [版本信息]
 **/

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class wwx {
    public static void main(String[] args) {
        int arr[]={2,4,6,54,73,2,3,4,5};
        //按从小到大进行排序;
        Arrays.sort(arr);
        //输出最后一个元素;
        System.out.println(arr[arr.length-1]);
    }
}

9、遍历数组

package com.imooc.servlet;
/*
 * description: 遍历数组;
 * @author  wu-weixin [作者]
 * @date 2022/07/11 14:29:43 [时间,这里是年/月/日 时:分:秒的格式]
 * @version 1.0.0 [版本信息]
 **/


import java.util.Arrays;

public class wwx {
    public static void main(String[] args) {
        int[] arr={2,3,43,5,5};
        System.out.println(Arrays.toString(arr) );
    }

}

10、获取String中的某个字符(返回指定索引处的char值)

package com.imooc.servlet;
public class wwx {
    public static void main(String[] args) {

        String w="hello";
        System.out.println(w.charAt(3));
        }
}

11、使用char时,记得用单引号

12、继承

在子类方法中访问一个变量,采用的是就近原则。(不考虑父亲的父亲…)

注意:子类中所有的构造方法默认都会访问父类中无参的构造方法

子类会继承父类中的数据,可能还会使用父类的数据。所以,子类初始化之前,一定要先完成父类数据的初始化,原因在于,每一个子类构造方法的第一条语句默认都是:super()

  • 方法重写的注意事项

  1. 私有方法不能被重写(父类私有成员子类是不能继承的)

  2. 子类方法访问权限不能更低(public > 默认 > 私有)

Java中类只支持单继承,不支持多继承

使用不同包下的类时,使用的时候要写类的全路径,写起来太麻烦了。为了简化带包的操作,Java就提供了导包的功能(import,类似于导入java包)

总结成一句话就是:静态成员方法只能访问静态成员

静态的成员变量可以通过类名调用,当然也可以通过对象名调用【推荐使用类名调用】

  • fianl修饰基本数据类型变量

    • final 修饰指的是基本类型的数据值不能发生改变

  • final修饰引用数据类型变量

    • final 修饰指的是引用类型的地址值不能发生改变,但是地址里面的内容是可以发生改变的

13、super() 父类的无参构造方法;

14、基本数据类型int不能等于null,必须用Integer,其它类似

你可能感兴趣的:(大数据)