zoukankan      html  css  js  c++  java
  • map集合和set集合

    一、Vector
         1 、java中提供的最早的集合,一个动态数组,与ArrayList相似,但是两者不同:
              (1)Vector是同步访问的
              (2)Vector包含了很多传统的方法,这些方法不属于集合框架
              (3)Vextor主要用于实现不知道数组的大小,或者重视需要一个可以改变大小的数组的情况
          
         2 、枚举   Enumeration    迭代器的前身    与Iterator功能类似

    二、Set 接口   (是Collection的子接口  Collection是容器的意思)
         1、Set接口不能存储重复的元素,不按照元素存储顺序排列,Set接口有三个实现类,分别是                HashSet、LinkedHashSet、TreeSet,主要是HashSet
         2、
            创建对象:Set<String> s=new HashSet<String>();
            主要方法:
                  s.add();添加元素
            用增强for循环和迭代器对集合元素输出:
                     public static void main(String[] args){
                           Set<String> s=new HashSet<String>();//创建对象
                            s.add("aa");
                            s.add("bb");
                            s.add("cc");
                            //当出现重复的元素时,后边的覆盖前边的
                            s.add("aa");
                            //增强for循环          for(集合元素类型 给元素起的名:集合名称){}
                           for(String s1:s){
                             System.out.println(s1);   
                             }
                           //迭代器        对象s调用Iterator()方法创建了一个迭代器
                           Iterator(String) it=s.iterator();//迭代器(要迭代集合的数据类型)  迭代器对象=集合对象.iterator();
                           while(it.hasNext()){             //it.hasNext()判断集合中是否还有下一个元素
                               System.out.println(it.next());     //next()将元素提取出来
                               }
                       }
    三、Map接口  (不是Collection子类)
           1、定义:Collection的子类存储对象都是向集合中一个一个放,像Set<String> s=new HashSet<String>();
                    而Map接口是成对存储,包括key值和Value值,key和Value一一对应,可以通过键值获取值HashMap<String(key值),String(Value值)> map=new HashMap<String,String>();
           
           2、Map集合中不能包含重复键,但可以包含重复的值
          
           3、子类有HashMap和LinkedHashMap等
     
           4、主要方法:put(添加)   get(提取)  remove(key)(移除)
             public class MapDemo1 {
                           //map中put是添加值
             public static void main(String[] args){
                  Map<Integer,String> map=new HashMap<Integer, String>();
                  map.put(1,"aa");
                  map.put(2,"bb");
                  map.put(3,"cc");
                          //键值不能重复,如果重复就覆盖之前的值
                  map.put(3,"nn");
                  map.put(100,"100");
                  System.out.println(map.get(3));
                  System.out.println(map);
                  System.out.println(map.get(66));
                  System.out.println(map.remove(1));
                  System.out.println(map);

                 }
              }
             5、LinkedHashMap的打印出来的元素顺序和键入的顺序无关

                public class LinkedHashMapDemo {
                public static void main(String[] args){
                           //LinkedHadhMap的打印顺序是根据数据输入的先后顺序
                     LinkedHashMap<String,String> m=new LinkedHashMap<String,String>();
                     m.put("1","a");
                     m.put("2","b");
                     m.put("3","c");
                     m.put("5","d");
                     m.put("4","e");
                     System.out.println(m);
                  }
               }


             6、Map集合的遍历方式   keySet()和entrySet()
                 
                Map集合没有迭代器的接口,没有办法进行遍历,所以先映射进Set集合再进行迭代
                
                keySet()是只把键映射到Set集合,entrySet()是把键和值都映射到Set集合

                keySet()方法:
                         public class MapDemo2 {
                         public static void main(String[] args){
                             Map<String,String> map=new HashMap<String, String>();
                             map.put("北凉","徐凤年");
                             map.put("北凉1","温华");
                             map.put("北凉2","老黄");
                             Set<String> set=map.keySet();
                             System.out.println(set);
                             Iterator<String> it=set.iterator();
                             while (it.hasNext()){
                             String value=it.next();
                             System.out.println(map.get(value));
                         }
                             System.out.println("=========================");
                             for (String se:set) {
                             System.out.println(map.get(se));
                           }
                        }
                      }

                Map嵌套中使用entrySet()方法(也可使用keySet()方法):
                         public class MaoMapDemo {
                         public static void main(String[] args){
                               //定义Java班集合
                               HashMap<String,String> javas=new HashMap<String, String>();
                               //定义Hdoop班集合
                               HashMap<String,String> Hdoop=new HashMap<String,String>();
                               //向班级里添加学生
                               javas.put("1","徐凤年");
                               javas.put("2","小泥人");

                               Hdoop.put("1","徐龙象");
                               Hdoop.put("2","徐北枳");

                              //System.out.println(javas);
                              //System.out.println(Hdoop);

                              HashMap<String,HashMap<String,String>> aaa=new                           HashMap<String,HashMap<String,String>>();
                              aaa.put("java班",javas);
                              aaa.put("Hdoop班",Hdoop);
                             //System.out.println(aaa);
                             //entrySet1(aaa);
                             foreachdemo(aaa);
                            }
    //迭代器
                 //    public static void entrySet1(HashMap<String,HashMap<String,String>> aaa){
    //                                       //entrySet()将HashMap中值映射到set集合中,键值和值全都映射过去
    //                        Set<Entry<String,HashMap<String,String>>> classNameSet=aaa.entrySet();
    //                             System.out.println(classNameSet);
    //                //迭代选择
    //                        Iterator<Entry<String,HashMap<String,String>>> it=classNameSet.iterator();
    //                        while (it.hasNext()){
    //                           //System.out.println(it.next());
    //                     //next的类型为Entry<String,HashMap<String,String>>,next的值Hdoop班={1=徐龙象, 2=徐北枳}和java班={1=徐凤年, 2=小泥人}
    //                        Entry<String,HashMap<String,String>> next=it.next();
    //                             System.out.println(next);
    //                    //得到Hdoop这样的key值(班名)
    //                        String classNameKey=next.getKey();
    //                             System.out.println(classNameKey);
    //                    //得到每个班的人名集合
    //                        HashMap<String,String>  classMap=next.getValue();
    //                             System.out.println(classMap);
    //                    //人名集合是Map类型,再次映射到set集合
    //                        Set<Entry<String,String>> StudentSet=classMap.entrySet();
    //                    //System.out.println(StudentSet+"==============");
    //                    //对人名集合进行迭代
    //                        Iterator<Entry<String,String>> classit=StudentSet.iterator();
    //                        while(classit.hasNext()){
    //                             Entry<String,String> studentEntry=classit.next();
    //                             String numKey=studentEntry.getKey();
    //                             String numName=studentEntry.getValue();
    //                             System.out.println(numKey+"     "+numName);
    //                       }
    //                     }
    //
    //                   }




    //增强for循环
                    public static void foreachdemo(HashMap<String,HashMap<String,String>> aaa){
                            Set<Entry<String,HashMap<String,String>>> classIt=aaa.entrySet();
                                  System.out.println(classIt);
                            for (Entry<String,HashMap<String,String>> className:classIt) {
                                  System.out.println(className);
                            Entry<String,HashMap<String,String>> classNameAGe=className;
                                  System.out.println(classNameAGe);
                            String classKey=classNameAGe.getKey();
                            HashMap<String,String> classValue=classNameAGe.getValue();
                                  System.out.println(classValue);
                            Set<Entry<String,String>> StudentSet=classValue.entrySet();
                            for (Entry<String,String> sksdfk: StudentSet) {
                                  System.out.println(sksdfk);
                            //Entry<String,String> gggg=sksdfk;
                                String studentKey=sksdfk.getKey();
                                String studentName=sksdfk.getValue();
                                System.out.println(studentKey+ "     " +studentName);
                              }
                          }
                         }
                      }

    四 、集合的操作工具      shuffle   生成随机的序列
                      public static void main(String[] args){
                                   fun1();
                         }
                      public static void fun1(){
                            List<Integer> list=new ArrayList<Integer>();
                            list.add(1000000);
                            list.add(1);
                            list.add(2);
                            list.add(24);
                            list.add(198);
                            list.add(138478);
                            list.add(1342);
                              System.out.println(list);
                            //工具类方法shuffle() 对集合进行随机排列
                           Collections.shuffle(list);
                           System.out.println(list);
                          }
                      }








  • 相关阅读:
    字符串
    Flume集群搭建
    hbase测试搭建
    hadoop2.0测试环境安装
    kafka集群报错
    kafka集群安装
    hadoop环境配置
    zookeeper安装
    虚拟机时间同步14 Aug 04:09:18 ntpdate[2941]: no server suitable for synchronization found
    python numpy中数组.min()
  • 原文地址:https://www.cnblogs.com/fbbg/p/10623512.html
Copyright © 2011-2022 走看看