Java基础(十五)

目录

一.单例集合使用案例

1.1 使用List类型容器实现

1.2 使用Set类型容器实现

二.双例集合

2.1 Map接口

 2.1.1 HashMap容器类

2.1.2 TreeMap容器类


一.单例集合使用案例

要求:产生1~10之间的随机数,将不重复的随机数放到容器中

1.1 使用List类型容器实现

public class testRongqi {
    public static void main(String[] args) {
        List<Integer> list1=new ArrayList<>();
       while(true){
           int num=(int)(Math.random()*10+1);
           //保证元素不重复
           if (!list1.contains(num)){
               list1.add(num);
           }
           //结束循环
           if (list1.size()==10){
               break;
           }
       }
       //查看元素
        for (Integer i:list1){
            System.out.println(i);
        }

    }
}

1.2 使用Set类型容器实现

Set<Integer> set1=new HashSet<>();
        while (true){
            int num=(int)(Math.random()*10+1);
            //由于Set不允许有重复元素出现,所以不需要加判断
            set1.add(num);
            if (set1.size()==10){
                break;
            }
        }
        //查看元素
        for (Integer i:set1){
            System.out.println(i);
        }

二.双例集合

2.1 Map接口

双例集合的存储特征是以key与value结构为单位进行存储的。

Map与Collection的区别:

1.Collection中的容器,元素是孤立存在的,向集合中存储元素采用一个元素的方式存储。

2.Map中的元素,元素是成对存在的,没对元素都由键和值两部分组成,通过键可以找所对应的值。

3.Collection中的元素成为单例集合,Map中的元素称为双例集合。

4.Map中的元素不能包含重复的键,值可以重复,每个键只能对应一个值。

5.Map中常用的容器有HashMap,TreeMap等。

Map中常用的方法:

 2.1.1 HashMap容器类

HashMap是Map接口的接口实现类,在查找,删除,修改方面具有非常高的效率。

//实例化HashMap容器
        Map<Integer,String> map=new HashMap<>();
        //添加元素
        map.put(1,"b");
        String value=map.put(1,"a");
        System.out.println(value);//返回b,后一个值会把前一个值代替掉,然后返回被替换掉的值
        map.put(2,"b");
        map.put(3,"c");
        //获取元素
        //方式一:通过get方法获取元素
        String value1=map.get(1);
        System.out.println(value1);
        //方式二:用keySet方法获取元素
        //获取HashMap容器中所以的元素,可以使用keySet方法和get方法一并完成
        Set<Integer> keys=map.keySet();
        for (Integer k:keys){
            String value2=map.get(k);
            System.out.println(value2);

        }
        //方式三:通过entrySet方法获取Map.Entry类型获取元素
        Set<Map.Entry<Integer,String>> e=map.entrySet();
        for (Map.Entry<Integer,String> entry:e){
            Integer key=entry.getKey();
            String v=entry.getValue();
            System.out.println(key+"----"+v);
        }
        //Map容器的并集操作
        Map<Integer,String> map2=new HashMap<>();
        map2.put(4,"doudou");
        map2.putAll(map);
        Set<Integer> k=map2.keySet();
        for (Integer kv:k){
            String v=map2.get(kv);
            System.out.println(kv+"---"+v);
        }
        //删除元素
        map2.remove(1);
        Set<Integer> key=map2.keySet();
        for (Integer k2:key){
            String v=map2.get(k2);
            System.out.println(k2+"---"+v);
        }
        //判断key是否存在
        boolean f1=map2.containsKey(1);
        System.out.println(f1);//false
        boolean f2=map2.containsKey(2);
        System.out.println(f2);//true
        //判断value是否存在
        boolean f3=map2.containsValue("a");
        System.out.println(f3);//false
        boolean f4=map2.containsValue("doudou");
        System.out.println(f4);//true

2.1.2 TreeMap容器类

HashMap效率高于TreeMap,TreeMap是可以对键进行排序的一种容器。

在使用TreeMap时需要给定排序规则:

1.元素自身实现比较规则。(有equals和HashSet方法)

//元素自身实现比较规则
        //实例化TreeMap
        Map<Integer,User> umap=new TreeMap<>();
        User u1=new User("doudou",18);
        User u2=new User("weibin",18);
        umap.put(1,u1);
        umap.put(2,u2);
        Set<Integer> ukey=umap.keySet();
        for (Integer uk:ukey){
            User uv=umap.get(uk);
            System.out.println(uk+"---"+uv);
        }

public class User {
    private String name;
    private int age;
    public User(String name,int age){
        this.name=name;
        this.age=age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

2.通过比较器实现比较规则。

//通过比较器实现比较规则
       Map<Student,Integer> smap= new TreeMap<>(new StudentComparator());//Student不能是值
        Student s1=new Student("doudou",18,"dou");
        Student s2=new Student("weibin",18,"xiaoyang");
        smap.put(s1,1);
        smap.put(s2,2);
        Set<Student> skey=smap.keySet();
        for (Student sk:skey){
            Integer sv=smap.get(sk);
            System.out.println(sk+"---"+sv);
        }

Student类

public class Student {
    String name;
    int ages;
    String chuohao;
    public Student(String name,int ages,String chuohao){
        this.name=name;
        this.ages=ages;
        this.chuohao=chuohao;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAges(int ages) {
        this.ages = ages;
    }

    public int getAges() {
        return ages;
    }

    public void setChuohao(String chuohao) {
        this.chuohao = chuohao;
    }

    public String getChuohao() {
        return chuohao;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", ages=" + ages +
                ", chuohao='" + chuohao + '\'' +
                '}';
    }

}

建造比较器:

public class StudentComparator implements Comparator<Student> {
    //定义比较规则

    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getAges()>o2.getAges()){
            return 1;
        }
        if (o1.getAges()== o2.getAges()){
            return o1.getName().compareTo(o2.getName());
        }
        return -1;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值