Java 类集框架(Set, List, Map)的使用

wangyunfei 7年前
   <p>虽然面试的要求是要知道HashMap是怎么实现的,令人望而却步,但搞明白它怎么用,就成功了一小半。</p>    <h3><strong>什么是类集框架</strong></h3>    <ul>     <li> <p>是一组类和接口</p> </li>     <li> <p>位于java.util包中</p> </li>     <li> <p>主要用于存储和管理对象</p> </li>     <li> <p>主要分为三大类--集合、列表和映射</p>      <ul>       <li> <p>集合中的对象不按特定的方式排序,并且没有重复对象。和数学中集合的概念类似。(如果添加了重复的对象,会怎样呢?并不会报错,但重复的元素只算一个)。</p> </li>       <li> <p>列表中的对象按照索引位置排序,可以有重复对象。</p> </li>       <li> <p>映射中的每一个元素包含一个键对象和一个值对象,即键值对。键不可以重复,值可以重复。</p> </li>      </ul> </li>    </ul>    <p>注意这些接口及其实现类的继承关系:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/07e3a6691f8cb480ce2a216f528909e6.png"></p>    <h3><strong>Collection 和 Iterator接口</strong></h3>    <p>Set 接口和 List 接口都是 Collection 的子接口,因此我们先看看Collection 接口中有什么方法:</p>    <table>     <thead>      <tr>       <th>方法名</th>       <th>作用</th>      </tr>     </thead>     <tbody>      <tr>       <td>boolean add(Object o)</td>       <td>向集合中加入一个对象</td>      </tr>      <tr>       <td>void clear()</td>       <td>删除集合中的所有对象</td>      </tr>      <tr>       <td>boolean isEmpty()</td>       <td>判断集合是否为空</td>      </tr>      <tr>       <td>remove(Object o)</td>       <td>从集合中删除一个对象的引用</td>      </tr>      <tr>       <td>int size()</td>       <td>返回集合中元素的数目</td>      </tr>     </tbody>    </table>    <p>Set有一个实现类,就是HashSet,它是Set中我们最常用的。下面我们举一个例子,在HashSet中使用这些方法。</p>    <p><strong>Set 和 HashSet的使用方法</strong></p>    <pre>  <code class="language-java">//导包时少用*,写明确一点,方便查错和阅读  import java.util.Set;  import java.util.HashSet;    public class Test {      public static void main(String args []) {          //把具体的实现类向上转型为接口类型,方便接收参数,即:          //HashSet<String> hashSet = new HashSet<String>();          //Set<String> set = hashSet;          Set<String> set = new HashSet<String>();          set.add("a");          set.add("b");          set.add("c");          set.add("d");          System.out.println("Size: " + set.size());            //重复添加元素,会怎样呢?           set.add("c");          System.out.println("Size with two c: " + set.size());            set.remove("a");          System.out.println("Size after remove: " + set.size());                    set.clear();          System.out.println("Size after clear: " + set.size());            System.out.println("Is empty: " + set.isEmpty());              }  }</code></pre>    <p>运行结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/caf958075962ada0c1272bee824bd028.png"></p>    <h3><strong>如何遍历 Set</strong></h3>    <p>集合是无序的,所以不能像数组或者List那样,根据下标输出其中的元素,因此我们需要使用迭代器 Iterator。</p>    <p>由最前面的图可以看出,它们之间的继承关系是:Iterator <-- Collection <-- Set <-- HashSet.</p>    <p>Iterator 的常用方法有:</p>    <ul>     <li>hasNext():判断迭代器当中,还有没有下一个元素</li>     <li>next():返回迭代器当中的下一个元素</li>    </ul>    <p>遍历Set的方法:</p>    <p>生成迭代器对象:Iterator it = set.iterator() ,这个迭代器对象可以把set中的元素都取出来。</p>    <p>iterator() 方法属于 collection 接口,所以 collection 的子接口 set 中, 必然有 iterator() 方法。</p>    <pre>  <code class="language-java">import java.util.Iterator;  import java.util.Set;  import java.util.HashSet;    public class Test {      public static void main(String args []) {          Set<String> set = new HashSet<String>();                  set.add("a");          set.add("b");          set.add("c");          set.add("d");                  Iterator<String> it = set.iterator();          while(it.hasNext())               System.out.println(it.next());              }  }</code></pre>    <p>运行结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/29797cdd61dab34986fd878ccb5f334c.png"></p>    <h3><strong>Map 和 HashMap的使用方法</strong></h3>    <p>由一开始的类图可以看出,Map并没有继承Collection接口,是一个单独的接口。</p>    <p>Map中的重要方法:</p>    <ul>     <li>put(K key, V value): 把键值对放入Map</li>     <li>get(Object key): 返回当前键对应的值</li>     <li>其他方法与Collection中类似,比如remove(), clear()</li>    </ul>    <p>一个简单的例子:</p>    <pre>  <code class="language-java">import java.util.Map;  import java.util.HashMap;    public class Test {      public static void main(String args []) {          Map<String, String> map = new HashMap<String, String>();          map.put("1", "a");          map.put("2", "b");          map.put("3", "c");          map.put("4", "d");          //如果有重复的键,后面的值会覆盖前面的          map.put("3", "e");            System.out.println(map.size());          System.out.println(map.get("3"));      }  }</code></pre>    <p>运行结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/45692c94db4a513714b5a7f2985f82a6.png"></p>    <p> </p>    <p>来自:http://www.jianshu.com/p/305c3ac8b17a</p>    <p> </p>