zoukankan      html  css  js  c++  java
  • Java学习笔记七 常用API对象三

    一、泛型:简单说就是对对象类型进行限定的技术

    public class GenericDemo {
        public static void main(String[] args){
            /*泛型作为1.5版之后的新技术,分两步使用
             * 1、在类名之后用<类型参数>,这里就像函数中的普通参数一样命名即可
             * 2、在生成对象和返回该对象参数时需要明确具体的类型,相当于传入实参
             * 上面说的是泛型类,除此之外,泛型还可以用于类中方法和接口
             */
            GenericTest<Person> gt=new GenericTest<Person>();
            gt.setQ(new Person(21,"asd1"));
            
        }
    }
    //泛型类
    class GenericTest<Q>{
        private Q q;
    
        public Q getQ() {
            return q;
        }
    
        public void setQ(Q q) {
            this.q = q;
        }
        //静态方法不能访问类中定义的泛型,如果必须使用泛型,只能将泛型定义在方法体上
        public static <W> void show1(W w){
            
        }
    }
    //泛型接口
    interface Inter<T>{
        public void show(T t);
    }
    class InterImpl implements Inter<String>{
        public void show(String str){
            System.out.println("str="+str);
        }
    }
    泛型示例
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.Iterator;
    
    public class GenericDemo2 {
        public static void main(String[] args){
            demo();
        }
    
        public static void demo() {
            ArrayList<String> al=new ArrayList<String>();
            al.add("asd1");
            al.add("asd2");
            //注意
            HashSet<Integer> hs=new HashSet<Integer>();
            hs.add(1);
            hs.add(2);
            printCollection(al);
            printCollection(hs);
        }
    
        /*这种参数和以前的类似,就是将类型提升到Collection,
         * 以便接受所有集合类型,然后用泛型规定集合中的元素类型
         * ?是通配符,在类型不确定时可以指代任意类型
         */
        private static void printCollection(Collection<?> coll) {
            for (Iterator<?> i = coll.iterator(); i.hasNext();) {
                System.out.println(i.next());
            }
        }
    
    //下面这种方式使用泛型方法,和上面使用?通配符类似,
    //只是T可以指代具体,而?不行。如果需要对类型进行操作,就需要使用这种方式
    //    private static <T> void printCollection(Collection<T> coll) {
    //        for (Iterator<T> i = coll.iterator(); i.hasNext();) {
    //            System.out.println(i.next());
    //        }
    //    }
    }
    泛型示例2

    二、Map集合:存储键值对元素

     1 import java.util.HashMap;
     2 import java.util.Iterator;
     3 import java.util.Map;
     4 import java.util.Map.Entry;
     5 import java.util.Set;
     6 
     7 public class MapDemo {
     8     public static void main(String[] args) {
     9         MapFunctionDemo();
    10         MapKeySetFunction();
    11         MapEntrySetFunction();
    12         }
    13 
    14     public static void MapFunctionDemo() {
    15         Map<Integer,String> m=new HashMap<Integer,String>();
    16         //设置
    17         System.out.println(m.put(1,"asd"));
    18         System.out.println(m.put(1,"dada"));
    19         m.put(2,"vtr");
    20         m.put(3,"sg");
    21         System.out.println("m="+m);
    22         //删除
    23         int num=2;
    24         System.out.println("删除掉"+num+"号元素:"+m.remove(2));
    25         System.out.println("m="+m);
    26         //判断
    27         System.out.println("m中存在3号元素是"+m.containsKey(3));
    28         //获取
    29         System.out.println("m中3号元素是"+m.get(3));
    30     }
    31 
    32     public static void MapKeySetFunction() {
    33         /*Map中没有迭代器,不能直接获取所有值,但是可以间接获取。使用KeySet()获取的原理
    34          * 1、使用KeySet获取所有键所在的Set集合,!!!这些键在Set集合中的排列完全依靠Set的特点
    35          * 2、通过Set集合的迭代器获取所有键
    36          * 3、通过键获取对应值
    37          */
    38         Map<Integer,String> m=new HashMap<Integer,String>();
    39         m.put(3,"asd");
    40         m.put(2,"gf");
    41         m.put(1,"ju");
    42         m.put(4,"cvby");
    43         Set<Integer> s=m.keySet();
    44         for (Iterator<Integer> i = s.iterator(); i.hasNext();) {
    45             Integer integer =i.next();
    46             System.out.println(integer+"..."+m.get(integer));
    47         }
    48     }
    49     
    50     public static void MapEntrySetFunction() {
    51         /*第二种获取所有值的方法是使用entrySet(),返回Set集合,其中的数据类型是Map.Entry<k,v>
    52          * Map.Entry说明Entey是Map的内部接口。
    53          * !!!而这个接口的方法是static的,所以可以被直接使用。
    54          */
    55         Map<Integer,String> m=new HashMap<Integer,String>();
    56         m.put(3,"asd");
    57         m.put(2,"gf");
    58         m.put(1,"ju");
    59         m.put(4,"cvby");
    60         Set<Map.Entry<Integer,String>> s=m.entrySet();
    61         for (Iterator<Map.Entry<Integer,String>> i = s.iterator(); i.hasNext();) {
    62             Entry<Integer, String> entry = i.next();
    63             Integer age=entry.getKey();
    64             String name=entry.getValue();
    65             System.out.println(age+"..."+name);
    66         }
    67     }
    68 }
    69 
    70 //内部接口和内部类的形式非常相近
    71 interface myMap{
    72     public static interface myEntry{
    73         void show();
    74     }
    75 }
    76 class myMapDemo implements myMap.myEntry{
    77     @Override
    78     public void show() {
    79         System.out.println("实现了内部接口");
    80     }
    81 }
    Map练习
     1 import java.util.HashMap;
     2 import java.util.Iterator;
     3 import java.util.Map;
     4 import java.util.Map.Entry;
     5 import java.util.Set;
     6 import java.util.TreeMap;
     7 
     8 import com.lll.cn.Person;
     9 
    10 public class SubMapDemo {
    11     public static void main(String[] args){
    12         HashMapDemo();
    13         TreeMapDemo();
    14         MapTest();
    15     }
    16 
    17     public static void HashMapDemo() {
    18         //具有Hash特点:即唯一性
    19         HashMap<Person,String> hm=new HashMap<Person,String>();
    20         hm.put(new Person(21,"cio"), "河南");
    21         hm.put(new Person(24,"asdf"), "安徽");
    22         hm.put(new Person(22,"byee"), "黑龙江");
    23         hm.put(new Person(23,"cf"), "黑龙江");
    24         Set<Person> sp=hm.keySet();
    25         for (Iterator<Person> i = sp.iterator(); i.hasNext();) {
    26             Person p = i.next();
    27             String address=hm.get(p);
    28             System.out.println(p+"..."+address);
    29         }
    30     }
    31 
    32     private static void TreeMapDemo() {
    33         //具有Tree特点:需要排序
    34         TreeMap<Person,String> tm=new TreeMap<Person,String>(new ComparatorByName());
    35         tm.put(new Person(21,"cio"), "河南");
    36         tm.put(new Person(24,"asdf"), "安徽");
    37         tm.put(new Person(22,"byee"), "黑龙江");
    38         tm.put(new Person(23,"cf"), "黑龙江");
    39         Set<Map.Entry<Person,String>> s=tm.entrySet();
    40         for (Iterator<Map.Entry<Person,String>> i = s.iterator(); i.hasNext();) {
    41             Entry<Person, String> entry = i.next();
    42             Person p=entry.getKey();
    43             String address=entry.getValue();
    44             System.out.println(p+"..."+address);
    45         }
    46     }
    47 
    48     private static void MapTest() {
    49         /*记录字串中每个字符出现的次数,思路:最后结果是字符和个数相对应,可以使用Map集合来保存
    50          * 所以可以遍历字串,key为字符,value为个数,遇到某个字符,个数就+1
    51          */
    52         String s="adsfkhukvjj";
    53         Map<Character,Integer> m=new HashMap<Character,Integer>();
    54         for (int i = 0; i < s.length(); i++) {
    55             Character curr=s.charAt(i);
    56             if(m.containsKey(curr)){
    57                 int num=m.get(curr);
    58                 m.put(curr, num+1);
    59             }else{
    60                 m.put(curr, 1);
    61             }
    62         }
    63         Set<Map.Entry<Character,Integer>> set=m.entrySet();
    64         for (Iterator<Map.Entry<Character,Integer>> i = set.iterator(); i.hasNext();) {
    65             Entry<Character,Integer> entry = i.next();
    66             Character word=entry.getKey();
    67             Integer num=entry.getValue();
    68             System.out.println(word+"..."+num);
    69         }
    70     }
    71 }
    Map子类练习

    三、集合框架工具类

    1、Collections类用于对collection(或者是子类List/Set)或Map进行操作。例如

      1、List中元素进行排序,不能使用Tree结构,因为允许重复元素,此时就可以使用Collections中的sort()进行排序。

      2、binarySerach()可以对List结构进行二分查找元素,min()/max()可以获取Collection的最值

      3、reverseOrder()返回一个比较器,可以对Tree结构拍好的顺序进行逆序;reverse()可以对List进行逆序操作

      4、shuffle()可以对List中元素进行随机排序

      5、还可以对非同步的Collection/List/Set/Map进行同步操作。synchronized类型

    2、Arrays类专门用于操作数组

      1、binarySerach()二分查找,sort()排序

      2、copyOf()将数组复制到指定数组,长则补0/null(自动),短则截取;copyOfRange()复制数组一部分到指定数组

      3、equals()比较数组是否相等

      4、fill()将指定内容填充数组

      重点方法:

      5、asList()将数组转为List:因为数组功能太少,注意:

        1、数组长度固定,所以即使转换为List,其中的增删方法也不能使用

        2、由于集合中只能存放引用类型数据:如果数组中是引用类型则可以直接存入集合中;如果是基本类型则会将数组作为元素整体存入

    四、集合在1.5版本后新特性:

     1 import java.util.ArrayList;
     2 import java.util.List;
     3 
     4 public class JDK5new {
     5     public static void main(String[] args){
     6         //本部分是说明1.5版本的一些新特性
     7         //新的for循环
     8         newfor();
     9         //不定参数的使用
    10         System.out.println(somePara(1,1,2,3));
    11         System.out.println(somePara(1,1,2,3,4));
    12         //
    13     }
    14 
    15     public static void newfor() {
    16         /*1.5版之后的高级for循环主要是简化集合和数组的遍历操作,
    17          * 参数是  类型  变量:集合名,然后就可以用变量读取数组/集合中的元素
    18          * 注意:新的for循环目的是用于遍历简单,但是不能取代原来的for循环
    19          */
    20         List<String> l=new ArrayList<String>();
    21         l.add("asd");
    22         l.add("fg");
    23         l.add("rrret");
    24         for(String i:l){
    25             System.out.println(i);
    26         }
    27     }
    28 
    29     private static int somePara(int a,int... arr) {
    30         /*Java函数中不定参数的解决方法
    31          * 1、重载函数
    32          * 2、使用数组接收参数
    33          * 3、使用1.5版之后的新方法,就像该方法的参数一样,
    34          *         优点是直接可以向函数传入多个int型数据,会自动将参数封装为数组
    35          *         !!!注意除了这个数组之外还有其他参数时,这个参数应该放在最后
    36          */
    37         int sum=0;
    38         for (int i = 0; i < arr.length; i++) {
    39             sum+=arr[i];
    40         }
    41         return sum;
    42     }
    43 }
    JDK5new

    五、其他常用基础类:、

    1、System:包含一些有用的类字段和方法。它不能被实例化,其中属性和方法都是static的。

     1 import java.util.Properties;
     2 import java.util.Set;
     3 
     4 public class SystemDemo {
     5     private static final String LINE_SEPARATOR=System.getProperty("line.separator");
     6     public static void main(String[] args){
     7         currentTime();
     8         getProperties();
     9         demo();
    10     }
    11 
    12     public static void currentTime() {
    13         //获取现在的时间毫秒值,起点是1970.1.1
    14         long l=System.currentTimeMillis();
    15         System.out.println(l);
    16     }
    17 
    18     public static void getProperties() {
    19         /*System.getProperties()获取当前系统的有关信息,返回一个Properties类型数据
    20          * !!!该类型是HashTable的子类,也就是键值对组合,并且键和值必须是String类型,所以不使用泛型 
    21          * !!!注意Properties集合遍历时,使用的是自身的方法而不是继承的方法
    22          */
    23         //除了可以使用系统提供的信息,还可以自己添加一些信息,是全局性的,方便使用
    24         System.setProperty("myKey", "myValue");
    25         Properties prop=System.getProperties();
    26         Set<String> nameSet=prop.stringPropertyNames();
    27         for(String name:nameSet){
    28             String value=prop.getProperty(name);
    29             System.out.println(name+":"+value);
    30         }
    31     }
    32 
    33     public static void demo() {
    34         /*在不同系统下,有一些地方是不同的,例如换行符,windows下是
    ,而UNIX下是
    
    35          * 如果将文件中使用的换行符全都固定写法,则更换系统时可能出现问题
    36          * Java在运行时会判断系统,并获取系统信息,可以用这些信息来替屏蔽掉系统,提高兼容性
    37          */
    38         System.out.println("hello"+LINE_SEPARATOR+"world");
    39     }
    40 }
    System示例

    2、Runntime:典型的单例类,私有构造器且有非静态方法,

    3、Math:执行数学运算的类,Random:随机数发生器类。Math中也有生成随机数的方法,但是只能是double类型,而Random类功能更强大。

    4、Date类是日期类,但是其中很多方法已经过时,转而使用Calender类。DateFormat抽象类用于格式化一个日期/时间的格式,但是其中有静态工厂方法可以获取实例对象;也可以用simpleDateFormat类来进行自定义格式化。

     1 import java.text.DateFormat;
     2 import java.text.ParseException;
     3 import java.util.Date;
     4 
     5 public class DateDemo {
     6     public static void main(String[] args) throws ParseException{
     7         demo();
     8         FormatDemo();
     9         parseDemo();
    10         CalenderDemo();
    11     }
    12 
    13     public static void demo() {
    14         long now=System.currentTimeMillis();
    15         Date d=new Date(now);
    16         System.out.println(d);
    17     }
    18 
    19     public static void FormatDemo() {
    20         //日期/时间的格式化使用的是DateFormat类,抽象类,不能直接实例化
    21         long now=System.currentTimeMillis();
    22         Date d=new Date(now);
    23         //!!!传入不同的参数对应不同的格式,解析时该格式的对象只能解析对应的字符串,见下
    24         DateFormat df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
    25         String date=df.format(d);
    26         System.out.println(date);
    27     }
    28 
    29     public static void parseDemo() throws ParseException {
    30         //还可以将一个表示日期时间的字符串转换为Date对象,默认只能解析以-分隔的
    31         String str_date="2105年4月25日";
    32         //!!!这里的参数是年/月/日分隔的时间格式,所以可以解析这种格式的 字符串
    33         DateFormat df=DateFormat.getDateInstance(DateFormat.LONG);
    34         Date date=df.parse(str_date);
    35         System.out.println(date);
    36     }
    37 
    38     public static void CalenderDemo() {
    39         //Calender类是将日期以键值对保存,且是字符串格式
    40     }
    41 }
    时间有关类演示
  • 相关阅读:
    python递归函数
    python全局替换文件内容脚本第1版
    python的if判断补充
    python装饰器
    python函数作用域
    python函数基础
    python文件操作
    ASCII、Unicode和UTF-8编码的区别
    python基础(二)----数据类型
    python基础第一章
  • 原文地址:https://www.cnblogs.com/songfeilong2325/p/4448969.html
Copyright © 2011-2022 走看看