黑马程序员_学习笔记_Java基础_集合(三)

来源:互联网 发布:剑网三大师捏脸数据 编辑:程序博客网 时间:2024/05/02 02:07

----------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! -----------------

集合第三部分 Map接口、集合工具类与JDK1.5部分新特性

1、Map接口

特点:

该集合存储键值对,是一对一对往里存,而且要保证键值的唯一性。这与Collection不同,Collection是单列集合,Map是双列集合。
Map框架:

Map

         |--Hashtable    底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。

         |--HashMap     底层是哈希表数据结构,并允许使用null键null值,该集合不同步。高效。

         |--TreeMap      底层是二叉树数据结构,线程不同步。可以用于给Map集合中的键进行排序。

Map接口的一般方法:

1、添加 put(K key,V value) putAll(Map<?extends K,? extends V> m)

2、删除  clear()       remove(Object key)

3、判断  containsValue(Objectvalue)  containsKey(Object key)          isEmpty()

4、获取  get(Objectkey)         size()         values()

排序

在排序上Map集合和Set很像,其实Set底层就是使用了Map集合。

而Set的一般方法只能一次取出一个值,如何一次性取出多个呢?这里Map集合有两种取出方法。

即keySet()和entrySet()两个方法。下面的例子便于掌握Map接口和这两种方法

/* * 练习: * 每一个学生都有对应的归属地。 * 学生Student,地址String。 * 学生属性,姓名年龄。 * 注意:姓名和年龄都相同视为同一个学生为重复元素。需保证唯一性。 * 1、描述学生---------实现comparable * 2、定义Map容器,将学生作为键,地址作为值存入。 * 3、获取Map集合中的元素。 *  */import java.util.*;class Student implements Comparable<Student>{private String name;private int age;Student(String name,int age){this.name=name;this.age=age;}public int compareTo(Student s){int num = new Integer(this.age).compareTo(new Integer(s.age));if(num==0)return this.name.compareTo(s.name);return num;}public int hashCode(){return name.hashCode()+age*22;}public boolean equals(Object obj){if(!(obj instanceof Student))throw new ClassCastException("类型不匹配");Student s = (Student)obj;return this.name.equals(s.name)&&this.age==s.age;}public String getName(){return name;}public int getAge(){return age;}public String toString(){return name+":"+age;}}public class MapTest1 {public static void main(String[] args) {HashMap<Student,String> hm = new HashMap<Student,String>();hm.put(new Student("张三",18),"北京");hm.put(new Student("张三",18),"天津");hm.put(new Student("李四",19),"武汉");hm.put(new Student("王五",16),"重庆");hm.put(new Student("陈六",23),"河北");//第一种取出方式keySet。Set<Student> keyset = hm.keySet();for(Iterator<Student> it =keyset.iterator();it.hasNext();){Student key = it.next();String value = hm.get(key);System.out.println(key+":"+value);}//第二种取出方式entrySetSet<Map.Entry<Student, String>> entryset = hm.entrySet();for(Iterator<Map.Entry<Student, String>> it = entryset.iterator();it.hasNext();){Map.Entry<Student, String> me = it.next();Student stu = me.getKey();String addr = me.getValue();System.out.println(stu+"..."+addr);}}}

Map扩展知识:集合嵌套

下面的实例来掌握集合嵌套:

import java.util.*;class Student{private String id;private String name;Student(String id,String name){this.id = id;this.name = name;}public String toString(){return id+":::"+name;}}class  MapDemo{public static void demo(){HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();List<Student> reyu = new ArrayList<Student>();List<Student> jiuye = new ArrayList<Student>();czbk.put("yureban",reyu);czbk.put("jiuyeban",jiuye);reyu.add(new Student("01","zhagnsa"));reyu.add(new Student("04","wangwu"));jiuye.add(new Student("01","zhouqi"));jiuye.add(new Student("02","zhaoli"));Iterator<String> it = czbk.keySet().iterator();while(it.hasNext()){String roomName = it.next();List<Student> room = czbk.get(roomName);System.out.println(roomName);getInfos(room);}}public static void getInfos(List<Student> list){Iterator<Student> it = list.iterator();while(it.hasNext()){Student s = it.next();System.out.println(s);}}public static void main(String[] args) { demo();}public static void getStudentInfo(HashMap<String,String> roomMap){Iterator<String> it = roomMap.keySet().iterator();while(it.hasNext()){String id = it.next();String name = roomMap.get(id);System.out.println(id+":"+name);}}}

2、集合工具类

集合工具类包括Collections和Arrays。

Collections工具类

(1)Collections类中存放的都是静态方法,可以直接Collections.方法名()来调用。
(2)Collection与Collections的区别:(重要)
Collection是集合框架中的一个顶层接口,里面定义了单列集合的共性方法。包含两个常用的子接口:List和Set接口。
而Collections是一个工具类,里面存放了很多静态方法,方便使用。
(3)通过下面的方法掌握Collections工具类:
import java.util.*;public class CollectionsDemo {public static void main(String[] args) {binarySearchDemo();}public static void binarySearchDemo(){List<String> list=new ArrayList<String>();list.add("abce");list.add("caee");list.add("hewtce");list.add("tce"); list.add("gge");list.add("hewtddce");Collections.sort(list);sop(list);int index = Collections.binarySearch(list,"gae");sop("Index = "+index);}public static void maxDemo(){List<String> list=new ArrayList<String>();list.add("abce");list.add("caee");list.add("zhewtce");list.add("tce");list.add("gge");list.add("hewtddce");String max = Collections.max(list);sop("max=="+max);}public static void sortDemo(){List<String> list=new ArrayList<String>();list.add("abce");list.add("caee");list.add("hewtce");list.add("tce");list.add("gge");list.add("hewtddce");sop(list);Collections.sort(list,new StrLenComparator());sop(list);}public static void sop(Object obj){System.out.println(obj);}}class StrLenComparator implements Comparator<String>{public int compare(String s1,String s2){if(s1.length()>s2.length())return 1;if(s1.length()<s2.length())return -1;return s1.compareTo(s2);}}

Arrays工具类

是用于操作数组的工具类。里面也都是静态方法。
重要方法:asList,可以将数组变为List集合。
数组变集合的好处:可以使用集合的思想和方法来操作数组中的元素。
但需要注意的是,将数组变为集合不可以使用集合的增删方法,因为数组长度是固定的。
请看下面的例子:
import java.util.*;public class ArraysDemo {public static void main(String[] args) {String[] arr = {"jtju","klu","cjy","gea","cage","age"};List<String> list = Arrays.asList(arr);sop(list);}public static void sop(Object obj){System.out.println(obj);}}
还需要注意的地方是:如果数组中的元素都是对象,那么变成集合时,数组中的元素就直接转成集合中的元素。
如果数组中的元素都是基本数据类型,那么会将该数组作为集合中的元素。

3、JDK1.5部分新特性

(1)高级for循环

格式:
for(数据类型 变量名:被遍历的集合(Collection)或数组)
使用高级for循环与迭代器的区别 
对集合进行遍历,只能获取元素,不能对集合进行操作(增删改查)。
而迭代器除了遍历,还可以remove集合中的元素。
如果使用ListIterator,还可以在遍历过程中进行增删改查等操作。
传统for循环和高级for的区别
高级for循环有一个局限性,必须有被遍历的目标。
建议在遍历数组的时候使用传统for循环。因为传统for可以定义角标。
通过下面的事例了解高级for循环
import java.util.*;public class ForEachDemo {public static void main(String[] args) {ArrayList<String> al=new ArrayList<String>();al.add("abc1");al.add("abc2");al.add("abc3");/*for(Iterator<String> it =al.iterator();it.hasNext();){System.out.println(it.next());}*/for(String s : al){System.out.println(s);}HashMap<Integer,String> hm = new HashMap<Integer,String>();hm.put(1,"aaa");hm.put(2,"bbb");hm.put(3,"ccc");hm.put(4,"ddd");Set<Integer> keyset = hm.keySet();for(Integer i : keyset){System.out.println(i+"....."+hm.get(i));}Set<Map.Entry<Integer, String>> entryset = hm.entrySet();for(Map.Entry<Integer, String> me :entryset){System.out.println(me.getKey()+":::::"+me.getValue());}for(Map.Entry<Integer, String> me :hm.entrySet()){System.out.println(me.getKey()+"-----"+me.getValue());}}}

(2)方法的可变参数

可变参数其实就是一种数组参数的简写形式,不必每次都手动建立数组对象了。
只要将这些要操作的元素作为参数传递即可。隐式的将这些参数封装成数组了。
请看下面的事例:
import java.util.*;public class ParamMethodDemo {public static void main(String[] args) {show("abce",2,5,67,7,8,3);//show(2,5,67,7,8,3,6,4,23,56,43);}public static void show(String s,int...arr){System.out.println(arr);}}
使用时需要注意的是,可变参数要定义在参数列表的最后边。

(3)静态导入

当类名重名时,需要指定具体的包名;当方法重名时,需要指定具备所述的对象或类。

了解下面的例子:

import java.util.*;import static java.util.Arrays.*;//导入的是Arrays类中所有的静态成员。public class StaticImport {public static void main(String[] args) {int[] arr = {3,5,1,8};sort(arr);int index =binarySearch(arr,5);System.out.println(Arrays.toString(arr));System.out.println("Index:"+index);}}
----------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ------------------
详细请查看:http://edu.csdn.net