Java集合Map与其子类回顾

接10月12号昨天的笔记,今天继续回顾集合中的Map集合。

一、集合工具操作类Collections

问题:collection和collections的区别?

1、collection是单列集合的顶层接口,它有List和Set子接口

2、collections是集合的一个操作类,里面包含的都是一些集合操作的静态方法

Collections的方法概述

1、排序

public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。

ArrayList <Integer> list =  new ArrayList<Integer>();
list.add(10);
list.add(50);
list.add(5);
Collections.sort(list);

sort可以针对基本数据类型进行排序,也可以针对引用类型进行排序。只是传入参数不同。

注意:如果该类同时实现了自然排序和比较器排序,那么优先使用比较器排序。

下面是一个学生类用比较器(匿名内部类)的方法实现的排序。

public static <T> void sort(List<T> list, Comparator<? super T> c)  

Collections.sort(list, new Comparator<Student>() {
    @Override
    public int compare(Student s1, Student s2) {
              int num = s2.getAge() - s1.getAge();
              int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;
          return num2;
     }
 });

 2、二分查找

public static <T> int binarySearch(List<?> list,T key)

返回值:当查找的键在列表中,返回键在集合中索引;否则返回(-(插入索引)-1)。
注意:使用二分查找时,需要对集合进行排序(sort(list)),否则无法确定查找键的位置。

3、最大值

public static <T> T max(Collection<?> coll)

4、反转

public static void reverse(List<?> list)

5、随机置换

public static void shuffle(List<?> list)

二、Map集合

相对于Collection的区别:它可以存储键值对元素,而Collection只能存储单个元素。

Map的特点:键唯一,值可重复,一个键映射一个值;Map中的数据结构只针对键有效。

Map集合功能概述:

1、添加功能

V put(K key,V value)

注意:如果是第一次存储,直接存储,返回null;如果键不是第一次存在,就将值替换,返回之前的值。

Map<String,String> map = new hashMap<String,String>();
map.put("baidu","B");
map.put("alibaba","A");
map.put("tencen","t");

 2、删除功能

void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回

3、判断功能

boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空

4、获取功能

Set<Map.Entry<K,V>> entrySet():获取键值对集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合

通过键获取到值

HashMap<String,String> hm = new HashMap<String,String>()
hm.put("baidu","B");
hm.put("tencen","t");
hm.put("alibaba","a");
Set<String> key = hm.keySet();
for(String key:hm){
String value = hm.get(key);
System.out.println(key+"------"+value);
}

获取到键值对集合然后分别获取键和值

HashMap<String,String> hm = new HashMap<String,String>()
hm.put("baidu","b");
hm.put("tencen","t");
hm.put("alibaba","a");
Set<Map.Entry<String,String>> set = hm.entrySet();
for(Map.Entry<String,String> me :hm){
String key = hm.getKey();
String value = hm.getValue();
System.out.println(key+"------"+value);
}

5、长度功能

int size():返回集合中的键值对的对数

TreeMap和TreeSet相同,需要排序时可实现比较器排序,使用匿名内部类的方式 new Comparator ()重写compare ()方法

下面使用Map实现一个功能:

需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)

分析:

  1、接收一个字符串

  2、创建一个TreeMap集合

  3、将字符串转换为字符数组

  4、遍历字符数组获取到每一个字符作为键

  5、在集合中用键查找值,如果值为null就添加,如果键存在,值就加1再存入集合

  6、创建字符缓冲数组

  7、遍历集合并将集合中的键和值拼接并存入缓冲区

  8、字符缓冲区转换为字符串输出

public class countNum{

public static void main(String[] args ){

Scanner sc =new Scanner(System.in);

System.out.println(" 请输入一个字符串:");

String s = sc.nextLine();

char[] ch = s.toCharArray();

TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();

for(Character key:ch){

Integer i = tm.get(key);

if(i=null){

  hm.put(key,1);

}else{

  i++;

  hm.put(key,i);

    }

  }

}

StringBuilder sb = new StringBuilder();

Set<Map.Entry<Character,Integer>> set = hm.entrySet();

Character key = hm.getKey();

Integer value = hm.getValue();

sb.append(key).append("(").append(value).append(")");

String  result= sb.toString();

System.out.println(result);

}

}
原文地址:https://www.cnblogs.com/zhoumiao/p/7661270.html