(1)Map接口常用方法:
- Map c = new HashMap();//声明
- c.put(Object key,Object val)//以"键-值"方式存储
- c.get(Object key);//通过key获得val
- c.size();//返回元素个数
- c.containsKey(Object key);//判断键是否存在
- c.containsValue(Object val);//判断值是否存在
- c.remove(Object key);//删除由指定键映射的键值对
- system.out.println(c.coutries);//遍历
- c.clear();//清空
import java.util.HashMap;
import java.util.Map;
public class TestHashMap {
public static void main(String[] args) {
// 1.声明Map接口下的hash类
Map coutries = new HashMap();
// 2.添加 put(Object key键,Object value值)
coutries.put("CN", "中国");
coutries.put("RU", "俄罗斯联邦");
coutries.put("FR", "法兰西共和国");
coutries.put("US", "美利坚");
// 3.通过key键获得值
String country = (String) coutries.get("CN");
System.out.println(country);
// 4.显示集合中的个数
System.out.println("Map接口***有" + coutries.size() + "组数据");
// 5.1.判断键是否存在
boolean bo = coutries.containsKey("FR");
System.out.println(bo);
// 5.2.判断值是否存在
boolean bovalue = coutries.containsValue("中国");
System.out.println(bovalue);
// 6.删除集合对象
coutries.remove("US");
// 7.1遍历(分别显示键集,值集,键值对集合)
System.out.println(coutries.keySet());
System.out.println(coutries.values());
System.out.println(coutries);
// 7.2foreach遍历
for (Object obj : coutries.keySet()) {
String keyz = (String) obj;
System.out.println("键值集合:"+keyz);
}
for (Object obj : coutries.values()) {
String valuez =(String)obj;
System.out.println("value值集合:"+valuez);
}
//8.清空
coutries.clear();
System.out.println(coutries.isEmpty());
}
}
(2)Vector和ArrayList的异同:
实现原理、功能相同、可以互用
主要区别:
Vector线程安全,ArrayList重速度轻安全,线程非安全;
长度需增长时,Vector默认增长一倍,ArrayList增长50%;
Hashtable和HashMap的异同:
实现原理、功能相同、可以互用
主要区别:
Hashtable继承Dictionary类,HashMap实现Map接口;
Hashtable线程安全,HashMap线程非安全;
Hashtable不允许null值,HashMap允许null值
在开发过程中,最好使用ArrayList和HashMap
(3)iterator迭代器
- 获取iterator:Collection接口的iterate()方法;
- booleanhasNext():判断是否存在另一个可访问的元素;
- Object next():返回要访问的下一个元素;
两种凡是遍历Map集合:
1.迭代器;
2.foreach;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestIterator {
public static void main(String[] args) {
Dog dog1 = new Dog("花花", 100, 90, "拉布拉多");
Dog dog2 = new Dog("青青", 90, 70, "小哈吧");
Dog dog3 = new Dog("娜娜", 50, 40, "雪橇");
Dog dog4 = new Dog("妮妮", 40, 30, "松狮");
Dog dog5 = new Dog("晶晶", 40, 30, "松狮");
Map dogMap = new HashMap();
dogMap.put(dog1.getName(), dog1);
dogMap.put(dog2.getName(), dog2);
dogMap.put(dog3.getName(), dog3);
dogMap.put(dog4.getName(), dog4);
dogMap.put(dog5.getName(), dog5);
System.out.println("方法1:使用iterator");
// 方式1:迭代器方式
Set keys = dogMap.keySet();//取出所有key的集合
Iterator it = keys.iterator();//获得Iterator对象
while (it.hasNext()) {
String key = (String) it.next();//取出key
// System.out.println(key);
Dog dog = (Dog) dogMap.get(key);//根据key取出对应的值
dog.print();
}
System.out.println("方法2:使用foreach");
// 方法2:foreach
for (Object key : keys) {
Dog dog = (Dog) dogMap.get(key);
dog.print();
}
}
}
测试结果:
(4)泛型集合
即限定一定范围的集合.
- List泛型集合:
import java.util.ArrayList;
import java.util.List;
/** * List泛型集合 * * @Hudie */
public class TestListCollection {
public static void main(String[] args) {
Dog dog1 = new Dog("花花", 100, 90, "拉布拉多");
Dog dog2 = new Dog("青青", 90, 70, "小哈吧");
Dog dog3 = new Dog("娜娜", 50, 40, "雪橇");
Dog dog4 = new Dog("妮妮", 40, 30, "松狮");
Dog dog5 = new Dog("晶晶", 40, 30, "松狮");
List<Dog> lis = new ArrayList<Dog>();
lis.add(dog1);
lis.add(dog2);
lis.add(dog3);
(lis.get(0)).print();// 不需要类型转换;
lis.remove(0);
// foreach也不需要类型转换了
for (Dog dog : lis) {
dog.print();
}
}
}
- Map泛型集合:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestMapList {
public static void main(String[] args) {
Dog dog1 = new Dog("花花", 100, 90, "拉布拉多");
Dog dog2 = new Dog("青青", 90, 70, "小哈吧");
Dog dog3 = new Dog("娜娜", 50, 40, "雪橇");
Dog dog4 = new Dog("妮妮", 40, 30, "松狮");
Dog dog5 = new Dog("晶晶", 40, 30, "松狮");
Map<String, Dog> dogMap = new HashMap<String, Dog>();
dogMap.put(dog1.getName(), dog1);
dogMap.put(dog2.getName(), dog2);
dogMap.put(dog3.getName(), dog3);
dogMap.put(dog4.getName(), dog4);
Set<String> keys = dogMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
dogMap.get(key).print();
}
for (String string : keys) {
dogMap.get(string).print();
}
}
}
测试结果: