Java中的集合分类及常用方法


集合类特点:提供了一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

集合分为单列集合(collection集合),双列集合(map集合),单列集合包括set集合和list集合。
集合都是接口,并不能直接创建对象。需要知道它的实现类

collection是一个接口,JDK不提供此接口任何直接实现,它提供了更具体的子接口的实现,如set和list


一、Collection集合常用的方法

1、boolean add(E e);添加元素
c.add("hello");
2、boolean remove(Object o);从集合中移出指定元素
c.remove("java");
3、void clear();清空集合中元素
c.clear();
4、boolean contains(Object o);判断集合中是否存在指定元素
boolean b1 = c.contains("hello");
5、boolean isEmpty();判断集合是否为空
boolean b = c.isEmpty();
6、int size();集合的长度,也就是集合中元素的个数
int size = c.size();
7、集合的遍历

public class CollectionDemo {
        public static void main(String[] args) {
            //创建Collection集合对象
            Collection<String> c = new ArrayList<String>();
            Collection<String> s = new LinkedList<String>();
            LinkedList<String> lk = new LinkedList<>();

            //集合中常用的方法
            //1、boolean add(E e);添加元素
            c.add("hello");
            c.add("world");
            c.add("java");
            c.add("javaee");
            System.out.println(c);//[hello, world, java, javaee]

            //2、boolean remove(Object o);从集合中移出指定元素
            c.remove("java");
            System.out.println(c);//[hello, world, javaee]

            //3、void clear();清空集合中元素
           // c.clear();

            //4、boolean contains(Object o);判断集合中是否存在指定元素
            boolean b1 = c.contains("hello");
            System.out.println("contains(hello):"+b1);//contains(hello):true

            //5、boolean isEmpty();判断集合是否为空
            boolean b = c.isEmpty();

            //6、int size();集合的长度,也就是集合中元素的个数
            int size = c.size();
            System.out.println(size);

            // 集合的遍历
            Iterator<String> it = c.iterator();
            while(it.hasNext()){    //it.hasNext()判断是否还有元素
                String e = it.next();
                System.out.println(e);
            }

            // 集合的遍历,增强for循环
            for(String sc : c){
                   System.out.println(sc);
               }

        }
    }

一、list集合

list集合特点:
有序:存储和取出的元素顺序一致
可重复:存储的元素可以重复

List集合子类特点:
ArrayList底层是数组,查询快,增删慢
LinkedList底层是链表,查询慢,增删快

1、List集合特有的方法:
public class CollectionDemo {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            //添加元素
            list.add("java");
            list.add("javaee");
            list.add("hdfs");
            list.add(1,"world");
            list.remove(2);
            list.set(1,"hello");
            list.get(2);
    }
}
2、LinkedList集合特有的方法:
 List<Integer> i = new LinkedList<>();

二、Set集合

set集合中的方法与Collection中常用的方法一样

set集合是不包含重复元素的集合,没有带索引的方法,所以不能通过for循环访问其中的元素
set和list的区别有三点:(1)无序(2)无索引(3)不允许重复

List集合子类特点:
HashSet集合特点:

1、底层数据结构是哈希表
2、对集合的迭代顺序不做任何的保证,也就是不能保证存储和取出的元素的顺序一致
3、没有带索引的方法,所以不能使用普通的for循环遍历
4、由于是set集合,所以是不包含重复元素的集合
hashSet集合保证元素唯一性的源码分析不清楚

LinkedHashSet集合特点:

1、由哈希表和链表实现set接口,具有可预测的迭代次序
2、由链表保证元素有序,也就是说元素的存储和取出顺序是一样的
3、由哈希表保证元素唯一,也就是说没有重复的元素

TreeSet集合特点:

元素有序,这里的有序不是指存储和取出的顺序,而是按照一定的规则进行排序,具体排序方式取决于构造方法
TreeSet():根据其元素的自然排序进行排序。数字就从小到达排序
TreeSet(Comparator comparator):根据指定的比较器进行排序
没有带索引的方法,所以不能使用普通for循环遍历
由于是set集合,所以是不包含重复元素的集合

//创建集合对象
Set<String> set = new HashSet<String>();

三、Map集合

Map集合概述
interface Map<K,V>
Map集合是一个接口,它的泛型由两部分组成,K是键的类型,V是值的类型。它是将键映射到值的对象,不能包含重复的键,每个键可以映射到最多一个值

Map集合基本方法

public class MapDemo {
        public static void main(String[] args) {
            Map<String, String> s = new HashMap<String, String>();
            //put方法可以添加元素,也可以是修改元素
            s.put("heima001","linqignxia");//K 学号,V 姓名
            //put方法键第二次出现是修改元素
            s.put("heima001","刘备");
            s.put("heima002","zhangfei");
            System.out.println(s);//{heima001=刘备, heima002=zhangfei}

             //V get(Object key);根据键获取值
            String v = s.get("heima001");
            System.out.println(v);//刘备

            //Set<K> keySet();获取所有键的集合
            Set<String> keySet = s.keySet();
            System.out.println(keySet);//[heima001, heima002]

            //Collection<V> values();;获取所有值的集合
            Collection<String> values = s.values();
            System.out.println(values);//[刘备, zhangfei]
            }
        }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章