Collection接口包含List接口与Set接口
——List 元素有序(存储顺序和取出顺序一致),元素可以重复
实现List接口的三个具体类有 ArrayList,vector,LinkedList
——Set 元素唯一,且元素无序(存储顺序和取出顺序不一致)
实现Set接口的两个具体类有: HashSet,TreeSet
Map接口不属于Collection接口
1、添加功能: put(K key,V value)将指定的值与该映射中的指定键相关联
2、删除功能:
remove(Object key)如果存在,从该map集合中删除一个键的映射
void clear()从该map集合中删除所有的映射
3、长度功能:int size()返回此地图中键值映射的数量
package review.MapDemo;
import java.util.HashMap;
import java.util.Map;
public class demo1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
// put(K key,V value)将键值对放入map集合
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
//两次插入同一个key的映射的时候,返回的是该key上一个映射中对应的值
System.out.println(map.put("孙悟空","金箍棒")); //null
System.out.println(map.put("孙悟空","定海神珍")); //金箍棒
System.out.println(map.toString()); //{沙僧=行李, 孙悟空=定海神珍, 唐僧=袈裟, 八戒=九齿钉耙}
// remove(Object key)根据key删除元素,会返回key对应的value值
String value1 = map.remove("沙僧");
System.out.println(value1); //行李
System.out.println(map.toString()); //{孙悟空=定海神珍, 唐僧=袈裟, 八戒=九齿钉耙}
// size() 返回map集合中映射的数量
System.out.println(map.size()); //3
// void clear()清空map集合
map.clear();
System.out.println(map.toString()); //{}
}
}
4、判断功能:
boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
package review.MapDemo;
import java.util.HashMap;
import java.util.Map;
public class demo2 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
//containsKey(Object key)判断集合中是否包含某个键
System.out.println(map.containsKey("八戒")); //true
System.out.println(map.containsKey("孙悟空")); //false
//containsValue(Object value)判断集合中是否包含某个值
System.out.println(map.containsValue("袈裟")); //true
System.out.println(map.containsValue("女儿国国王")); //false
//isEmpty()判断map集合是否为空
System.out.println(map.isEmpty()); //false
map.clear();
System.out.println(map.isEmpty()); //true
}
}
5、获取功能:
(一)get(Object key) 根据指定的key获取对应的value
(二)Set< K > keySet() 返回一个Set集合,包含map集合中全部的key值
(三)Collection< V > values() 返回一个Collection集合,包含map集合中全部的value值
(四)Set
package review.MapDemo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class demo3 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
// get(Object key)根据指定的key获取对应的value
//如果key值不存在,则返回null
System.out.println(map.get("唐僧")); //袈裟
System.out.println(map.get("女儿国国王")); //null
//Set keySet()返回一个Set集合,包含map集合中全部的key值
Set<String> keys = map.keySet();
for(String s : keys){
System.out.println(s);
}
//Collection values()返回一个Collection集合,包含map集合中全部的value值
Collection<String> values = map.values();
for(String s : values){
System.out.println(s);
}
//Set> entrySet()返回一个Set集合,包含map集合中全部的映射关系
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for(Map.Entry<String,String> s : entrySet){
String key = s.getKey();
String value = s.getValue();
System.out.println(key+"="+value);
}
}
}
package review.MapDemo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class demo4 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("孙悟空","金箍棒");
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
/*
借助Set keySet()遍历
1、获取Map集合中所有映射的键的Set集合
2、遍历键的集合,根据每一个键获取对应的值
*/
//方式一,用增强for循环
Set<String> keys = map.keySet();
for(String s : keys){
String key = s;
String value = map.get(s);
System.out.println(key+"="+value);
}
//方式二,用迭代器
//为Set集合创建一个迭代器Iterator
Set<String> itkeys = map.keySet();
Iterator<String> it = itkeys.iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
package review.MapDemo;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class demo5 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("孙悟空","金箍棒");
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
/*
通过集合中每个键值对(Entry)对象,获取键值对(Entry)对象中的键与值。
1、获取所有的键值对的集合
2、遍历包含所有键值对的Set集合,得到每一个键值对对象
3、根据获取到的每一个键值对,来获取键和值
*/
//方式一、增强for循环
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for(Map.Entry<String,String> s : entrySet){
String key = s.getKey();
String value = s.getValue();
System.out.println(key+"="+value);
}
//方式二、迭代器遍历
Set<Map.Entry<String,String>> entrySet1 = map.entrySet();
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String,String> entry = it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
结果同上面一样
具体实现我在之前的博客写过了
集合List和Map——重写equals和hashCode方法(集合元素是类的对象)
这里举一个简单的例子
package review.MapDemo;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
package review.MapDemo;
import java.util.HashMap;
import java.util.Set;
public class demo6 {
public static void main(String[] args) {
HashMap<Student,Integer> map = new HashMap<>();
Student s1 = new Student("zhang",12);
Student s2 = new Student("zhou",14);
Student s3 = new Student("zhang",12);
map.put(s1,1);
map.put(s2,2);
map.put(s3,3);
Set<Student> keys = map.keySet();
for(Student s : keys){
String name = s.getName();
int age = s.getAge();
System.out.println(name+"---"+age);
}
}
}
LinkedHashMap
package review.MapDemo;
import java.util.LinkedHashMap;
import java.util.Set;
public class LinkedHashMapDemp1 {
public static void main(String[] args) {
LinkedHashMap<String,String> map = new LinkedHashMap<>();
map.put("1","java");
map.put("3","hive");
map.put("2","hadoop");
Set<String> keys = map.keySet();
for(String s : keys){
String key = s;
String value = map.get(s);
System.out.println(key+"="+value);
}
}
}
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo1 {
public static void main(String[] args) {
TreeMap<String,Integer> treeMap = new TreeMap<>();
treeMap.put("java",1);
treeMap.put("hive",2);
treeMap.put("flume",3);
Set<Map.Entry<String,Integer>> set = treeMap.entrySet();
for(Map.Entry<String,Integer> entry : set){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
package review.TreeMapDemo;
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int compareTo(Student s){
//首先按照年龄进行排序
int i = this.age - s.age;
//然后判断是不是属性值一样,如果都一样则认为是一个对象,不会加入到集合中
int ii = i==0 ? this.name.compareTo(s.name) : i;
return ii;
}
}
package review.TreeMapDemo;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo2 {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<>();
Student s1 = new Student("zhang",16);
Student s2 = new Student("meng",30);
Student s3 = new Student("cao",16);
Student s4 = new Student("zhang",16);
treeMap.put(s1,"学生一");
treeMap.put(s2,"学生二");
treeMap.put(s3,"学生三");
treeMap.put(s4,"学生四");
Set<Map.Entry<Student,String>> set = treeMap.entrySet();
for(Map.Entry<Student,String> entry : set){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
结果为
属性值相同的学生对象被去重成功,不同的学生对象按照年龄进行排序,相同年龄的学生继续按照姓名进行排序
package review.TreeMapDemo;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo3 {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.getAge() - o2.getAge();
int i2 = i==0 ? o1.getName().compareTo(o2.getName()) : i;
return i2;
}
});
Student s1 = new Student("zhang",16);
Student s2 = new Student("meng",30);
Student s3 = new Student("cao",16);
Student s4 = new Student("zhang",16);
treeMap.put(s1,"学生一");
treeMap.put(s2,"学生二");
treeMap.put(s3,"学生三");
treeMap.put(s4,"学生四");
Set<Map.Entry<Student,String>> set = treeMap.entrySet();
for(Map.Entry<Student,String> entry : set){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
需求:用键盘录入字符串,获取字符串中每一个字母出现的次数
输出格式为:a(1)b(2)c(3)
package review;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class StringDemo1 {
public static void main(String[] args) {
//接收键盘录入的字符串
Scanner sc = new Scanner(System.in);
String next = sc.next();
//将字符串转为字符数组
char[] c = next.toCharArray();
//创建一个TreeMap,用这个Treemap的key值来存放字符,value值来存放该字符出现的次数
TreeMap<Character,Integer> treeMap = new TreeMap<>();
//创建一个StringBuffer对象,用来打印TreeMap中的值
StringBuffer sb = new StringBuffer();
//循环遍历字符数组中的每一个字符
//每次循环都用当前字符作为key值去集合中查询
//如果值为空,说明该字符是第一次出现,则加入到集合中
//如果值不为空,则修改,值+1
for(int i=0;i<c.length;i++){
if(treeMap.get(c[i])==null){
treeMap.put(c[i],1);
} else {
treeMap.put(c[i], treeMap.get(c[i]) + 1);
}
}
//打印TreeMap集合中的值
Set<Character> set = treeMap.keySet();
for(Character cc : set){
sb.append(cc+"("+treeMap.get(cc)+")");
}
System.out.println(sb.toString());
}
}