java集合

1. 简介

JDK1.2 引入了 Java 集合框架,包含一组数据结构。与数组不同,这些数据结构的存储空间会随着元素添加动态增加。其中,一些支持添加重复元素另一些不支持,一些支持 null,一些能自动升序打印元素。

所有这些数据结构在 java.util 包里,包含了 Collection、List、Set、Map、SortedMap 接口。这些接口的实现类有 LinkedList、TreeSet、ArrayList、HashMap 等。除了这些数据结构,java.util 包还提供了 Date、GregorianCalender、StringTokenizer、Random 这样的工具类。
在这里插入图片描述

2.分类

HashSet:实现 Set 接口,不允许重复的元素,底层数据结构 hash table
LinkedHashSet:实现 Set 接口,不允许重复的元素,底层数据结构 hash table 与双链表
TreeSet:实现 NavigableSet 接口,不允许重复的元素,底层数据结构红黑树
ArrayList:实现 List 接口,允许重复元素,底层数据结构可变数组
LinkedList:实现 List 接口,允许重复元素,底层数据结构双链表
Vector:实现 List 接口,允许重复元素,底层数据结构可变数组
HashMap:实现 Map 接口,不允许重复的 key,底层数据结构 hash table
LinkedHashMap:实现 Map 接口,不允许重复的 key,底层数据结构 hash table 与双链表
HashTable:实现 Map 接口,不允许重复的 key,底层数据结构 hash table
TreeMap:实现 SortedMap 接口,不允许重复的 key,底层数据结构红黑树

1.常用的三个集合区别

在这里插入图片描述

3.List接口

在这里插入图片描述
3.1ArrayList的一些常用方法
在这里插入图片描述
红色的是list 的父接口Colleection的方法

1.ArrayList

package Collection;

import java.util.ArrayList;
import java.util.Iterator;

//新闻管理
public class AraayListDemo {
	public static void main(String[] args) {
		// 集合
		NewsTitle title1 = new NewsTitle(1, "北京终于晴天了", "admin");
		NewsTitle title2 = new NewsTitle(2, "上海终于晴天了", "admin");
		NewsTitle title3 = new NewsTitle(3, "天津终于晴天了", "admin");
		NewsTitle title4 = new NewsTitle(4, "石家庄终于晴天了", "admin");
		NewsTitle title5 = new NewsTitle(5, "石家庄终于晴天了", "admin");
		ArrayList list = new ArrayList();
		list.add(title1);
		list.add(title2);
		list.add(title3);
		list.add(title4);
		list.add(2, title5);
		list.remove(1);
		
		System.out.println("新闻标题的下总数:" + list.size());
		// 遍历 list,取出每条新闻的题目

		for (int i = 0; i < list.size(); i++) {
			NewsTitle title = (NewsTitle) list.get(i);

			System.out.println(title.getTitle());

		}
		System.out.println("*********************");
		// 增强for
		for (Object opj : list) {
			NewsTitle title = (NewsTitle) opj;
			System.out.println(title.getTitle());
		}
		System.out.println(list.contains(title1));

		System.out.println("*********************");
		// 给到迭代器
		Iterator itof = list.iterator();
		while (itof.hasNext()) {// hasNext迭代器的方法用来判断元素合时为空
			NewsTitle title = (NewsTitle) itof.next();
			System.out.println(title.getTitle());
		}
		System.out.println();

	}

}

2.LinkedList
在这里插入图片描述
LinkedList独有方法

package Collection;

import java.util.LinkedList;
import java.util.List;

public class LinkedlistDemo {
	public static void main(String[] args) {

		NewsTitle title1 = new NewsTitle(1, "北京终于晴天了 1", "admin");
		NewsTitle title2 = new NewsTitle(2, "上海终于晴天了 2", "admin");
		NewsTitle title3 = new NewsTitle(3, "天津终于晴天了 3", "admin");
		NewsTitle title4 = new NewsTitle(4, "石家庄终于晴天了 4", "admin");
		NewsTitle title5 = new NewsTitle(5, "石家庄终于晴天了 5", "admin");
		NewsTitle title6 = new NewsTitle(6, "石家庄终于晴天了 6", "admin");
		// 不能使用独有的方法如addLast addFirst。
		// List list=new LinkedList();

		LinkedList list = new LinkedList();
		list.add(title1);
		list.add(title2);
		list.addFirst(title5); //插到第一个
		list.add(title3);
		list.addLast(title4);// 程序从上往下addLast插入只是当前的最后
		list.add(title6);
		// list.remove(2);
		NewsTitle titlen = (NewsTitle) list.remove(2);
		System.out.println(titlen.getTitle());

		NewsTitle first = (NewsTitle) list.getFirst();
		System.out.println(first.getTitle());
		System.out.println(list.size());
		for (int i = 0; i < list.size(); i++) {
			NewsTitle title = (NewsTitle) list.get(i);
			System.out.println(title.getTitle());

		}

	}
}

4.Map接口

在这里插入图片描述
1.HashMap

package MapDemo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDeom2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, String> Collection = new HashMap();
		Collection.put("CN", "中华人民共和国");
		Collection.put("RU", "俄罗斯联邦");
		Collection.put("FR", "法国");

		// System.out.println(Collection.size());
		// String
		// collection=(String)Collection.get("CN");//输入的String类型的元素所以转String
		//
		// System.out.println(collection);
		//
		// 判断map中是否包含某个键
		// boolean fiag=Collection.containsKey("CN");
		// System.out.println("是否包含中国"+fiag);
		// 删除特定键对应的键值对
		// Collection.remove("RU");
		// System.out.println(Collection.size());

		// 分别显示Map中键集·值集·和键值对集
		// System.out.println(Collection.keySet());
		// System.out.println(Collection.values());
		// System.out.println(Collection);

		// 清空

		// Collection.clear();
		// if(Collection.isEmpty()){
		// System.out.println("数据已空");
		// }
		// System.out.println("*****************");

		// 分别获取map中的键和值
		// (1)先获取到每一个KEY,然后根据每个key拿到相应的value
		// 使用增强for和for
		Set<String> keys = Collection.keySet();// 拿到key到set里
		for (String obj : keys) {
			String key = (String) obj;// 拿键

			String value = (String) Collection.get(key);// 拿值
			System.out.println(key + "****" + value);

			System.out.println("*****************");
			// 方法二:使用Iterator迭代器拿到每个key
			Iterator itor = keys.iterator();
			while (itor.hasNext()) {
				String Key = (String) itor.next();
				String Value = (String) Collection.get(Key);
				System.out.println(Key + "********" + Value);

			}
			// System.out.println(Collection);
			// 方法三先拿到Map中的键值对,然后再每个键值对中分别取出键和值

			Set<Map.Entry<String, String>> mSet = Collection.entrySet();
			for (Map.Entry<String, String> o : mSet) {
				System.out.println(o.getKey() + "-" + o.getValue());

			}

		}

	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值