集合详解(一)----Collection和Map接口

来源:互联网 发布:linux修改ssh端口号 编辑:程序博客网 时间:2024/06/10 13:40

  • Collection
    • List
    • Set
  • Map

Collection


    在我们编程的时候,有时候需要集中存放多个数据,可以用数组来保存多个数据,但是数组的长度是不可变的,一旦数组的长度确定了之后就无法再改变,如果要保存可变长度的数据的话,数组肯定是不行的了。而且数组也无法保存具有一定关联的数据,比如:数学–80,英语–50。为了可以保存上面的这些信息,java提供了集合类,主要是负责保存、盛装数据的。因此集合相当于一个容器类。
    集合类有两个派生类,CollectionMap,本篇文章主要讲解Collection接口
这里写图片描述

package java.util;public interface Collection<E> extends Iterable<E> {    // Query Operations    //查询的一些方法    int size();    boolean isEmpty();    boolean contains(Object o);    Iterator<E> iterator();    Object[] toArray();    <T> T[] toArray(T[] a);    // Modification Operations    //修改的一些方法    boolean add(E e);    boolean remove(Object o);    // Bulk Operations    //一些批量操作的方法    boolean containsAll(Collection<?> c);    boolean addAll(Collection<? extends E> c);    boolean removeAll(Collection<?> c);    boolean retainAll(Collection<?> c);    void clear();    // Comparison and hashing    //和hash对比      boolean equals(Object o);    int hashCode();

    

List

    List的长度可变。
    List集合像一个数组,是有序的。

0 1 2 3 4 5 6 ele1 ele2 ele3 ele4 ele5 ele6 ele7

    
    List集合的一些基本操作

List<String>    list    =    new    ArrayList<String>();   list.add("testone");   list.add(“testtwo”);   //List遍历//第一种:这种方式在循环执行过程中会进行数据锁定,性能稍差,同时,如果你想在寻欢过程中去掉某个元素,只能调用it.remove方法,不能使list.remove方法,否则一定出现并发访问的错误.  for(Iterator<String> it = ist.iterator(); it.hasNext();)    {          ....      }   //第二种:内部调用第一种,换汤不换药,因此比Iterator慢,这种循环方式还有其他限制,不建议使用它。 for(String data:list)    {          .....      }   //第三种:内部不锁定,效率最高,但是当写多线程时要考虑并发操作的问题。for(int i=0;i<list.size();i++)    {          A a=list.get(i);          ...      }   

    

Set

    Set集合是无序的,元元素不允许重复。

这里写图片描述

HashSet h=new HashSet();h.add("1st");h.add("2nd");h.add(new Integer(3));h.add(new Double(4.0));h.add("2nd");            //重复元素,未被添加h.add(new Integer(3));      //重复元素,未被添加h.add(new Date());System.out.println("开始:size="+h.size());Iterator it=h.iterator();while(it.hasNext()){    Object o=it.next();    System.out.println(o);}h.remove("2nd");System.out.println("移除元素后:size="+h.size());System.out.println(h);

    

Map


    Map是java集合的另一个根接口,下图中是Map体系中一些常用的实现类。

这里写图片描述

    map也像一个罐子,不过在map中存储的时候都是以键值对的方式存储的(key-value方式)。存储的时候,key值是不能重复的,相当于索引,而value值是可以重复的。查询value值时通过key进行查询。

这里写图片描述

package com.jackey.topic;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;//循环遍历map的方法public class CircleMap{    public static void main(String[] args) {      Map<String, Integer> tempMap = new HashMap<String, Integer>();      tempMap.put("a", 1);      tempMap.put("b", 2);      tempMap.put("c", 3);      // JDK1.4中      // 遍历方法一 hashmap entrySet() 遍历      System.out.println("方法一");      Iterator it = tempMap.entrySet().iterator();      while (it.hasNext()) {           Map.Entry entry = (Map.Entry) it.next();           Object key = entry.getKey();           Object value = entry.getValue();           System.out.println("key=" + key + " value=" + value);      }      System.out.println("");      // JDK1.5中,应用新特性For-Each循环      // 遍历方法二      System.out.println("方法二");      for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {           String key = entry.getKey().toString();           String value = entry.getValue().toString();           System.out.println("key=" + key + " value=" + value);      }      System.out.println("");      // 遍历方法三 hashmap keySet() 遍历      System.out.println("方法三");      for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {           Object obj = i.next();           System.out.println(obj);// 循环输出key           System.out.println("key=" + obj + " value=" + tempMap.get(obj));      }      for (Iterator i = tempMap.values().iterator(); i.hasNext();) {           Object obj = i.next();           System.out.println(obj);// 循环输出value      }      System.out.println("");      // 遍历方法四 treemap keySet()遍历      System.out.println("方法四");      for (Object o : tempMap.keySet()) {          System.out.println("key=" + o + " value=" + tempMap.get(o));      }      System.out.println("11111");      // java如何遍历Map <String, ArrayList> map = new HashMap <String,      // ArrayList>();      System.out.println("java  遍历Map <String, ArrayList> map = new HashMap<String, ArrayList>();");      Map<String, ArrayList> map = new HashMap<String, ArrayList>();      Set<String> keys = map.keySet();      Iterator<String> iterator = keys.iterator();      while (iterator.hasNext()) {           String key = iterator.next();           ArrayList arrayList = map.get(key);           for (Object o : arrayList) {               System.out.println(o + "遍历过程");           }      }      System.out.println("2222");      Map<String, List> mapList = new HashMap<String, List>();      for (Map.Entry entry : mapList.entrySet()) {           String key = entry.getKey().toString();           List<String> values = (List) entry.getValue();           for (String value : values) {               System.out.println(key + " --> " + value);           }      }    }}
4 2
原创粉丝点击