ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 泛型和常见集合框架

2022-05-08 17:03:12  阅读:149  来源: 互联网

标签:Map java map 集合 泛型 new public


1. 泛型 Generic

1.1 泛型的安全机制

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

泛型的书写格式 :

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

    • 安全性提高了

    • 程序的代码量减少

    • 避免了类型的强制转换

    • 程序的问题,由运行时期,提前到编译时期

1.2 泛型中的 E 问题

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

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

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

1.3 自定义泛型类

/**
* 定义类,类名叫工厂
* 自定义泛型类
* Factory<什么都可以写> 只是变量名而已
*/
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类对象
   // Factory factory = new Factory();//没有泛型,QQ就是Object

   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> {

   /*
    * 静态方法
    * Q是非静态的, Q的数据类型,是new的时候指定的
    *
    * 静态方法参数中的泛型,不能和类一样
    * 静态方法的泛型,需要在方法上单独定义
    * 写在返回值类型的前面
    */
   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 泛型限定

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

  • <? extends Company> 传递类型可以是Company或者是他的子类

  • <? extends E>传递E类型或者是E的子类,泛型上限限定

  • <? super 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个对象,一个对象称为键(Key),一个对象称为值(Value)

在一个Map的集合容器中,键保证唯一性,不包含重复键,每个键只能对应一个值

2.1 Map接口方法

  • V put(K,V)存储键值对,存储重复键,返回被覆盖之前的值

  • V get(K)通过键获取值,参数传递键,找这个键对应的值,没有这个键返回null

  • boolean containsKey(K)判断集合是否包含这个键,包含返回true

  • boolean containsValue(V)判断集合是否包含这个值,包含返回true

  • int size() 返回集合长度,Map集合中键值对的个数

  • V remove(K)移除指定的键值对,返回被移除之前的值

  • Collection<V> values() Map集合中的所有的值拿出,存储到Collection集合

/*boolean containsKey(K)判断集合是否包含这个键,包含返回true
- boolean containsValue(V)判断集合是否包含这个值,包含返回true
- int size() 返回集合长度,Map集合中键值对的个数
- V remove(K)移除指定的键值对,返回被移除之前的值
- Collection<V> 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集合的遍历-键找值

  • 实现思想 :

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

    • 遍历Set集合

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

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

2.3 Map集合的遍历-键值对映射关系

  • 实现思想 :

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

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

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

    • 遍历Set集合

    • 取出Set集合的元素

      • 是Map.Entry接口对象

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

3. HashMap

  • HashMap集合特点

    • 是哈希表结构

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

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

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

  /**
    * HashMap集合
    * 键是Person,值是String
    */
   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);
  }

   /**
    * HashMap集合
    * 键是字符串,值是Person
    */
   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);

       //Set<String> set = map.keySet();
       for(String key : map.keySet()){
           //Person person = map.get(key);
           System.out.println(key+"==="+map.get(key));
      }
       System.out.println("==============");

       //Set<Map.Entry<String,Person>> set = map.entrySet();
       for(Map.Entry<String,Person> entry : map.entrySet()){
           System.out.println(entry.getKey()+"==="+entry.getValue());
      }

4. LinkedHashMap

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

5. Hashtable集合类

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

  • Hashtable类的特点

    • 底层数据结构是哈希表

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

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

6. Vector集合类

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

  • Vector类的特点

    • 底层实现结构是数组

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

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

7. TreeMap集合

  • TreeMap集合的特点

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

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

    • 排序依据 :

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

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

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

 /**
    * TreeMap集合存储对象
    * Student作为键,字符串是值
    * 自定义的比较器排序
    */
   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);
  }

   /**
    * TreeMap集合存储对象
    * Person作为键,字符串是值
    */
   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);
  }
/**
* 自定义的比较器,实现接口 Comparator
*/
class MyCom implements Comparator<Student>{
   /**
    * 方法compare 是TreeMap调用
    * 传递参数,后来的对象传递到s1, 已经有的对象传递到s2
    */
  public int compare(Student s1, Student s2){
      return s1.getAge() - s2.getAge();
  }

}
 /**
    * 进行比较:
    *   compareTo方法由,集合TreeMap调用
    *   传递相关的参数 集合中后来的对象是this,先来的对象是参数 p
    */
   public int compareTo(Person p){
       return this.age - p.age;
  }

8. Properties

  • Properties集合特点

    • 继承Hashtable,实现Map接口

    • 底层是哈希表结构

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

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

    • 集合有自己的特有方法

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

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

 /**
    * 集合遍历
    *   Properties类的方法 stringPropertyNames() [等效于map.keySet()] 返回Set集合
    *   Set集合存储的是 Properties集合的所有键
    */
   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));
      }
  }

   /**
    * 集合取出元素
    * Properties集合取出方法 getProperty(String 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);
  }

   /**
    * 集合存储键值对
    * Map接口,存储方法put
    * Properties集合存储方法 setProperty(String key,String 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);
  }

标签:Map,java,map,集合,泛型,new,public
来源: https://www.cnblogs.com/Ziper1076443011/p/16246041.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有