onlyxue

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  31 随笔 :: 0 文章 :: 0 评论 :: 2391 阅读

Java基础

Java集合框架

泛型

  • 本质是参数化类型,把类型作为参数传递
  • 常见类型有泛型类、泛型 接口、泛型方法
  • 好处:提高代码的重用性、防止类型转换异常

​ 泛型类

/**
 * 泛型类
 * 语法:类型<T>
 *     T是一个占位符,表示一种引用类型。可以编写多个逗号隔开
 * @author xue
 */
public class GenericDemo<T> {
    //使用泛型T
    T t;

    //泛型作为方法的参数
    public void show(T t) {
        System.out.println(t);
    }

    //泛型作为方法的返回值
    public T getT() {
        return t;
    }
}

public class TestGeneric {
    
    public static void main(String[] args) {
        //使用泛型类创建对象
        GenericDemo<String> gd = new GenericDemo();
        gd.t = "test";
        gd.show("nihao");
        System.out.println(gd.getT());

        GenericDemo<Integer> gd2 = new GenericDemo<>();
        gd2.t = 23424;
        gd2.show(234256436);
        System.out.println(gd2.getT());
        
        //不同泛型对象不能相互复制
        //GenericDemo<String> gd3 = gd2;
    }
}

​ 泛型接口

/**
 * 泛型接口
 * 语法:接口名<T>
 * 不能泛型静态常量
 * @param <T>
 * @author xue
 */
public interface GenericDemo2<T> {
    String name = "zhangsan";

    T server(T t);
}


/**
 * 这里是先指定泛型类型
 */
public class GenericDemo2Imp implements GenericDemo2<String>{


    @Override
    public String server(String s) {
        System.out.println(s);
        return s;
    }
}

//使用泛型接口类GenericDemo2Imp
//使用时直接创建对象
        GenericDemo2Imp gd2i = new GenericDemo2Imp();
        gd2i.server("我被String固定了");


/**
 * 这里是不指定泛型类型
 */
public class GenericDemo2Imp2<T> implements GenericDemo2<T>{
    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}


//使用泛型接口类GenericDemo2Imp2
        GenericDemo2Imp2<Integer> gd2i2 = new GenericDemo2Imp2();
        gd2i2.server(123);
        GenericDemo2Imp2<String> gd2i3 = new GenericDemo2Imp2();
        gd2i3.server("我可以自己指定泛型了");

​ 泛型方法

/**
 * 泛型方法
 * 语法: <T> 返回值类型
 * @author xue
 */
public class FuncDemo {

    //泛型方法 可以是静态
    public <T> T show(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}

//泛型方法调用
        FuncDemo fd = new FuncDemo();
        //方法传入类型是什么 泛型类型就是什么
        fd.show("我是String类型");

泛型集合

  • 参数化类型、类型安全的集合,强制集合元素的类型必须一致。
  • 编译时即可检查是否异常
  • 访问时不必类型转换了
  • 不同反省之间引用不能相互赋值,泛型不存在多态
public class GenericDemo1 {
    public static void main(String[] args) {
        ArrayList<String> alist = new ArrayList<>();
        alist.add("只能");
        alist.add("添加");
        alist.add("字符串");

        for (String s:alist) {
            System.out.println(alist);
        }

        ArrayList<Student> alist2 = new ArrayList<>();
        Student stu1 = new Student("zhangsan",20);
        Student stu2 = new Student("lisi",23);
        Student stu3 = new Student("wangwu",45);
        alist2.add(stu1);
        alist2.add(stu2);
        alist2.add(stu3);

        Iterator<Student> it = alist2.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s);
        }
        
        //有了泛型之后可以发现不必强制转换类型了

    }
}

Set接口

  • 无序、无下标、元素不可重复

    /**
     *
     * Set接口的使用
     * 特点:
     *      没有下标
     *      无序
     *      元素不可重复
     * @author xue
     *
     */
    public class Demo1 {
        public static void main(String[] args) {
            //创建set接口 实现类有HashSet、TreeSet
            Set<String> set = new HashSet<>();
    
            //添加数据
            set.add("a");
            set.add("c");
            set.add("b");
            set.add("d");
            set.add("d");
            System.out.println("元素个数:"+set.size()); //4
            System.out.println(set.toString());
    
            //删除元素
            set.remove("b");
            System.out.println("元素个数:"+set.size()); //3
            System.out.println(set.toString());
    
            //遍历
            System.out.println("Iterator遍历");
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
                System.out.println(it.next());
            }
    
            System.out.println("foreach遍历");
            for (String str:set) {
                System.out.println(str);
            }
    
            //判断
            System.out.println(set.contains("a"));
            System.out.println(set.isEmpty());
    
        }
    }
    

HashSet

  • 是基于HashCode计算元素存放的位置

  • 当存入元素的哈希值相同时,会条用equals进行确认,如果结果为true,则拒绝后者存入

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
    
    
    /**
     * HashSet集合的使用
     * 存储结构:哈希表(数组+链表+红黑树)
     * 存储过程:
     *          1、根据hashcode计算保存的位置,如果位置为空,则直接保存,如果不为空执行第二部
     *          2、执行equals方法,返回true,则认为是重复;否则形成链表
     *
     * @author xue
     */
    public class Demo2 {
        public static void main(String[] args) {
            //新建集合
            HashSet<Person> hashSet = new HashSet<>();
    
            Person p1 = new Person("adf",88);
            Person p2 = new Person("df",87);
            Person p3 = new Person("gsd",86);
            Person p4 = new Person("dfhkk",85);
    
            //添加元素
            hashSet.add(p1);
            hashSet.add(p2);
            hashSet.add(p3);
            hashSet.add(p4);
            hashSet.add(new Person("dfhkk",85));
            System.out.println("元素个数:"+hashSet.size());
            System.out.println(hashSet);
    
            //删除元素
            hashSet.remove(new Person("gsd",86));
            System.out.println("元素个数:"+hashSet.size());
            System.out.println(hashSet);
    
            //遍历
            System.out.println("Iterator遍历");
            Iterator<Person> it = hashSet.iterator();
            while (it.hasNext()){
                System.out.println(it.next());
            }
    
            System.out.println("foreach遍历");
            for (Person P:hashSet) {
                System.out.println(P);
            }
    
            //判断
            System.out.println(hashSet.contains("a"));
            System.out.println(hashSet.isEmpty());
    
        }
    

重写hashcode与equals方法后 remove新对象时 会直接判断对象的值 根据值判断

TreeSet

  • 基于排列顺序实现元素不重复

  • 实现了SortedSet接口,对集合元素自动排序

  • 元素对象的类型必须实现Comparable,指定排序规则

  • 通过CompareTo方法确定是否为重复元素

    @Override
    public int compareTo(Person o) {
        int re1 = this.getName().compareTo(o.getName());
        int re2 = this.getAge() - o.getAge();
    
        return re1==0?re1:re2;
    }
    
    /**
     * TreeSet的使用
     * 存储结构:红黑树
     * @author xue
     */
    public class Demo3 {
        public static void main(String[] args) {
            //创建集合
            TreeSet<Person> treeSet = new TreeSet<>();
    
            Person p1 = new Person("sdf",88);
            Person p2 = new Person("dfg",87);
            Person p3 = new Person("yudf",86);
            Person p4 = new Person("cvbh",85);
            //添加元素
            treeSet.add(p1);
            treeSet.add(p2);
            treeSet.add(p3);
            treeSet.add(p4);
    
            System.out.println("元素个数:"+treeSet.size());
            System.out.println(treeSet);
    
            //删除元素
            treeSet.remove(new Person("yudf",86));
            System.out.println("元素个数:"+treeSet.size());
            System.out.println(treeSet);
    
            //遍历
            System.out.println("Iterator遍历");
            Iterator<Person> it = treeSet.iterator();
            while (it.hasNext()){
                System.out.println(it.next());
            }
    
            System.out.println("foreach遍历");
            for (Person P:treeSet) {
                System.out.println(P);
            }
        }
    }
    
posted on   守望在路上  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示