对Collection接口和Map接口的认识

本文深入介绍了Java集合框架的基础概念及核心实现,包括Collection接口、List和Set的不同应用场景,以及Map接口的特点与用法。通过具体实例展示了ArrayList、LinkedList、HashSet、TreeSet等不同集合类型的特性与操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Collection接口

Collection是最基本的集合接口,定义了一组允许重复或者不重复的对象,分为两大类Set和List。List接口允许元素重复,有序的而Set接口不允许重复也无序的。List接口又分为ArrayList和LinkedList;而Set又分为HashSet和TreeSet。




ArrayList的功能:

//创建其对象

ArrayList<String> fruit=new ArrayList<String>();

//添加元素

fruit.add("香蕉");

fruit.add("abc");

//获取链表长度

int size=fruit.size();

//访问内容(一一遍历各个元素)

for(int i = 0 ;  i < size; i++){

String s=fruit.get(i);

System.out.println(s);

}

//移除内容

使用remove(int index)表示按下表移除

String removeItem=fruit.remove(2);

使用remove(Object obj)表示按元素内容移除(首次出现的)

fruit.remove("香蕉");

//修改内容

fruit.set(3,"梨子");

//检测是否包含

boolean isHas=fruits.contains("葡萄");

//检测是否为空

boolean isEmpt=x.isEmpty();

//清空所有的内容

x.clear();  

//将链表转为数字

Integer[ ]  ary = new  Integer[x.size()] ;

x.toArray(ary);

//将数组转为链表

List<Integer> l=Arrays.asList(ary);



####    LinkedList
链式集合,跟ArrayList不一样,它基于链(头尾相连),特点增删速度快,查询速度慢。ArrayList查询快,增删慢。

    LinkedList<String> list = new LinkedList<String>();
    //添加到末尾
    list.add("苹果");
    // 添加到头部
    list.addFirst("香蕉");
    // 添加到尾部
    list.addLast("梨子");

    // 移除头部元素
    String str = list.removeFirst();

    removeLast(); 移除尾部元素

    //读取头部元素,但不移除
    String s2 = list.peek();

####    Set接口
不允许重复,无序的

#####    HashSet
允许有null元素,但是只有一个
    
    HashSet<String> hs = new HashSet<String>();
    //添加内容
    hs.add("abc");
    hs.add("123");

    //移除内容
    hs.remove("abc");

#####    TreeSet
有自己的排序方式(红黑树结构排列),元素不允许为null

    TreeSet<String> ts = new TreeSet<String>();
    ts.add("abc");
    ts.add("123");

    //获取大小
    int size = ts.size();

#####    Iterator接口(迭代器)
可以对collection进行迭代

对Set迭代
    
    //从集合中产生迭代器
    Iterator<String> it = ts.iterator();
    while(it.hasNext()){  //判断有没有下一个节点
        String s = it.next(); //读取下一个节点(指针向后移动)
        System.out.println(s);
    }

####    Map接口
Map接口用于维护键-值对的集合

特点:不允许重复的键

#####    HashMap
基于哈希表(散列表)

    //尖括号中第一个类型描述的键的类型,第一个是值的类型
    HashMap<Character,String> hm = new HashMap<Character, String>();
    //添加内容
    hm.put('a', "苹果");
    hm.put('b', "香蕉");
    hm.put('c', "香蕉");

    //获取内容
    String str = hm.get('a');

    //检测某个键是否存在
    boolean isHasKey = hm.containsKey('d');

遍历Map内容

    //先将Map转为Set<Entry<K,V>>
    Set<Entry<Character,String>> set = hm.entrySet();
    //从Set上获取迭代器
    Iterator<Entry<Character,String>> it = set.iterator();
    //迭代每个键值对对象
    while(it.hasNext()){
        Entry<Character,String> entry = it.next();
        //获取key
        char key = entry.getKey();
        //获取值
        String value = entry.getValue();
        System.out.println(key+":"+value);
    }


问题: 有一连串的字符串abcabacabcacb,问有几种不同的字母。数量分别是多少?

当有问题成对出现时需要想到Map接口

public static void main(String[] args) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
String str = "abcabacabcacb";
int len = str.length();

//通过循环来获取字符串中的每个字符
for (int i = 0; i < len; i++) {
char a = str.charAt(i);
System.out.print(a+" ");

//判断是否是首次出现,如果首次出现就添加(K,V ) 否则就V++
if (map.containsKey(a)) {
int num = map.get(a);
num++;
map.put(a, num);
} else {
map.put(a, 1);
}
}
System.out.println();
System.out.println("一共有"+map.size()+"个不同的字母");

//下面就是打印了,可以将他封装成一个方法
Set<Entry<Character, Integer>> set = map.entrySet();
// 从set上获取迭代器
Iterator<Entry<Character, Integer>> it = set.iterator();
while (it.hasNext()) {
Entry<Character, Integer> entry = it.next();
// 获取key
char key = entry.getKey();
// 获取value
int value = entry.getValue();
System.out.println(key + ":" + value);
}


}




























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值