Set和Map学习

Set

在这里插入图片描述
入门案例
Set特点
无序(存储和读取的顺序由可能不一样)
不允许重复(要求元素唯一)
没有索引

/**
 * 使用HashSet存储字符串并遍历
 * Set集合的特点:
 *          无序(存储和读取的顺序由可能不一样)
 *          不允许重复(要求元素唯一)
 *          没有索引
 */
public class HashSetDemo {
    public static void main(String[] args) {
        //创建集合对象
        //Set<String> set = new HashSet<String>();
        Set<String> set = new HashSet<String>();//父接口引用指向子类对象,多态
        //添加元素对象
        set.add("hello");
        set.add("world");
        set.add("java");
        //遍历集合对象
        //三种遍历方式
        //转数组
        System.out.println("-------------------转数组方式遍历-----------------------");
        HashSetDemo.method(set);
        System.out.println("-------------------增强for方式遍历-----------------------");
        HashSetDemo.method2(set);
        System.out.println("-------------------迭代器方式遍历-------------------");
        HashSetDemo.method3(set);

        //迭代器

        //增强for
    }
    //遍历集合对象
    private static void method(Set<String> set) {
        Object[] array = set.toArray();
        for (Object o : array) {
            System.out.println(o);
        }
    }
    //迭代器方式遍历
    private static void method2(Set<String> set) {
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String next = it.next();
            System.out.println(next);
        }
    }

    //增强for
    private static void method3(Set<String> set) {
        for (String s : set) {
            System.out.println(s);
        }
    }
}

使用HashSet存储自定义对象并遍历
在这里插入图片描述

发现,HashSet没有去重
add方法
首先会使用当前集合中的每一个元素和新添加的元素进行hash值比较,
如果hash值不一样,则直接添加新的元素
如果hash值一样,则比较地址或者使用equals方法进行比较
比较结果一样,则认为是重复不添加
所有的比较结果都不一样则添加
add方法的值,是Map中put方法的key
在这里插入图片描述
可以重新equals方法和hashcode方法解决
在这里插入图片描述

Collections 工具类

面试题:Collections和Collection有什么区别
Collection是集合体系的最顶层,包含的集合体系的共性
Collections是一个工具类,方法都是用于操作Collection

/**
 * Collections:
 * 面试题:Collections和Collection有什么区别
 * Collection是集合体系的最顶层,包含的集合体系的共性
 * Collections是一个工具类,方法都是用于操作Collection
 */
public class CollectionsDemo {
    public static void main(String[] args) {
//        static <T> int binarySearch(List list, T key)使用二分查找查找指定元素在指定列表的索引位置
        //二分查找
//        method1();
//        method2();
//        static void fill(List list ,Object obj):使用指定的对象填充列表的所有元素
//        method3();
//        static reverse(List list)  :反转
//        method4();
        //sort安照列表中的自然顺序排序
//        sort安照列表中的自然顺序排序();
//        shuffle随机置换();
//        static void swap(List list ,int i,int j)将列表中的两个索引进行位置互换
//        swap将指定列表中的两个索引进行位置互换();
    }

    private static void swap将指定列表中的两个索引进行位置互换() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("3");
        strings.add("2");
        strings.add("4");
        Collections.swap(strings, 0, 3);
        System.out.println(strings);
    }

    private static void shuffle随机置换() {
        //shuffle()随机置换
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("3");
        strings.add("2");
        strings.add("4");
        Collections.shuffle(strings);
        System.out.println(strings);
    }

    private static void sort安照列表中的自然顺序排序() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("3");
        strings.add("2");
        strings.add("4");
        Collections.sort(strings);
        System.out.println(strings);
    }

    private static void method4() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        Collections.reverse(strings);
        System.out.println(strings);
    }

    private static void method3() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        Collections.fill(strings, "a");
        for (String string : strings) {
            System.out.println(string);
        }
    }

    private static void method2() {
        //static void copy(List dest,List src):是把源列表中的数据覆盖到目标列表
        //创建源列表
        ArrayList<String> src = new ArrayList<>();
        src.add("hello");
        src.add("world");
        src.add("java");
        //创建目标列表
        ArrayList<String> dest = new ArrayList<>();//目标列表长度必须大于或者大于源列表
        dest.add("hehe");
        dest.add("hehe");
        dest.add("hehe");
        dest.add("hehe");
        Collections.copy(dest, src);
        for (String s : dest) {
            System.out.println(s);
        }
    }

    private static void method1() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        int i = Collections.binarySearch(list, 3);//第二个参数为要找的值
        System.out.println(i);
    }
}

Map

* Map常用的功能:
 *      添加功能:
 *      V put(K key,V value)
 *      获取功能:
 *      V get(Object key)
 *      int size()
 *      判断功能:
 *      boolean containsKey(Object key)
 *      boolean containsValue(Object value)
 *      boolean isEmpty()
 *      删除功能:
 *      void clear()
 *      V remove(Object key)
 *      遍历功能:
 *      void clear()
 *      V remove(Object key)
 *      遍历功能:
 *      Set<Map.Entry<K,V>>   entrySet()
 *      Set<K> keySet()
 *      Collection<V> values()
 *
 */
public class MapDemo {
    public static void main(String[] args) {
        //创建Map对象
        Map<String, String> map = new HashMap<>();
        map.put("1", "张三");
        map.put("1", "张三");
        map.put("2", "李四");
//        map.put("1", "李四");//这样子会被覆盖掉,map的key不能重复
        Set<String> set = map.keySet();
        for (String s : set) {
            System.out.println(s+"\t\t\t"+map.get(s));
        }
        map.containsKey("0001");//判断指定key是否存在
        map.containsValue("张三");//判断Value值是否存在
//        第二种遍历
        System.out.println("-----------------------------------------------");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey()+"\t\t\t"+entry.getValue());
        }
        System.out.println("-----------------------------------------------");
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next().getKey();
            System.out.println(key+"\t\t"+map.get(key));
        }
    }

}


总结

set无序且值不能相同,map的key不能相同

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CV工程湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值