zoukankan      html  css  js  c++  java
  • 在集合中使用泛型

    1 保证集合中元素类型的一致

    package lianxi3;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.junit.Test;
    
    public class TestGeneric {
    @Test  //在集合中使用泛型
       public void test1(){
          List<String> list = new ArrayList<String>();
          list.add("f");
          list.add("sfd");
          list.add("bb");
          list.add(new String("sf"));
          list.add(new String("ga"));
          Iterator<String> ite = list.iterator();
          while(ite.hasNext()){
              System.out.println(ite.next());
          }
    }
    @Test  //在Map中使用泛型
    public void test2(){
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(33, "safd");
        map.put(23, "sasdf");
        map.put(45, "ssd");
        map.put(90, "vfd");
        Set<Map.Entry<Integer,String>> set = map.entrySet();
        for(Map.Entry<Integer,String> o:set){
            System.out.println(o.getKey()+"---->"+o.getValue());
        }
    }
    }

    结果:

    f
    sfd
    bb
    sf
    ga
    33---->safd
    23---->sasdf
    90---->vfd
    45---->ssd

  • 相关阅读:
    MySQL之字符集
    PHP7.0-PHP7.3新特性与变更
    MySQL之开发规范
    php框架之thinkphp
    MySQL之日期时间类型
    php扩展之Yar
    XAMPP支持多PHP版本
    MySQL之执行流程
    RabbitMQ之php-amqplib使用
    (转)YAML最最基础语法
  • 原文地址:https://www.cnblogs.com/yjtm53/p/4150126.html
Copyright © 2011-2022 走看看