Java——Map和Collections

Java进阶总结——Map和Collections

1.Map

 

1.1 HashMap

/**

 * HashMap

 * 1.无序的

 * 2.数据以键值对的方式存储

 * 3.可以允许有一个key键为空的成员

 * 4.key值唯一,value值不唯一

 * */

package day21.map;

 

import java.util.Collection;

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Set;

public class HashMapDemo {

public static void main(String[] args) {

//1.初始化一个HashMap对象

HashMap<Integer, Integer>map=new HashMap<Integer, Integer>();

//2.添加数据

map.put(0, 151);

map.put(1, 152);

map.put(3, 153);

map.put(2, 154);

map.put(null, 111);

map.put(4, 0);

System.out.println(map.toString());

//3.修改

map.put(null, 154);

System.out.println(map.toString());

//4.删除元素,通过key删除value

map.remove(null);

System.out.println(map.toString());

//5.查看成员

//通过key获取value

System.out.println(map.get(3));

//判断是否包含指定的key值

System.out.println(map.containsKey(null));

//判断是否包含value值

System.out.println(map.containsValue(152));

//查看键值对的个数

System.out.println(map.size());

//获取所有的key值

System.out.println("key值");

Set<Integer>keys=map.keySet();

for (Integer integer : keys) {

System.out.print(integer+"\t");

}

System.out.println();

System.out.println("value值");

//获取到所有的value值

Collection<Integer>values=map.values();

for (Integer integer : values) {

System.out.print(integer+"\t");

}

System.out.println();

//获取所有的映射关系,进行遍历map

Set<Entry<Integer, Integer>>en=map.entrySet();

for (Entry<Integer, Integer> entry : en) {

System.out.print(entry.getKey()+"--"+entry.getValue());

System.out.println();

}

}

}

输出结果:

1.2TreeMap

/**

 * TreeMap特点

 * 1.根据key进行自动排序

 * 2.key不允许为null

 * */

 

package day21.map;

 

import java.util.Map.Entry;

import java.util.Set;

import java.util.TreeMap;

 

public class TreeMapDemo {

 

public static void main(String[] args) {

//初始化一个TreeMap对象

TreeMap<Integer, String>map=new TreeMap<Integer, String>();

//添加

map.put(6, "浪子一秋");

map.put(2, "一叶知秋");

map.put(1, "葬剑山庄");

map.put(9, "武林至尊");

map.put(4, "葬剑灬尊");

System.out.println(map.toString());

//取值

System.out.println(map.firstEntry());

//获取所有元素

Set<Entry<Integer, String>>en=map.entrySet();

for (Entry<Integer, String> entry : en) {

System.out.print(entry.getKey()+"\t"+entry.getValue());

System.out.println();

}

System.out.println(map.floorEntry(3));

}

}

输出结果:

2.Collections

package day21.map;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

 

public class CollectionsDemo {

public static void main(String[] args) {

//向指定的列表中添加元素

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

Collections.addAll(list,"id ","name","age","sex");

System.out.println(list.toString());

//使用二分查找法查找指定元素

System.out.println(Collections.binarySearch(list, "sex"));

//将列表中的元素复制到另一个列表中

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

Collections.addAll(dest,"id1 ","name","age","sex","nn");

Collections.copy(dest, list);

System.out.println(dest.toString());

//反转顺序

Collections.reverse(dest);

System.out.println(dest.toString());

//获取最值,assic码值

System.out.println(Collections.max(dest));

System.out.println(Collections.min(dest));

//比较器

Collections.reverseOrder();

System.out.println(dest.toString());

//Collections.reverseOrder(Comparator<String>cmp);

 

//填充元素

Collections.fill(dest,"score");

System.out.println(dest.toString());

}

}

输出结果:

3.知识框架

 

猜你喜欢

转载自blog.csdn.net/qq_41534115/article/details/81636976