java 泛型 和集合容器(Java generics and collection containers)

1. 泛型

1.1 泛型的安全机制

泛型作用 : 强制了集合存储固定的数据类型,例如就不会存在一个集合又存字符串又存数值类型。

泛型作用 : 强制了集合存储固定的数据类型,例如就不会存在一个集合又存字符串又存数值类型。

泛型的书写格式 :

集合类<存储的数据类型>  变量名 = new 集合类<存储的数据类型>();    //  类型可以不写:钻石操作符

使用泛型的好处 :

  • 安全性提高
  • 代码量减少
  • 避免了类型的强制转换
  • 程序的问题,由运行时期,提前到编译时期

1.2 泛型中的 E 问题

E没有什么实际价值,只是一个变量而已

特殊 : 等待接收指定的数据类型

ArrayList<E>//创建对象ArrayList<String> al = new ArrayList<String>();   //  E 不再是E了,变成String

1.3 自定义泛型类

public class Factory<QQ> {    private QQ q;    public void setQ(QQ q){  this.q = q;}    public QQ getQ(){ return q; }}
public static void main(String[] args) {    Factory<String> factory = new Factory<String>();    factory.setQ("abc");    String s = factory.getQ();    System.out.println(s);    Factory<Double> factory2 = new Factory<Double>();    factory2.setQ(1.5);    Double q = factory2.getQ();    System.out.println(q);}

1.4 泛型方法

public class Factory<Q> {    public static<T> void staticMethod(T q){        System.out.println(q);    }    public void print(Q q){        System.out.println(q);    }}

1.5 泛型接口

  • 实现类实现接口,不实现泛型
  • 实现类实现接口,同时指定泛型
//泛型接口public interface Inter <T> {    public abstract void inter(T t);}/** * 实现接口,不理会泛型 * 对象创建的时候,指定类型 */public class InterImpl<T> implements Inter<T>{    public void inter(T t){        System.out.println(t);    }}/** * 实现接口,同时指定泛型 */public class InterImpl2 implements Inter<String> {    public void inter(String s) {        System.out.println("s=="+s);    }}public class GenericTest {    public static void main(String[] args) {        Inter<String> in = new InterImpl<String>();        in.inter("ok");        Inter in2 = new InterImpl2();        in2.inter("kkk");    }}

1.6 泛型通配符

//泛型的通配符public class GenericTest {    public static void main(String[] args) {        List<String> stringList = new ArrayList<String>();        stringList.add("abc");        stringList.add("bbc");        List<Integer> integerList =  new ArrayList<Integer>();        integerList.add(1);        integerList.add(2);        each(stringList);        each(integerList);    }    /**     * 定义方法,可以同时迭代器 遍历这两个集合     * 方法的参数,是要遍历的集合,不确定是哪个集合     * 定义参数,写接口类型,不要写实现类     */    public static void each(List<?> list){        Iterator<?> it = list.iterator();        while (it.hasNext()){            Object obj = it.next();            System.out.println(obj);        }    }}

1.7 泛型限定

泛型限定 : 限制的是数据类型

  • 传递类型可以是Company或者是他的子类
  • 传递E类型或者是 E 的子类,泛型上限限定
  • 传递E类型或者是E的父类,泛型下限限定
public static void main(String[] args) {    //创建集合,存储员工对象    //开发部的    List<Development> devList = new ArrayList<Development>();    //存储开发部员工对象    Development d1 = new Development();    d1.setName("张三");    d1.setId("开发部001");    Development d2 = new Development();    d2.setName("张三2");    d2.setId("开发部002");    devList.add(d1);    devList.add(d2);    //财务部集合    List<Financial> finList = new ArrayList<Financial>();    Financial f1 = new Financial();    f1.setName("李四");    f1.setId("财务部001");    Financial f2 = new Financial();    f2.setName("李四2");    f2.setId("财务部002");    finList.add(f1);    finList.add(f2);    System.out.println(devList);    System.out.println(finList);    each(devList);    each(finList);    //        List<Integer> integerList = new ArrayList<>();    //        integerList.add(1);    //        each(integerList);}/**     * 要求 : 定义方法     * 同时遍历2个集合     * 遍历的同时取出集合元素,调用方法work()     * ? 接收任何一个类型     * 只能接收 Company和子类对象     * 明确父类,不能明确子类     */public static void each(List<? extends Company> list){    Iterator<? extends Company> it = list.iterator();    while (it.hasNext()){        //取出元素        Company obj =it.next();        obj.work();    }}

2 Map集合

java.util.Map接口,是双列集合的顶级接口.在一个Map的集合容器中,键保证唯一性,不包含重复键,每个键只能对应一个值

2.1 Map接口方法

  • boolean containsValue(V)判断集合是否包含这个值,包含返回true
  • V remove(K)移除指定的键值对,返回被移除之前的值
  • Collection values() Map集合中的所有的值拿出,存储到Collection集合
public static void mapMethod(){    //创建集合,键是整数,值是String    Map<Integer,String> map = new HashMap<Integer, String>();    map.put(1,"a");    map.put(2,"b");    map.put(3,"c");    map.put(4,"d");    map.put(5,"e");    //boolean containsKey(K)判断集合是否包含这个键,包含返回true    boolean b = map.containsKey(1);    System.out.println("集合中包含键:"+b);    //boolean containsValue(V)判断集合是否包含这个值,包含返回true    b = map.containsValue("c");    System.out.println("集合中包含值:"+b);    //size()返回集合的长度    int size = map.size();    System.out.println("集合长度:"+size);    //V remove(K)移除指定的键值对,返回被移除之前的值    String value =  map.remove(1);    System.out.println("被删除之前的:"+value);    System.out.println(map);    //Collection<V> values() Map集合中的所有的值拿出,存储到Collection集合    Collection<String> coll =  map.values();    for(String s : coll){        System.out.println(s);    }}

2.2 Map集合的遍历

2.2.1 键找值

  • 实现思想 :

    Map接口定义了方法 keySet() 所有的键,存储到Set集合

    遍历Set集合

    取出Set集合元素 Set集合的元素是Map集合的键

    Map集合方法get()传递键获取值

  • Map接口定义了方法 keySet() 所有的键,存储到Set集合
  • 遍历Set集合
  • 取出Set集合元素 Set集合的元素是Map集合的键
  • Map集合方法get()传递键获取值

2.2.2 键值对映射关系

  • 实现思想 :

    Map接口的方法 Set< Map.Entry > entrySet()

    方法返回Set集合,集合中存储的元素,比较特别

    存储的是Map集合中,键值对映射关系的对象 , 内部接口 Map.Entry

    遍历Set集合

    取出Set集合的元素

    是Map.Entry接口对象

    接口的对象方法: getKey() ,getValue()

  • Map接口的方法 Set< Map.Entry > entrySet()

    方法返回Set集合,集合中存储的元素,比较特别

    存储的是Map集合中,键值对映射关系的对象 , 内部接口 Map.Entry

  • 方法返回Set集合,集合中存储的元素,比较特别
  • 存储的是Map集合中,键值对映射关系的对象 , 内部接口 Map.Entry
  • 遍历Set集合
  • 取出Set集合的元素

    是Map.Entry接口对象

    接口的对象方法: getKey() ,getValue()

  • 是Map.Entry接口对象
  • 接口的对象方法: getKey() ,getValue()

2.3. HashMap

  • 特点:

    哈希表结构

    保证键唯一性,用于键的对象,必须重写hashCode,equals方法

    线程不安全集合,运行速度快

    合运行使用null,作为键或者值

  • 哈希表结构
  • 保证键唯一性,用于键的对象,必须重写hashCode,equals方法
  • 线程不安全集合,运行速度快
  • 合运行使用null,作为键或者值
   public static void hashMap2(){        Map<Person,String> map = new HashMap<Person, String>();        map.put(new Person("a",20),"广东");        map.put(new Person("b",22),"香港");        map.put(new Person("b",22),"贵港");        map.put(new Person("c",24),"澳门");        map.put(new Person("d",26),"深圳");        System.out.println("map = " + map);    }    public static void hashMap1(){        Map<String, Person> map =  new HashMap<String, Person>();        map.put("a",new Person("张三",20));        map.put("b",new Person("张三",20));        map.put("c",new Person("张三",20));        map.put(null,null);        for(String key : map.keySet()){            System.out.println(key+"==="+map.get(key));        }               for(Map.Entry<String,Person> entry : map.entrySet()){            System.out.println(entry.getKey()+"==="+entry.getValue());        }

2.4 LinkedHashMap

LinkedHashMap继承HashMap实现Map接口,LinkedHashMap底层实现原理是哈希表,双向链,存取有序. 其它的特性和父类HashMap一样.

2.5 Hashtable

Map接口的实现类Hashtable, Hashtable类诞生于JDK1.0版本, Map接口诞生于JDK1.2版本. Hashtable类从JDK1.2开始,改进为实现Map接口

  • Hashtable类的特点

    底层数据结构是哈希表

    线程安全的,运行速度慢,被更加先进的HashMap取代

    不允许null值,null键, 存储null直接抛出空指针异常

  • 底层数据结构是哈希表
  • 线程安全的,运行速度慢,被更加先进的HashMap取代
  • 不允许null值,null键, 存储null直接抛出空指针异常

2.6 Vector集合类

List接口的实现Vector,命运和Hashtable一样.

  • Vector类的特点

    底层实现结构是数组

    数组的默认容量是10,每次扩容是原来的长度*2

    线程安全,运行速度慢,被ArrayList取代

  • 底层实现结构是数组
  • 数组的默认容量是10,每次扩容是原来的长度*2
  • 线程安全,运行速度慢,被ArrayList取代

2.7 TreeMap集合

  • TreeMap集合的特点

    底层实现是红黑树结构 (添加查询速度比较快)

    存储到TreeMap中元素,对键进行排序

    排序依据 :

    对象的自然顺序,作为键的对象,实现了接口Comparable

    自己提供比较器,实现接口Comparator,优先级高

    线程不安全的,运行速度快

  • 底层实现是红黑树结构 (添加查询速度比较快)
  • 存储到TreeMap中元素,对键进行排序
  • 排序依据 :

    对象的自然顺序,作为键的对象,实现了接口Comparable

    自己提供比较器,实现接口Comparator,优先级高

  • 对象的自然顺序,作为键的对象,实现了接口Comparable
  • 自己提供比较器,实现接口Comparator,优先级高
  • 线程不安全的,运行速度快
    public static void treeMap2(){        Map<Student,String> map = new TreeMap<Student, String>( new MyCom() );        map.put(new Student("a",20),"广东");        map.put(new Student("b",19),"广西");        System.out.println("map = " + map);    }    public static void treeMap1(){        Map<Person,String> map = new TreeMap<Person, String>();        map.put(new Person("a",20),"广东");        map.put(new Person("b",19),"广西");        System.out.println("map = " + map);    }
class MyCom implements Comparator<Student>{   public int compare(Student s1, Student s2){       return s1.getAge() - s2.getAge();   }}
   public int compareTo(Person p){        return this.age - p.age;   }

2.8 ConcurrentHashMap

ConcurrentHashMap类本质上Map集合,键值对的集合.使用方式和HashMap没有区别.

凡是对于此Map集合的操作,不去修改里面的元素,不会锁定,线程安全

2.9 Properties

  • Properties集合特点

    继承Hashtable,实现Map接口

    底层是哈希表结构

    线程是安全的,运行速度慢

    集合没有泛型的写法,键和值的数据类型锁定为String类型

    集合有自己的特有方法

    此集合可以和IO流对象结合使用,实现数据的持久存储

    方法和IO相关 : load(输入流)

  • 继承Hashtable,实现Map接口
  • 底层是哈希表结构
  • 线程是安全的,运行速度慢
  • 集合没有泛型的写法,键和值的数据类型锁定为String类型
  • 集合有自己的特有方法
  • 此集合可以和IO流对象结合使用,实现数据的持久存储
  • 方法和IO相关 : load(输入流)
    public static void prop3(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        Set<String> set = prop.stringPropertyNames();        for(String key : set){            System.out.println(key +"=="+ prop.getProperty(key));        }    }    public static void prop2(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        System.out.println(prop);        String value = prop.getProperty("a");        System.out.println(value);    }    public static void prop1(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        System.out.println(prop);    }
————————

1. Generic

1.1 security mechanism of generics

Generic function: it forces the collection to store fixed data types. For example, there will be no collection with string and numeric types.

Generic function: it forces the collection to store fixed data types. For example, there will be no collection with string and numeric types.

Generic writing format:

集合类<存储的数据类型>  变量名 = new 集合类<存储的数据类型>();    //  类型可以不写:钻石操作符

Benefits of using generics:

  • Improved safety
  • Code reduction
  • Type casting is avoided
  • The problem of the program is advanced from the running time to the compiling time

1.2 e problem in generics

E has no real value, just a variable

Special: waiting to receive the specified data type

ArrayList<E>//创建对象ArrayList<String> al = new ArrayList<String>();   //  E 不再是E了,变成String

1.3 custom generic classes

public class Factory<QQ> {    private QQ q;    public void setQ(QQ q){  this.q = q;}    public QQ getQ(){ return q; }}
public static void main(String[] args) {    Factory<String> factory = new Factory<String>();    factory.setQ("abc");    String s = factory.getQ();    System.out.println(s);    Factory<Double> factory2 = new Factory<Double>();    factory2.setQ(1.5);    Double q = factory2.getQ();    System.out.println(q);}

1.4 generic methods

public class Factory<Q> {    public static<T> void staticMethod(T q){        System.out.println(q);    }    public void print(Q q){        System.out.println(q);    }}

1.5 generic interface

  • Implement classes, implement interfaces, and do not implement generics
  • The implementation class implements the interface and specifies the generic type
//泛型接口public interface Inter <T> {    public abstract void inter(T t);}/** * 实现接口,不理会泛型 * 对象创建的时候,指定类型 */public class InterImpl<T> implements Inter<T>{    public void inter(T t){        System.out.println(t);    }}/** * 实现接口,同时指定泛型 */public class InterImpl2 implements Inter<String> {    public void inter(String s) {        System.out.println("s=="+s);    }}public class GenericTest {    public static void main(String[] args) {        Inter<String> in = new InterImpl<String>();        in.inter("ok");        Inter in2 = new InterImpl2();        in2.inter("kkk");    }}

1.6 generic wildcards

//泛型的通配符public class GenericTest {    public static void main(String[] args) {        List<String> stringList = new ArrayList<String>();        stringList.add("abc");        stringList.add("bbc");        List<Integer> integerList =  new ArrayList<Integer>();        integerList.add(1);        integerList.add(2);        each(stringList);        each(integerList);    }    /**     * 定义方法,可以同时迭代器 遍历这两个集合     * 方法的参数,是要遍历的集合,不确定是哪个集合     * 定义参数,写接口类型,不要写实现类     */    public static void each(List<?> list){        Iterator<?> it = list.iterator();        while (it.hasNext()){            Object obj = it.next();            System.out.println(obj);        }    }}

1.7 generic qualification

Generic qualification: data types are restricted

  • 传递类型可以是Company或者是他的子类
  • pass E type or subclass of E, and the upper limit of generic type is limited
  • pass the type E or the parent class of E, and the lower limit of generic type is limited
public static void main(String[] args) {    //创建集合,存储员工对象    //开发部的    List<Development> devList = new ArrayList<Development>();    //存储开发部员工对象    Development d1 = new Development();    d1.setName("张三");    d1.setId("开发部001");    Development d2 = new Development();    d2.setName("张三2");    d2.setId("开发部002");    devList.add(d1);    devList.add(d2);    //财务部集合    List<Financial> finList = new ArrayList<Financial>();    Financial f1 = new Financial();    f1.setName("李四");    f1.setId("财务部001");    Financial f2 = new Financial();    f2.setName("李四2");    f2.setId("财务部002");    finList.add(f1);    finList.add(f2);    System.out.println(devList);    System.out.println(finList);    each(devList);    each(finList);    //        List<Integer> integerList = new ArrayList<>();    //        integerList.add(1);    //        each(integerList);}/**     * 要求 : 定义方法     * 同时遍历2个集合     * 遍历的同时取出集合元素,调用方法work()     * ? 接收任何一个类型     * 只能接收 Company和子类对象     * 明确父类,不能明确子类     */public static void each(List<? extends Company> list){    Iterator<? extends Company> it = list.iterator();    while (it.hasNext()){        //取出元素        Company obj =it.next();        obj.work();    }}

2 Map集合

java. util. Map interface is the top-level interface of double column collection In the collection container of a map, the key ensures uniqueness, does not contain duplicate keys, and each key can only correspond to one value

2.1 map interface method

  • boolean containsValue(V)判断集合是否包含这个值,包含返回true
  • V remove (k) removes the specified key value pair and returns the value before it is removed
  • Collection values() Map集合中的所有的值拿出,存储到Collection集合
public static void mapMethod(){    //创建集合,键是整数,值是String    Map<Integer,String> map = new HashMap<Integer, String>();    map.put(1,"a");    map.put(2,"b");    map.put(3,"c");    map.put(4,"d");    map.put(5,"e");    //boolean containsKey(K)判断集合是否包含这个键,包含返回true    boolean b = map.containsKey(1);    System.out.println("集合中包含键:"+b);    //boolean containsValue(V)判断集合是否包含这个值,包含返回true    b = map.containsValue("c");    System.out.println("集合中包含值:"+b);    //size()返回集合的长度    int size = map.size();    System.out.println("集合长度:"+size);    //V remove(K)移除指定的键值对,返回被移除之前的值    String value =  map.remove(1);    System.out.println("被删除之前的:"+value);    System.out.println(map);    //Collection<V> values() Map集合中的所有的值拿出,存储到Collection集合    Collection<String> coll =  map.values();    for(String s : coll){        System.out.println(s);    }}

2.2 traversal of map set

2.2.1 key value finding

  • Realization idea:
    The map interface defines all keys of the method keyset() and stores them in the set
    Traverse set set
    Take out the set set element. The set element is the key of the map set
    The map collection method get() passes the key to get the value
  • The map interface defines all keys of the method keyset() and stores them in the set
  • Traverse set set
  • Take out the set set element. The set element is the key of the map set
  • The map collection method get() passes the key to get the value

2.2.2 key value pair mapping relationship

  • Realization idea:
    Method set of map interface < map Entry > entrySet()
    Method returns a set set. The elements stored in the set are special
    It stores the object of key value pair mapping relationship in the map set, and the internal interface map Entry
    Traverse set set
    Take out the elements of the set
    It’s map Entry interface object
    Object method of interface: getkey(), getvalue()
  • Method set of map interface < map Entry > entrySet()
    Method returns a set set. The elements stored in the set are special
    It stores the object of key value pair mapping relationship in the map set, and the internal interface map Entry
  • Method returns a set set. The elements stored in the set are special
  • It stores the object of key value pair mapping relationship in the map set, and the internal interface map Entry
  • Traverse set set
  • Take out the elements of the set
    It’s map Entry interface object
    Object method of interface: getkey(), getvalue()
  • 是Map.Entry接口对象
  • 接口的对象方法: getKey() ,getValue()

2.3. HashMap

  • characteristic:
    Hash table structure
    To ensure key uniqueness, the hashcode and equals methods must be overridden for key objects
    Thread unsafe collection, fast running speed
    The combination run uses null as a key or value
  • Hash table structure
  • To ensure key uniqueness, the hashcode and equals methods must be overridden for key objects
  • Thread unsafe collection, fast running speed
  • The combination run uses null as a key or value
   public static void hashMap2(){        Map<Person,String> map = new HashMap<Person, String>();        map.put(new Person("a",20),"广东");        map.put(new Person("b",22),"香港");        map.put(new Person("b",22),"贵港");        map.put(new Person("c",24),"澳门");        map.put(new Person("d",26),"深圳");        System.out.println("map = " + map);    }    public static void hashMap1(){        Map<String, Person> map =  new HashMap<String, Person>();        map.put("a",new Person("张三",20));        map.put("b",new Person("张三",20));        map.put("c",new Person("张三",20));        map.put(null,null);        for(String key : map.keySet()){            System.out.println(key+"==="+map.get(key));        }               for(Map.Entry<String,Person> entry : map.entrySet()){            System.out.println(entry.getKey()+"==="+entry.getValue());        }

2.4 LinkedHashMap

LinkedHashMap inherits from HashMap to realize the map interface. The underlying implementation principle of LinkedHashMap is hash table, two-way Chain and orderly access Other features are the same as the parent HashMap

2.5 Hashtable

The implementation class of map interface is hashtable, which was born in jdk1 0 version, map interface was born in jdk1 2 version Hashtable class from jdk1 2. At the beginning, the improvement is to implement the map interface

  • Characteristics of hashtable class
    The underlying data structure is a hash table
    Thread safe, slow running speed, replaced by more advanced HashMap
    Null value and null key are not allowed. Null stored directly throws null pointer exception
  • The underlying data structure is a hash table
  • Thread safe, slow running speed, replaced by more advanced HashMap
  • Null value and null key are not allowed. Null stored directly throws null pointer exception

2.6 Vector集合类

The implementation of the list interface, vector, has the same fate as hashtable

  • Characteristics of vector class
    The underlying implementation structure is an array
    The default capacity of the array is 10, and each expansion is the original length * 2
    Thread safe, slow running speed, replaced by ArrayList
  • The underlying implementation structure is an array
  • The default capacity of the array is 10, and each expansion is the original length * 2
  • Thread safe, slow running speed, replaced by ArrayList

2.7 TreeMap集合

  • Characteristics of treemap set
    The bottom implementation is red black tree structure (adding query is faster)
    Elements stored in treemap to sort keys
    Sort by:
    The natural order of objects, as the key object, implements the interface comparable
    Provide the comparator and implement the interface comparator with high priority
    Thread is unsafe and runs fast
  • The bottom implementation is red black tree structure (adding query is faster)
  • Elements stored in treemap to sort keys
  • Sort by:
    The natural order of objects, as the key object, implements the interface comparable
    Provide the comparator and implement the interface comparator with high priority
  • The natural order of objects, as the key object, implements the interface comparable
  • Provide the comparator and implement the interface comparator with high priority
  • Thread is unsafe and runs fast
    public static void treeMap2(){        Map<Student,String> map = new TreeMap<Student, String>( new MyCom() );        map.put(new Student("a",20),"广东");        map.put(new Student("b",19),"广西");        System.out.println("map = " + map);    }    public static void treeMap1(){        Map<Person,String> map = new TreeMap<Person, String>();        map.put(new Person("a",20),"广东");        map.put(new Person("b",19),"广西");        System.out.println("map = " + map);    }
class MyCom implements Comparator<Student>{   public int compare(Student s1, Student s2){       return s1.getAge() - s2.getAge();   }}
   public int compareTo(Person p){        return this.age - p.age;   }

2.8 ConcurrentHashMap

The concurrenthashmap class is essentially a map collection, a collection of key value pairs The usage is no different from HashMap

For the operation of this map set, do not modify the elements in it, and it will not be locked, which is thread safe

2.9 Properties

  • Properties collection features
    Inherit hashtable and implement map interface
    The bottom layer is the hash table structure
    Threads are safe and slow
    The collection has no generic writing method, and the data types of keys and values are locked to string type
    Collections have their own unique methods
    This collection can be used in combination with IO stream objects to realize the persistent storage of data
    Method and IO related: load (input stream)
  • 继承Hashtable,实现Map接口
  • The bottom layer is the hash table structure
  • Threads are safe and slow
  • The collection has no generic writing method, and the data types of keys and values are locked to string type
  • Collections have their own unique methods
  • This collection can be used in combination with IO stream objects to realize the persistent storage of data
  • Method and IO related: load (input stream)
    public static void prop3(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        Set<String> set = prop.stringPropertyNames();        for(String key : set){            System.out.println(key +"=="+ prop.getProperty(key));        }    }    public static void prop2(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        System.out.println(prop);        String value = prop.getProperty("a");        System.out.println(value);    }    public static void prop1(){        Properties prop = new Properties();        prop.setProperty("a","1");        prop.setProperty("b","2");        prop.setProperty("c","3");        System.out.println(prop);    }