java集合

目录

collection (单列) 

list

arraylist

LinkedList

Vector

set

HashSet

TreeSet

Map(双列)

HashMap

TreeMap

HashTable

collections

collection (单列) 

集合只用于引用数据类型
Collection<Integer> c = new Arraylist<>();
创建集合时,默认是object类型,但是一般都需要明确指出数据类型
常用的方法
add(),clear(),isEmpty(),contains(),remove(),addAll(),containsAll()
removeAll()删除指定集合中包含的所有此集合的元素,有变化-true
retainAll()保留相同的元素,有变化-true
集合转数组           
object类型的
Collection<integer> c = new ArrayList();
c.add(12);
c.add(10);
c.add(11);
System.out.println(c);  结果是------->[12,10,11]
Object [] object = c.toArray();
System.out.printf(object.length);结果是------->3
System.out.println(Arrays.toString(object));结果是------->[12,10,11]
整数类型的
Collection<integer> c = new ArrayList();
c.add(12);
c.add(10);
c.add(11);
Integer [] array = c.toArray(new Integer[c.size()]);
System.out.printf(Arrays.toString(array));  结果是-------> [12,10,11]
System.out.printf(Arrays.sort(array)); -------> 这个格式是错误的,sort只能单独写出来
Arrays.sort(array);
System.out.printf(Arrays.toString(array));结果是------->[10,11,12]

list

有序的(按照插入元素的顺序排序),存在重复元素,值插入后有索引,通过索引访问元素

arraylist

底层是数组,查询快,中间增删慢

arraylist的创建时的长度问题
ArrayList<Integer> list =  new ArrayList<>();
arraylist默认不创建底层数组,当添加一个元素时,数组长度为10  ------->  new arraylist();
arraylist创建对象时,创建一个指定对象的数组   ------->new arraylist(int length);
实现了collection接口的子类,构造成一个arraylist     ------->new arraylist(Collection collection);

arraylist中最常用的方法
    list.add(E e);   向列表的末尾添加元素
    list.add(index,E e);  向指定位置添加元素
    add底层数组扩容,当数组满时,扩为原来的1.5倍
    get(int index) 获取指定位置的元素,并且返回被指定的元素
    remove(Object o)删除指定元素,只删除第一个存在的
    remove(int index)删除指定位置的元素,并且返回被删除的元素
    (*****)removeIf(Predicate<? super Integer> filter)条件删除
    由于考虑到只用一次,new一个匿名内部类
    ArrayList<Integer> list =  new ArrayList<>();
    list.add("e");
    list.add("e");
    list.add("e");
    list.add("e");
    list.add("e");
    list.add("e");
    list.removeIf(new Predicate<String>(){
    public boolean test(String t){
    return t.equals("e");    ------->删除集合中满足e元素的    
}
});
    (*****)removeRange(int fromindex,int toindex) 该方法需要在AyyayList子类中才能使用
    
    set(int index,E element),替换指定位置的值

    ArrayList<Integer> list =  new ArrayList<>();
    list.add("e");    
    list.add("e");
    list.add("e");
    list.add("e");
    list.add("e");
    list.add("e");
    list.sort(Comparator<? super E> c)定制排序    科普:Comparable 自然排序
    list.sort(new Comparator<String>(){
    public int compare(String 01,String 02){
    return 01.compareTo(02); 
    为什么用compareTo?
    答:list.add("e");------->因为其中的“e”是字符串,不能使用相减
    又因为返回值类型是int 所以也不能用equals,所以只能用compareTo
}
});
    subList(int fromindex,int toindex)从原集合中复制一段返回新集合,原集合不变

LinkedList

底层是双向链表,查询慢,中间增加,删除快
创建时,创建一个空的列表:new LinkedList<>(); 
实现了collection接口的子类,构造成一个Linkedlist    ------->new LinkedList(Collection collection);
LinkedList常用的方法:
    get(int Index)区别于ArrayList的get方法,是底层实现问题,查询慢,传入一个索引,如果索引小于集合的一半,从头结点开始查找
    直到找到这个位置的值,如果大于索引的一半,从尾结点开始查找,直到找到这个位置的值

Vector

底层是数组,查询快,中间增删慢,线程是安全的

(*****)list接口的迭代器遍历
Arraylist(迭代器遍历)
for循环遍历
用get(int Index),循环时允许删除元素,但要注意索引与集合的长度关系

增强for循环遍历
格式:for(Integer item: list){
      System.out.printf(item);
}
循环遍历时,不允许删除元素

迭代器遍历
现在的循环交给迭代器循环了,不是list,相当于,
把集合中的数据备份到迭代器中
Iterator<Integer> item = list.iterator();
    while(item.hasNext()){
    Integer it = item.next();
    if(it==1){
    item.remove();
}

System.out.println(list);
Iterator()和ListIteratord()的区别是     ListIterator()只能用于List接口的实现类,Iterator()遍历list,set
Iterator是从前向后遍历,可以删除元素
listiterator可以从任意方向遍历,需要加索引,用listiterator(int index)这个方法,有增加元素(前提是必须获取前一个元素next()),remove删除元素,set调换元素
add(E e),hasNext(),hasPrevious(),next(),nextIndex(),previous(),previousIndex(),remove(),set(E e);

Stream遍历流
第一种遍历循环 
Stream<Integer> s =  list.stream();
s.forEach(new Consumer<Integer>(){
    public void accept(Integer t){
    System.out.println(t);
}
});
第二种遍历循环
     Stream<Integer> s = list.stream();
    s.forEach(t->System.out.println(t));

set

不可以重复,值没有索引可以存储一个元素为空的值(null)

HashSet

无序 底层使用哈希表+链表+红黑树   

hashset添加时如何判断值是否重复
添加时会调用hashcode(),equals()
添加时要比较内容是否相等,既要保证效率,又要保证安全
先调用hashcoed()计算出一个哈希值,比较哈希值非常快,但是不安全
当哈希值相同时,再调用equals()方法比较 
例子:有一个学生的类型,即student类中没有重写hashcode()和equals(),会调用object中的方法
String(字符串).Integer....这些类重写了hashcode()都是根据对象中包含的内容来计算哈希值

TreeSet

有序(按照值(编码)的顺序排序) 底层是红黑树

TreeSet实现自定义类的时候,需要实现comparable接口,重写compareTo
treeset如何避免元素重复
treeset向里面加元素,需要比较一下(comparable),排序一下,以及去重

(*****)set接口的迭代器遍历
增强for循环遍历
Iterator()
Stream流遍历

Map(双列)

双列存储 key(键不能重复) value(可以重复)
       clear()
       remove(Object key) 
       replace(K key, V value) 
       put(key,value)
       putAll(Map<? extends K,? extends V> m) 
       containsKey(Object key) 
       containsValue(Object value) 
       get(Object key) 
       values() 
       size() 
       isEmpty() 
       
       keySet() 
       entrySet() 
       forEach(BiConsumer<? super K,? super V> action) 


HashMap

key不能重复,顺序不固定(因为用哈希值计算出元素在哈希表中的位置是不固定的),可以存一个null值
        底层存储结构
    哈希表+链表+红黑树
put方法实现
用key算出哈希值,用哈希值计算元素在哈希表中的位置,将元素添加到对应的位置
当有重复位置的元素加进来时,以链表的形式存储(避免哈希冲突的解决方法,拉链法)

jdk8以后,做了优化,当链表长度为8时,自动转为红黑树    

哈希表默认初始长度是16
负载因子0.75

每次触发扩容机制时 ,扩容为原来的2倍    


TreeMap

key不能重复,按照键的顺序排序, key值所在的类必须实现Comparable接口

HashTable(线程安全) Stringbuffer,Vector
实现了同步
不能存储一个null值
底层也是哈希表+链表(红黑树)实现 


/*Map遍历方式1:keySet()*/ 先拿到key然后找到value
        Set<String> strings = map.keySet();
        for (String key : strings) {
            System.out.println(key + "::" + map.get(key));

        }

 /*方式2 entrySet()将map中底层存储键值的Entry对象,封装到一个Set集合中 */
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey()+"::"+entry.getValue());

        }

 /*方式3 */
        map.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String k, String v) {
                System.out.println(k+"::"+v);

            }
        });

collections

int...n 可变长度的参数,本质是一个数组,一个参数列表只能有一个,必须放在参数列表的最后一个

1.addAll(Collection<? super T> c, T... elements) 

 ArrayList<String> list = new ArrayList<>();
        list.add("a");
        Collections.addAll(list, "b", "c", "d", "e", "f"); //向指定集合添加元素
        System.out.println(list);

2.sort(List<T> list)

 ArrayList<String> list = new ArrayList<>();
        list.add("a");
        Collections.addAll(list, "c", "f", "d", "e");
        Collections.sort(list);
        int index = Collections.binarySearch(list, "d");
        System.out.println(index);
        System.out.println(list);

3.sort(List<T> list, Comparator<? super T> c) 这个用于自定义类的排序,比如学生的学号进行排序

截图

4.swap(List<?> list, int i, int j)
ArrayList<String> list = new ArrayList<>();
        list.add("a");
        Collections.addAll(list, "c", "f", "d", "e");
        Collections.swap(list, 0, 3);
        System.out.println(list);

5.copy(List<? super T> dest, List<? extends T> src)

 ArrayList<String> list = new ArrayList<>();
        list.add("a");
        Collections.addAll(list, "c", "f", "d", "e");
        ArrayList<String> list1 = new ArrayList<>(10);
            list1.add("y");
            list1.add("y");
            list1.add("y");
            list1.add("y");
            list1.add("y");
        Collections.copy(list1,list);
        System.out.println(list1);

6.emptyList()//返回一个空集合,不能使用
System.out.println(Collections.emptyList());

7.fill(List<? super T> list, T obj) //用指定值填充列表中所有的元素
Collections.fill(list1,"b");
System.out.println(list1);

8.max(Collection<? extends T> coll)
System.out.println(Collections.max(list));

9.min(Collection<? extends T> coll)
System.out.println(Collections.min(list));

10.replaceAll(List<T> list, T oldVal, T newVal) //将列表中的某一特定值的所有出现替换为另一个。
     Collections.fill(list1, "b");
        Collections.replaceAll(list1, "b", "a");
        System.out.println(list1);

11.reverse(List<?> list) //反向列表中的元素
 Collections.reverse(list);
 System.out.println(list);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值