集合体系结构-2

集合

HashSet(无序)
  • 不同对象的哈希值默认不同,就算存储的数据一样,HashSet也无法直接保证唯一性,需要重写两个方法 ,保证对象内部存储内容一致时对象的哈希值相同,从而通过HashSet保证元素唯一性。
LinkedHashSet
  • 哈希表和链表实现的set接口,具有可预测的迭代性。
  • 由链表保证元素有序,也就是说元素的存储和取出顺序是一致的。
  • 由哈希表保证元素唯一,也就是说没有重复的元素。
TreeSet集合(排序)
  • 元素有序,顺序不是指存储和取出的顺序,而是按照一定的规则排序,具体排序方式取决于构造方法。
  • 没有带索引的方法不能用普通for循环遍历。
  • 由于是Set集合,不包含重复的元素。

自然排序(Comparable)
  • 创建TreeSet集合使用无参构造方法
  • 需要让元素所属的类实现Comparaable接口,重写CompareTo(To)方法。
  • 重写方法时,一定要按照排序的主要条件和次要条件来写。

需要重写比较方法


    @Override
    public int compareTo(Student s) {
//      return 1; //返回正数 s2在s1后面,s3在s2后面
//      return 0; //返回0,以为是重复元素,不添加
//      return -1; //返回负数,按照倒序排列
        //按照年龄从小到大排序
        int num = this.age - s.age; //想要升序 this放在前面
        int num2 = num == 0 ? this.name.compareTo(s.name) : num;
        return num2;


比较器排序Comparator的使用

-创建TreeSet集合使用带参构造方法

  • 带参构造方法使用的是比较器排序对元素进行排序的
  • 比较器排序就是让集合构造方法接收Comparator的实现类对象,重写compare(To 1,To2)方法
  • 重写方法一定要注意排序规则必须按照要求的主要条件和次要条件来写。

        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //this.age - s.age
                //s1,s2
                //在内部类里面 这个方法里的this代表TreeSetDemo02  不代表学生
                int num = s1.getAge() - s2.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });
        

在类A的内部定义一个类B,类B就被称为内部类。
不能直接访问内部类,可以通过
外部类名.内部类名 对象 = 外部类对象.内部类对象

Outer.Inner oi = new Outer().new Inner(); 

TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getSum() - s2.getSum(); //总成绩按照升序排列
                int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
                int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
                int num4 =num3==0? s1.getName().compareTo(s2.getName()):num3;//如果其他都重复且名字重复,则不添加该元素
                return num4;
            }
        });

        Student s1 = new Student("赵云", 98, 88);
        Student s2 = new Student("赵", 78, 88);
        Student s3 = new Student("云", 78, 98);
        Student s4 = new Student("赵大云", 18, 88);
        Student s5 = new Student("赵云", 18, 88);
        Student s6 = new Student("赵云", 88, 88);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);

        for(Student s : ts){
            System.out.println(s.getName()+" "+s.getSum()+" "+s.getChinese()+" "+s.getMath());
        }
        
        赵云 106 18 88
		赵大云 106 18 88166 78 88176 78 98
		赵云 176 88 88
		赵云 186 98 88


泛型
  • 将类型由原来的具体类型参数化,然后在使用/调用时传入具体的的类型。
    这种参数类型可以用在类、方法和接口中,分别称为泛型类、泛型方法、泛型接口。
  • 把运行时期的问题提前到了编译期间,避免了强制类型转换。
//泛型 String
Collection<String> c = new ArrayList<String>(); //在中括号中填入参数类型

泛型类定义格式

格式:修饰符 class 类名<类型>{}
可以传入什么类型


泛型类的定义

		
public class Generic<T> {
    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}


测试类

		//写入什么类型就可以定义什么类型数据
        Generic<String> g1 = new Generic<String>(); 
        g1.setT("666");
        System.out.println(g1.getT());

        Generic<Integer> g2 = new Generic<Integer>();
        g2.setT(45);
        Integer tt = g2.getT();
        System.out.println(tt);
泛型方法

格式:修饰符<类型> 返回值类型 方法名(类型 变量名){}
范例:public void show(T t){}

public class GenericMethod {

    public <T> void show(T t){
        System.out.println(t);
    }
}
泛型接口

接口

public interface Generic<T> {
    void show(T t);
}



实现接口的类


public class GenericImpl<T> implements Generic<T> {
    @Override
    public void show(T t) {
        System.out.println(t);
    }
}

测试类

public class GenericDemo {

    public static void main(String[] args) {
        Generic<String> g1 = new GenericImpl<String>();
        g1.show("string");

        Generic<Integer> g2 = new GenericImpl<Integer>();
        g2.show(66);
    }
}

类型通配符
  • 类型通配符:<?>
  • List<?>:表示元素类型未知的List,它的元素可以匹配任何的类型
  • 这种带通配符的List仅表示它是各种泛型List的父类,不能在其中添加元素
  • 类型通配符上限:<? extends 类型>
  • List <? extends Number>:它表示的类型是Number或者其子类型
  • 类型通配符下限:<? super 类型>
  • List <? super Number>:它表示的类型是Number或者其父类型
可变参数

格式:修饰符 返回值类型 方法名(数据类型…变量名){}
范例:public static int sum(int… a){}

  • 如果一个方法有多个参数,包含可变参数,可变参数要放在最后
public class ArgsDemo {
    
    public static void main(String[] args) {
        System.out.println(sum(10, 20, 30));
        System.out.println(sum(10, 20, 30, 40, 50));
    }
    
    public static int sum(int... a) {
        int sum = 0;
        for (int i : a) {
            sum += i;
        }
        return sum;
    }

}

可变参数的使用:


        //返回由指定数组指定大小的列表,不能添加或删除元素  类似于python中的元组
         List<String> list1 = Arrays.asList("Hello","world","java");

Map集合
  • Interface Map<K,V> K:键的类型 V:值的类型
    将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值
    public static void main(String[] args) {

        Map<String, String> map = new HashMap<String, String>();
        map.put("d1","21");
        map.put("d2","21");
        map.put("d3","21");
        map.put("d3","15");//第二次就是修改元素
        System.out.println(map);

    }

Map集合的一些方法

        Set<String> sss = map.keySet();//获取到所有键的集合
        System.out.println(sss);
        Collection<String> values = map.values(); //获取到所有值的集合
        System.out.println(values);

		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值