Java学习之路(十三)集合类

一、集合

简单粗暴点,我们来说说集合的应用场景:

  • 无法预测存储数据的数量。
  • 同时具有一对一关系的数据。
  • 需要进行数据的增删。
  • 数据重复问题。

接下来,我们来看一下集合的体系架构

e000xg.png

  • Collection(存储类的对象)
    • List(序列):有序,可重复
      • ArrayList(动态数组)
      • LinkedList(链表)
    • Queue(队列):有序,可重复
      • LinkedList(链表)
    • Set(集):无序,不可重复
      • HashSet(哈希集)
  • Map(存储键值对)
    • HashMap(哈希表)

二、List与Queue

  • 元素有序并且可以重复的集合;
  • 可以精确的控制每个元素的插入位置,或删除某个位置的元素;
  • 主要实现类是ArrayList和LinkedList。

(一)ArrayList

  • ArrayList底层是由数组实现的;
  • 动态增长,以满足应用程序的需求;
  • 更适合查找和更新数据。
public static void main(String[] args) {
    TestArrayList testArrayList = new TestArrayList();

    List<Integer> arrayList = new ArrayList<>();

    for (int i=1; i<20; i++) {
        // 添加元素
        arrayList.add(i);
    }

    testArrayList.getList1(arrayList);

    // 移除 
    arrayList.remove(3);
    testArrayList.getList2(arrayList);

    // 修改
    arrayList.set(0, 20);	
    // 获取
    System.out.println(arrayList.get(0));

    // 包含
    System.out.println(arrayList.contains(arrayList.get(3)));
    System.out.println(arrayList.contains(100));
}

// 遍历方法1:for循环遍历
public void getList1(List<Integer> arrayList) {
    for (int i: arrayList) {
        System.out.print(i + " ");
    }
    System.out.println();
}

// 遍历方法2:迭代器遍历
public void getList2(List<Integer> arrayList) {
    Iterator<Integer> iterable = arrayList.iterator();

    while (iterable.hasNext()) {
        System.out.print(iterable.next() + " ");
    }
    System.out.println();
}

(二)LinkedList

  • LinkedList底层使用双向链表实现;
  • 动态增长,以满足应用程序的需求;
  • 更适合插入和删除元素。
public static void main(String[] args) {
    TestLinkedList testLinkedList = new TestLinkedList();

    List<Integer> linkedList = new LinkedList<>();

    for (int i=1; i<20; i++) {
        // 添加元素,不推荐add,因为add方法在失败的时候会抛出异常
        ((LinkedList<Integer>) linkedList).offer(i);

    }

    testLinkedList.getList1(linkedList);

    // 移除元素,当删除第一个元素时,remove()和poll()均可,但是推荐使用poll(),理由同上
    linkedList.remove(3);
    testLinkedList.getList2(linkedList);

    // 修改
    linkedList.set(0, 20);	
    // 获取
    System.out.println(linkedList.get(0));

    // 包含
    System.out.println(linkedList.contains(linkedList.get(3)));
    System.out.println(linkedList.contains(100));

}

// 遍历方法1:for循环遍历
public void getList1(List<Integer> arrayList) {
    for (int i: arrayList) {
        System.out.print(i + " ");
    }
    System.out.println();
}

// 遍历方法2:迭代器遍历
public void getList2(List<Integer> arrayList) {
    Iterator<Integer> iterable = arrayList.iterator();

    while (iterable.hasNext()) {
        System.out.print(iterable.next() + " ");
    }
    System.out.println();
}

三、Set

  • 元素无序且不可重复的集合。

HashSet

  • HashSet是Set的一个重要实现类,称为哈希集
  • HashSet中的元素无序并且不可重复
  • HashSet中只允许一个null元素
  • 具有良好的存取和查找性能
public static void main(String[] args) {
    TestHashSet testHashSet = new TestHashSet();

    Cat cat = new Cat("老一", 2);
    Cat cat2 = new Cat("老二", 5);
    Cat cat3 = new Cat("老三", 5);

    Set<Cat> set = new HashSet<>();

    // 添加
    set.add(cat);
    set.add(cat2);
    System.out.println("添加元素:");
    testHashSet.getSet1(set);

    // 移除
    set.remove(cat);
    set.remove(cat3);
    System.out.println("移除元素:");
    testHashSet.getSet2(set);

    // 查找
    System.out.println("查找元素:");
    System.out.println(set.contains(cat));
    System.out.println(set.contains(cat2));
}

// 遍历方法1
public void getSet1(Set<Cat> set) {
    for (Cat cat: set) {
        System.out.println(cat.toString());
    }
}

// 遍历方法2:迭代器遍历
public void getSet2(Set<Cat> set) {
    Iterator<Cat> iterable = set.iterator();

    while (iterable.hasNext()) {
        System.out.print(iterable.next() + " ");
    }
    System.out.println();
}

四、Map

  • Map中的数据是以键值对(key-value)的形式进行存储
  • key-value以Entry类型的对象实例存在
  • 可以通过key快速地查找value
  • 一个映射不能包含重复的键
  • 每个key最多只能映射到一个值

HashMap

  • 基于哈希表的Map接口的实现
  • 允许使用null值和null键
  • key值不允许重复
  • HashMap中的Entry对象是无序排列的
public static void main(String[] args) {
    TestHashMap testHashMap = new TestHashMap();

    Map<String, String> map = new HashMap();

    String[] strings = {"狮子", "老虎", "猴"};
    String[] strings2 = {"Lion", "Tiger", "Monkey"};

    for (int i=0; i<strings.length; i++) {
        // 添加、修改
        map.put(strings[i], strings2[i]);
    }

    testHashMap.getMap1(map);
    testHashMap.getMap2(map);
    testHashMap.getMap3(map);
}

// 遍历方法1:for循环遍历
public void getMap1(Map<String, String> map) {
    for (String key: map.keySet()) {
        System.out.println(key + "-" + map.get(key));
    }
}

// 遍历方法2:迭代器遍历
public void getMap2(Map<String, String> map) {
    Iterator<String> iterableKey = map.keySet().iterator();		// 获取key
    Iterator<String> iterableValue = map.values().iterator();

    while (iterableKey.hasNext()) {
        System.out.println(iterableKey.next() + "-" + iterableValue.next());
    }
}

// 遍历方法3:entrySet方法遍历
public void getMap3(Map<String, String> map) {
    Set<Entry<String, String>> eSet = map.entrySet();

    for (Entry<String, String> sEntry:eSet) {
        System.out.println(sEntry.getKey() + "-" + sEntry.getValue());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值