Java集合框架有什么用

发布时间:2021-07-10 12:23:15 作者:小新
来源:亿速云 阅读:210

这篇文章主要介绍Java集合框架有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Java集合框架

集合

测试

/*
            1.添加 2.删除 3.遍历 4.判断
         */
        Collection col = new ArrayList();
        col.add("张三");
        col.add("李四");
        col.add("王五");
//        col.add("张三");
        System.out.println(col);
//        col.remove("张三");
//        System.out.println(col);
        for (Object o : col) {
            System.out.println(o);
        }
        System.out.println("------------------");
        Iterator it = col.iterator();
        while (it.hasNext()){
            String next = (String) it.next();
            System.out.println(next);
        }
        System.out.println(col.isEmpty());
        System.out.println(col.contains("张三"));

List接口

特点:有序、有下标、元素可以重复。

可以通过角标在指定位置添加查询元素。

 List list = new ArrayList();
        list.add("java");
        list.add("c++");
        list.add(1,"python");
        list.add(".net");
        System.out.println(list.size());
        System.out.println(list.toString());
        //1.for each遍历
        System.out.println("---------------");
        for (Object o : list) {
            System.out.println(o);
        }
        //2.迭代器遍历
        System.out.println("---------------");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //3.list迭代器遍历
        System.out.println("--------正序-------");
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()){
            System.out.println(listIterator.next());
        }
        //逆序前必须先进行正序遍历,让指针指向列表最后一个元素,才能开发遍历
        System.out.println("--------逆序-------");
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex() + ":" +listIterator.previous());
        }

添加数字等基本类型数据时,会进行自动装箱的操作。

删除数字元素需要通过下标来删除,或者将需要删除的数字转成object类或者该类型对应的包装类。

subList:返回一个子集合,含头不含尾。

List实现类

ArrayList

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!增长修改个数
        elementData[size++] = e;
        return true;
    }
private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        ensureExplicitCapacity(minCapacity);
    }
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

测试代码:

 ArrayList arrayList = new ArrayList();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",18);
        Student s3 = new Student("王五",18);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println(arrayList.toString());
        //删除元素(需要重写equals方法)
        arrayList.remove(new Student("李四",18));
        System.out.println(arrayList.size());
 public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

Vector

Vector vector = new Vector();
        vector.add("java");
        vector.add("python");
        vector.add(".net");
        System.out.println(vector.toString());
        //枚举器遍历
        Enumeration elements = vector.elements();
        while (elements.hasMoreElements()){
            System.out.println(elements.nextElement());
        }

LinkedList:

泛型:

泛型集合:参数化类型、类型安全的集合,强制集合元素的类型必须一致。

特点:

Set接口

特点:无序、无下标、元素不可重复

方法:全部继承自Collection中的方法。

Set实现类

HashSet

public HashSet(){
  map = new HashMap<>();
}

测试代码:

 HashSet<Student> set = new HashSet<>();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",18);
        Student s3 = new Student("王五",18);
        set.add(s1);
        set.add(s2);
        set.add(s3);
//        set.add(new Student("李四",18));
        System.out.println(set.size());
        System.out.println(set.toString());
//        set.remove(new Student("李四",18));
//        System.out.println(set.size());
//        System.out.println(set.toString());
        for (Student student : set) {
            System.out.println(student);
        }
        System.out.println("====================");
        Iterator<Student> iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }
public int hashCode() {
        return Objects.hash(name, age);
    }

hashcode重写方法中加入31的原因

1.31是一个质数,减少散列冲突

2.31提高执行效率

TreeSet

测试代码:使用TreeSet集合实现字符串按照长度进行排序

TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
             int n1 = o1.length() - o2.length();
             int n2 = o1.compareTo(o2);
             return n1==0?n2:n1;
         }
        treeSet.add("zhangSan");
        treeSet.add("wkf");
        treeSet.add("asd");
        treeSet.add("abc");
        treeSet.add("ljCv");
        treeSet.add("liSi");
        treeSet.add("wanG");
        System.out.println(treeSet.toString());
        System.out.println(treeSet.size());

Map接口

特点:

1.用于储存任意键值对(Key,Value)

2.键:无序、无下标、不允许重复

3.值:无序、无下标、允许重复

遍历:

测试代码:

Map<String, String> map = new HashMap<>();
        map.put("wkf","666");
        map.put("qwe","678");
        map.put("kfc","999");
        map.put("asd","694");
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            System.out.println(s + "=" + map.get(s));
        }
        System.out.println("===================");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey() +"=" + entry.getValue() );
        }

HashMap

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // hashMap初始容量大小16
static final int MAXIMUM_CAPACITY = 1 << 30;//hashMap的数组最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认加载因子
static final int TREEIFY_THRESHOLD = 8;//jdk1.8开始,当链表长度大于8时,调整成红黑树
static final int UNTREEIFY_THRESHOLD = 6;//jdk1.8开始,当链表长度小于6时,调整成链表
static final int MIN_TREEIFY_CAPACITY = 64;//jdk1.8开始,当链表长度大于8时,并且集合元素个数大于等于64时调整成红黑树
transient Node<K,V>[] table;//哈希表中的数组

总结:

Hashtable

TreeMap

Collections工具类

以上是“Java集合框架有什么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Java集合框架介绍
  2. javabean有什么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何实现Unity UI拖拽模型选择功能

下一篇:Selenium+PhantomJS+python如何实现爬虫功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》