首页 > 其他 > 详细

集合

时间:2014-03-14 18:17:58      阅读:524      评论:0      收藏:0      [点我收藏+]

1.主要掌握5个接口:

Collection List Set Map Iterator Enumeration

2.不允许重复的子接口:Set

3.Set中常用的两个子类:散列存放的子类:HashSet

  顺序存放的子类TreeSet

4. Map接口

public Set<K> keySet() ,将Map中的所有key以Set集合的方式返回

public Set<Map.Entry><K,V>>entrySet(),将Map集合变为Set集合

Map的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.ylfeiu.utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
public class HashMapEcho {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "txt");
        map.put(2, "txt2");
        map.put(null, "ylfeiu");
        map.put(1, "hello");
 
        Set<Integer> set = map.keySet();
        Iterator<Integer> car = set.iterator();
 
        while (car.hasNext()) {
            Integer key = car.next();
            System.err.println(key + "------------>" + map.get(key));
        }
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
例子2
 
package com.ylfeiu.utils;
 
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
public class HashMapEcho2 {
    public static void main(String[] args) {
        Map<Integer, String> map = new Hashtable<>();
        map.put(1, "hello");
        map.put(2, "txt");
        map.put(2, "txt2");
        // map.put(null, "ylfeiu");//HashTable不能使用null做key
        map.put(1, "hello");
 
        Set<Map.Entry<Integer, String>> set = map.entrySet();//把Map.Entry当作一个对象
        Iterator<Map.Entry<Integer, String>> car = set.iterator();
 
        while (car.hasNext()) {
            Map.Entry<Integer, String> me = car.next();
            System.err.println(me.getKey() + "------------>" + me.getValue());
 
        }
 
    }
}

  

  

集合,布布扣,bubuko.com

集合

原文:http://www.cnblogs.com/ylfeiu/p/3599344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!