黑马程序员技术交流社区

标题: Java基础学习 第18天 [打印本页]

作者: luguoyuanf    时间: 2013-4-16 23:14
标题: Java基础学习 第18天
问题总结:
1)  is a 和like a的区别.
区分在程序的设计上
2) JDK中哪些类是不能继承的?
JDK中的类写着final的类就不能被继承.
3) 什么时候覆写hashCode,equals?
当数据要存在HashSet时需要覆写HashCode和equals.
4)comparable,comparator区别
对象自己具备比较性用comparable接口实现,让集合自身也具有比较性.
5) 集合中接口不需要覆写.

Map<K,V>双列集合
Map集合包含两个类型参数:键(Key)和值(Value).
HashMap: 基于哈希表数据结构的双列集合,不允许重复键(Key),允许null作为键或者值,没有迭代器.
keySet()方法将集合中的键存储到Set集合中,迭代Set集合可以获取到键,通过get方法获取到值.
entrySet()方法,将集合中键值映射关系保存在Map.Entry对象中,保存到Set中.
put(key,value)方法,将键值存储到集合中.
get(Objectkey)传入键,返回对应的值,如果没有找到对应的键返回null.
remove(Objectkey)通过键删除该映射关系,前提是存在这个映射关系.
isEmpty()如果此映射不包含键值映射关系,则返回true.
提取方式(1)---工作开发中使用
//新建一个HashMap集合
HashMap<String,Integer>hm = new HashMap<String,Integer>();
//向HashMap内分别存入键(Key)和值(Value)
hm.put("zhangsan1",21);
hm.put("lisi",22);
//将HashMap集合的键(Key)返回到Set集合内,这里泛型只写键值的数据类型
Set<String>s = hm.keySet();
//创建Set集合的Iterator迭代器,由于Set集合中的数据类型是String,所以泛型也设置String
Iterator<String>it = s.iterator();
//while循环取值
while(it.hasNext()){
//迭代器取出Set集合内存储的键(Key)并强制转换成String类型
Stringst = (String)it.next();
//HashMap集合使用get方法获取值(Value)
Integerin = hm.get(st);
//打印键(Key)和值(Value)
System.out.println(st+"..."+in);
}
提取方式(2)---面试使用
//新建一个HashMap集合
HashMap<String,Integer>hm = new HashMap<String,Integer>();
//向HashMap内分别存入键(Key)和值(Value)
hm.put("zhangsan1",21);
hm.put("lisi",22);
//使用Map.Entry将对应关系封装成对象返回,用Set集合接收.这个关系用Map.Entry对象描述,Map.Entry的泛型是这个集合中的键值.
//得到的这个关系需要将这个关系存储到Set集合中,所以Set集合的泛型就是这个关系.
//entrySet返回集合对应关系
Set<Map.Entry<String,Integer>>set = hm.entrySet();
//创建Set迭代器,Map没有迭代器,泛型跟Set相同.
Iterator<Map.Entry<String,Integer>>it = set.iterator();
//判断下个元素是否存在
while(it.hasNext()){
//定义个Map.Entry类型变量接收Set集合内的元素
Map.Entry<String,Integer>me = it.next();
//getKey方法提取键
Strings = me.getKey();
//getValue方法提取值
Integerin = me.getValue();
//打印输出键值
System.out.println(s+"==="+in);
}

作者: luguoyuanf    时间: 2013-4-16 23:16
HashMap存取自定义类实例
//集合类型 <键类型,值类型>泛型        变量名 = new 集合类型<键类型,值类型>泛型 ();
                HashMap<Car,String> hm = new HashMap<Car,String>();
                //向集合内存储键(Key)和值(Value)
                hm.put(new Car("东风",2),"中国");
                hm.put(new Car("上海大众",4),"中国");
                hm.put(new Car("福特",4), "美国");
                //使用entrySet获取映射关系赋值给Set类型变量,Set泛型所以是Map.Entry<Car,String>
                //Map.Entry<Key,Value>这里的泛型是键值.
                Set<Map.Entry<Car,String>> set = hm.entrySet();
                //创建迭代器,泛型Set集合的泛型一样
                Iterator<Map.Entry<Car, String>> it= set.iterator();
                while (it.hasNext()){
                        //定义一个Map.Entry类型的me变量接收迭代器的下一个元素,泛型是Map集合的键值
                        Map.Entry<Car,String> me = it.next();
                        //定义Car类型的变量接收getKey()方法获取的键,在Car类要覆写toString类,否则打印出来的是地址值
                        Car c = me.getKey();
                        //定义String类型的变量接收getValue方法获取的值.
                        String s = me.getValue();
                        System.out.println(c+"..."+s);
                }
TreeMap:底层基于二叉树数据结构的双列集合,不允许重复键,对存储对象进行自然排序,可以自定义比较器,是TreeMap具备比较性
TreeMap存取自定义类
        //导包
        import java.uti.*;
        //新建一个TreeMap集合,泛型设置为集合的键值
        TreeMap<Animal,String> tm = new TreeMap<Animal,String>();
        //向TreeMap集合中存储键(Key)和值(Value)
        tm.put(new Animal("xiaobai",2),"吃萝卜");
        tm.put(new Animal("dahuilang",1), "吃肉");
        //使用keySet方法获取键(Key),赋值给Set类型集合
        Set<Animal> s = tm.keySet();
        //创建Set类型迭代器
        Iterator<Animal> it= s.iterator();
        while (it.hasNext()){
                //使用迭代器取值并强行转换为自定义类并赋值给自定义类
                Animal a = (Animal)it.next();
                /*String st = a.getName();
                int i = a.getAge();*/
                //使用TreeMap集合的get()方法获取值(Value)
                String st1 = tm.get(a);
                System.out.println(a+"...."+st1);
        }
//实现Comparable接口并覆写compareTo方法
class Animal implements Comparable<Animal>{
..........
//覆写compareTo方法,建立自己的比较方式.
        public int compareTo(Animal obj){
                //按照数字的升序排序
                /*int num = age - obj.age;
                return num==0?name.compareTo(obj.name):num;*/
                //按照字母的字典顺序排序
                int num = name.compareTo(obj.name);
                return num ==0?age-obj.age:num;
        }
}
TreeMap小练习
需求:计算字符串中的字母出现过几次,不记录特殊符号,数字等其他字符.
//定义一个字符串
                String s = "hsakhkjsadhhdhd &^^%$#$";
                //使用toCharArray方法转换字符串为字符数组
                char[] ch = s.toCharArray();
                //定义个空TreeMap集合存储记录次数
                TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
                //循环提取字符数组的内容
                for (int x=0;x<ch.length;x++){
                        //健壮性判断,如果不是大小写字母则continue返回重新开始循环.
                        if((ch[x]<'a'||ch[x]>'z') && (ch[x]<'A'||ch[x]>'Z')){
                                continue;
                        }
                        //定一个Integer类型变量,判断TreeMap的值是否为空,所以数据类型是Integer而不是int
                        Integer i = tm.get(ch[x]);
                        //判断是否为空,为空就将字符数组的元素存入集合,并在值上面赋值为1.
                        if (i==null){
                                tm.put(ch[x], 1);
                        }else
                        //否则就是存在的字符,先将值自加1再重新赋值到集合中.
                        {
                                i++;
                                tm.put(ch[x],i);
                        }
                       
                }
                //keySet()方法获取键并存储到Set类型集合,因为键的类型是Character,所以这里泛型时Character
                Set<Character> set = tm.keySet();
                //定义Iterator迭代器,泛型跟Set类型集合一样
                Iterator<Character> it = set.iterator();
                while (it.hasNext()){
                        //强制转换键的类型为Character并赋值给char类型变量
                        char c = (Character)it.next();
                        //使用get(key)方法提取相应的值
                        int i = tm.get(c);
                        System.out.println(c+"出现了"+i+"次");
                }

作者: luguoyuanf    时间: 2013-4-16 23:16
HashTable:底层基于哈希表数据结构的双列集合,不允许重复键,不允许使用null作为键或者值,支持线程同步,速度比较慢,用于多线程. HashMap和HashTable区别:
1) HashMap不重复键,允许使用null作为键值;HashTable不允许重复键,不允许使用null作为键值.
2) HashMap不支持线程同步,速度快,用于单线程.
3) HashTable支持线程同步,速度比较慢,用于多线程. Collections 作用:专门对Collection进行操作.
特点:这个类中的方法,都没有访问过特有数据全部是静态,只能操作collection集合.
                //建立一个ArrayList集合
                ArrayList<String> al = new ArrayList<String>();
                //向集合内添加元素
                al.add("zzz111111");
                al.add("zz12");
                al.add("ggee1r1");
                al.add("sa1");
                //可以按照List集合默认的自然顺序排序,也可以自己定义比较器.
                //格式:Collections.sort(集合名);
                Collections.sort(al);
                System.out.println("原:"+al);
                //格式:Collections.sort(集合名,new 比较器());
                Collections.sort(al,new NewCompare());
                System.out.println("新:"+al);
                //按照自然顺序查找返回最大值
                String s = Collections.max(al);
                System.out.println(s);
                //按照自然顺序查找返回最小值
                String s1 = Collections.min(al);
                System.out.println(s1);
                //将集合元素全部替换
                Collections.fill(al,"www");
                System.out.println(al);
                //将集合的元素全部反转
                Collections.reverse(al);
                System.out.println(al);
                //方法强行逆转自然顺序
                Collections.sort(al,Collections.reverseOrder());
                System.out.println(al);
                //指定下标对元素互换位置
                Collections.swap(al,1,2);
                System.out.println(al);
                //对元素随机顺序排列
                Collections.shuffle(al);
                System.out.println(al);
                //根据集合元素下标进行二分查表法查找,如果没查找到则返回
                System.out.println(al);
                int index = Collections.binarySearch(al,"zz3");
                System.out.println(index);
                //ArrayList允许重复,存储和取出顺序一致,线程不同步.
                synchronizedCollections将不同步的集合,变成同步的集合.
                Collection c = Collections.synchronizedCollection(myCollection);
     ...
  synchronized(c) {
      Iterator i = c.iterator(); // Must be in the synchronized block
      while (i.hasNext())
         foo(i.next());
  }
Arrays
全静态方法,直接使用.
sort()对数组进行升序排序
        int[] a = {3,4,52,12,9};
        Arrays.sort(a);
        for (int i =0;i<a.length;i++){
        System.out.println(arr[i]+" ");
        }
binarySearch()方法对有序的数组进行二分查找,找不到返回(-插入点)-1.
先排序再二分查到.
asList()转换数组为List集合后,不能改变长度(不能添加,删除元素,否则抛UnspportedOperationException异常).
        String[] s = {"a","b","c"};
        List<String> list = Arrays.asList(s);  //转换成集合
        String[] s1 =(String[])list.toArray(); //强转回数组.
        int i = {4,5,6,7};  //5个对象
        List news = Arrays.asList(i); //集合不能接收基本类型,只能接收对象,数组是引用类型.

增强For循环 ----记住格式
JDK1.5版本出现的新功能,增强型for循环(但是没有增强while循环).
作用:使用查看数组和集合内的元素.
提倡使用简单方式来遍历集合和数组.
                ArrayList<String> al = new ArrayList<String>();
                al.add("sads");
                al.add("qwef");
                al.add("qwerddsa");
                //格式:数据类型 变量名:集合或者数组
                for (String s:al){
                        System.out.println(s);
                }
                //增强for循环不能直接操作Map,但是可以间接操作,可以直接操作Collection集合和数组.
                HashMap<Integer,String> hm = new HashMap<Integer,String>();
                hm.put(1,"add1");
                hm.put(2,"add2");
                hm.put(3,"add3");
                //Map.Entry的entrySet方法获取键(Key)
                Set<Map.Entry<Integer,String>> set = hm.entrySet();
                //数据类型写Set的泛型
                for (Map.Entry<Integer, String> me:set){
                        //getKey方法获取键
                        Integer i = me.getKey();
                        //getValue方法获取值
                        String s = me.getValue();
                        System.out.println(i+"..."+s);
                }
静态导入--了解
开发中不建议使用,避免冲突.
        import static java.lang.System.*; //静态导入包,只能导入到类.
        out.println("helloworld!");
        import static java.util.Arrays.*;
        sort(arr);

函数的可变参数--记住格式
格式:
函数修饰符 返回值类型 方法名 (数据类型...变量名){
        参数就是一个数组
        }
        保证数据类型一致.
        public static void sum(int...arr){ //必须三个.
        int sum = 0;
        for(int x = 0;x<arr.length;x++{
                sum = sum + arr[x];
        }
        System.out.println(sum);
        }
        public static void main(String[] args){
        sum(4,5,6,7);
        }

作者: Sword    时间: 2013-4-16 23:46
不知道写这么多代码,想说明什么问题?{:soso_e132:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2