Map
Map通过键值对存储
键不能重复,值可以重复
HashMap
public class HashMapDemo {
/**
* HashMap 键值对
* 键是无序的,如果有重读的键后面的值会覆盖上一个键的值
* 可以存储一个为null的键
*/
public static void main(String[] args) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("1","a");
hashMap.put("2","b");
hashMap.put("1","c");
hashMap.put("3","d");
hashMap.put(null,"d");
System.out.println(hashMap);
hashMap.remove("2");//移除key为"2"的元素
hashMap.remove("c");
System.out.println(hashMap);
//hashMap.clear();
System.out.println(hashMap.containsKey("2"));//是否包含Key值"2"
System.out.println(hashMap.containsValue("d"));//是否包含值"d"
System.out.println(hashMap);
System.out.println(hashMap.isEmpty());//是否为空
System.out.println(hashMap.size());//获取长度
}
}
TreeMap
public static void main(String[] args) {
/**
* TreeMap 键值对
* 键不重复
* 可根据键的自然顺序排序,指定的键的类型的类必须事先Comparede接口,在排序时使用
*/
TreeMap<String,String> treeMap = new TreeMap<>();
treeMap.put("1","a");
treeMap.put("2","b");
treeMap.put("1","c");
treeMap.put("4","c");
treeMap.put("3","d");
System.out.println(treeMap);
}
运行结果:
{
1=c, 2=b, 3=d, 4=c}
HashTable
public static void main(String[] args) {
/**
* Hashtable 无序的
* 初始容量为11
* 线程安全,不允许有键为null的键
*/
Hashtable<String,String> hashtable = new Hashtable<>();
hashtable.put("a","1");
hashtable.put("b","2");
hashtable.put("c","3");
hashtable.put("d","4");
//hashtable.put(null,"4");
System.out.println(hashtable);
}
运行结果:
{
b=2, a=1, d=4, c=3}
Map集合的遍历
public static void main(String[] args) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("1","a");
hashMap.put("2","b");
hashMap.put("1","c");
hashMap.put("3","d");
/*
Map集合的遍历方法:
1.foreach 2.keySet(); 3.entrySet()
*/
//hashMap.forEach((k,v)-> System.out.println(k+":"+v));
/*
keySet();取hashMap中所有的键,存储到Set集合中
在对Set集合进行遍历,通过每次得到的Key去获取相对应的值
*/
/*Set<String> set = hashMap.keySet();
for (String key:set) {
System.out.println(key+":"+hashMap.get(key));
}*/
/*
values();获取hashMap中所有的值,存储到list集合中
*/
/*Collection list = hashMap.values();
System.out.println(list);*/
/*
entrySet();将hashMap中的键值对封装到一个entry对象中
可以通过entry获取key或者value
*/
Set<Map.Entry<String,String>> entrySet = hashMap.entrySet();
/*for (Map.Entry<String,String> entry: entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}*/
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while (it.hasNext()){
/*Map.Entry<String,String> entry=it.next();
System.out.println(entry.getKey()+":"+entry.getValue());*/
System.out.println(it.next());
}
}
Collection类
public static void main(String[] args) {
/*ArrayList arrayList = new ArrayList();
arrayList.add("x");
Collections.addAll(arrayList,"a","g","d");//多个元素进行添加
System.out.println(arrayList);
Collections.sort(arrayList);//对集合进行排序
System.out.println(arrayList);
System.out.println(Collections.binarySearch(arrayList,"d"));//二分查找
*/
ArrayList arrayList1 = new ArrayList();
Collections.addAll(arrayList1,"a","g","d","c");
ArrayList arrayList2 = new ArrayList();
Collections.addAll(arrayList2,"x","y","z");
Collections.copy(arrayList1,arrayList2);//要求目标集合的size大于sec源集合的size
System.out.println(arrayList1);
List list = Collections.emptyList();//返回一个空集合,无法进行add操作
//list.add("a"); java.lang.UnsupportedOperationException
System.out.println(list);
Collections.fill(arrayList2,"w");//用指定元素对目标集合进行覆盖
System.out.println(arrayList2);
System.out.println(Collections.max(arrayList1));//返回集合中最大元素
System.out.println(Collections.replaceAll(arrayList1,"x","X"));//对集合中的指定元素进行替换,替换成功返回true
System.out.println(arrayList1);
Collections.reverse(arrayList1);//对集合进行翻转
System.out.println(arrayList1);
Collections.swap(arrayList1,0,3);//对集合中的两个元素进行互换
System.out.println(arrayList1);
}