java基础---集合(下)

java基础—集合 (下)

一、 Map (HashMap、TreeMap)

  • Map的常用子类

    • |–Hashtable:内部结构是哈希表,是同步的。不允许null作为键,null作为值。

      • |–Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
    • |–HashMap:内部结构式哈希表,不是同步的。允许null作为键,null作为值。

    • |–TreeMap:内部结构式二叉树,不是同步的。可以对Map结合中的键进行排序。

  • Map的特点:

    • 一次添加一对元素,Collection一次添加一个元素。

    • Map也称为双列集合,Collection集合称为单列集合。

    • 其实Map集合中存储的就是键值对。

    • map集合中必须保证键的唯一性。

  • Map常用方法

    1. 添加

      扫描二维码关注公众号,回复: 139010 查看本文章
      1. value put(key,value); 返回前一个和key关联的值,如果没有返回null。
    2. 删除

      1. void clear(); 清空Map集合。

      2. value remove(Object key); 根据指定的key删除这个键值对。

    3. 判断

      1. boolean containsKey(key); 如果此映射包含指定键的映射关系,则返回 true。

      2. boolean containsValue(value); 如果此映射将一个或多个键映射到指定值,则返回 true。

      3. boolean isEmpty(); 如果此映射未包含键-值映射关系,则返回 true。

    4. 获取

      1. value get(key): 通过键获取值,如果没有该键返回null。当然可以通过返回null,来判断是否包含指定键。

      2. int size(): 获取键值对个数。

  • 示例:

    • 代码:
import java.util.*;
class MapDemo 
{
    public static void main(String[] args) 
    {
        Map<Integer,String> map=new HashMap<Integer,String>();
        method(map);
    }
    public static void method(Map<Integer,String> map)
    {
        //因为8(key)没有前一个对应的值,故返回null;
        System.out.println(map.put(8,"小明"));

        //返回前一个和key关联的值
        System.out.println(map.put(8,"小红"));

        System.out.println(map);
        map.put(9,"张三");
        map.put(10,"李四");
        System.out.println(map);

        //根据key值删除键值对
        System.out.println("remove:"+map.remove(8));

        //集合中是否包含此键的映射关系
        System.out.println("containsKey:"+map.containsKey(9));

        //根据键获取值
        System.out.println(map.get(9));
    }
}
  • 输出结果:
    这里写图片描述

  • 获取Map集合元素并打印的方法:

    1. 通过keySet方法获取Map中所有的键所在的set集合,在通过set的迭代器获取到每一个键。

    2. 通过entrySet方法将键和值的映射关系作为对象存储到了Set集合中,在分别求得元素的键和值。

    3. 通过values方法获得所有值得Collection集合,在通过Collection的迭代器将每一个值打印出来。
  • 示例(通过keySet方法打印Map集合中的元素)

    • 代码:
import java.util.*;
class MapDemo1 
{
    public static void main(String[] args) 
    {
        Map<Integer,String> map=new HashMap<Integer,String>();

        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"赵六");

        Set<Integer> s=map.keySet();//获取Map及集合中所有键的set集合
        Iterator<Integer> it= s.iterator();//获取Set集合的迭代器
        while (it.hasNext())
        {
            Integer key=it.next();
            String str=map.get(key);//根据键获取其在Map集合中对应的值
            System.out.println(key+"="+str);
        }
    }
}
  • 输出结果:
    这里写图片描述

二、Collections类

  • Collections:是集合框架的工具类,里面的方法都是静态的。

  • Collections的常用方法:

    • binarySearch(List list, T key):使用二分搜索法搜索指定列表,以获得指定对象。

    • sort(List list) :根据元素的自然顺序 对指定列表按升序进行排序。

    • sort(List list, Comparator c) :根据指定比较器产生的顺序对指定列表进行排序。

    • max(Collection coll, Comparator comp) :根据指定比较器产生的顺序,返回给定 collection 的最大元素。

    • reverseOrder(Comparator cmp) :返回一个比较器,它强行逆转指定比较器的顺序。

    • swap(List list, int i, int j) :在指定列表的指定位置处交换元素。

    • replaceAll(List list, T oldVal, T newVal) :使用另一个值替换列表中出现的所有某一指定值。

    • shuffle(List list) :使用默认随机源对指定列表进行置换。

  • 示例:

    • 代码:
import java.util.*;
class CollectionsDemo1
{
    public static void main(String[] args) 
    {
        demo2();
    }
    public static void demo2()
    {
        List<String> al=new ArrayList<String>();
        al.add("aaad");
        al.add("aa");
        al.add("sdd");
        al.add("ddd");
        al.add("rtt");
        //对集合al中的元素进行排序
        Collections.sort(al);
        System.out.println(al);
        //使用二分法查找集合中的指定元素的角标
        int index=Collections.binarySearch(al,"ddd");
        System.out.println(index);
        //获取集合al中字符串长度最大的元素
        String max=Collections.max(al,new ComparatorByLength());
        System.out.println(max);
    }
}
class ComparatorByLength implements Comparator<String>
{
    public int compare(String o1,String o2)
    {
        int temp=o1.length()-o2.length();
        return temp==0?o1.compareTo(o2):temp;
    }
}
  • 输出结果:
    这里写图片描述

三、Arrays工具类

  • Arrays::集合框架的工具类,里面的方法都是静态的。

  • Arrays工具类的常用方法:

    • toString(Object[] obj):返回指定数组内容的字符串表示形式。

    • asList(数组):将数组转成集合

      • 如果数组中的元素是对象,那么转成集合时,直接将数组中的元素作为集合中的元素进行集合存储。
      • 如果数组中的元素是基本类型数值,那么会将该数组作为集合中的元素进行存储。

猜你喜欢

转载自blog.csdn.net/ly_20104803/article/details/49704027