java集合

java中有几种常用的数据结构,主要分为Collection(Iterable注定可以使用迭代器)和map两个主要接口其主要的关系(继承关系)有:
Iterable---->Collection---->Collections
Iterable---->Collection---->List----->(Vector \ ArryList \ LinkedList)
Iterable---->Collection---->Set------>(HashSet \ LinkedHashSet \ SortedSet)
Map----->SortedMap------>TreeMap
Map------>HashMap

Map集合循环遍历的几种方式:

public class TestMap {

         public static void main(String[] args) {
              Map<String, Object> map = new HashMap<String, Object>();
              map.put("aaa", 111);
              map.put("bbb", 222);
              map.put("ccc", 333);
              map.put("ddd", 444);
            //方式一 通过Map.keySet()遍历key和value:"
            for(String key:map.keySet()){//keySet获取map集合key的集合  然后在遍历key即可
                String value = map.get(key).toString();//
                System.out.println("key:"+key+" vlaue:"+value);
            }
            //方式二  通过迭代器的方式
            Iterator<Entry<String, Object>> it = map.entrySet().iterator();
            while(it.hasNext()){
                Entry<String, Object> entry = it.next();
                System.out.println("key:"+entry.getKey()+"  key:"+entry.getValue());
            }
            //方式三 推荐,尤其是容量大时
            for (Map.Entry<String, Object> m : map.entrySet()) {
              System.out.println("key:" + m.getKey() + " value:" + m.getValue());
            }
            // 方式四:
            for(Object m:map.values()){
                System.out.println(m);
            }
       }
}

Map 子类HashMap、TreeSet中有 EntrySet内部类(父类实现了Set,拥有迭代器)
HashSet 底层是HashMap实现,TreeSet是TreeMap实现

数据存储的常用结构有:栈、队列、数组、链表和红黑树。

posted @ 2020-05-15 10:54  gsluofu  阅读(138)  评论(0编辑  收藏  举报