4,集合

2018-10-08  本文已影响13人  旅程中

集合
List
Eg:

List<Notice> noticeList = new ArrayList<Notice>();
        
        noticeList.add(new Notice(1, "旅程1", new Date()));
        noticeList.add(new Notice(2, "旅程2", new Date()));
        noticeList.add(new Notice(3, "旅程3", new Date()));
        
        //删除元素
        noticeList.remove(2);
        
        for(int i = 0;i < noticeList.size(); i++) {
            System.out.println(noticeList.get(i).getName());
        }

Set:无序且不可重复
Hashset:
是set一个重要实现类,只允许一个null元素

迭代器
//List ,Set ,Map操作都可以用迭代器

        Iterator<String> iterator = set.iterator();
        
        //遍历迭代器输出元素 contains判断元素是否相等
        while(iterator.hasNext()) {
            if(set.contains("4444")) {
                System.out.println(1);
                break;
            }
            System.out.println(iterator.next());
        }

Map:

    // TODO Auto-generated method stub
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        
        
        for(String testString : map.keySet()) {
            //boolean isEmpty()   长度为0返回true否则false
            //System.out.println(testString);
        }
        
         //Map.Entry对象  推荐使用 ,其实就是get和set的应用,符合面向对象,取key和values
        
        Set<Map.Entry<String, String>> set = map.entrySet();
        
        Iterator<Map.Entry<String, String>> iterator = set.iterator();
        
        while(iterator.hasNext()) {
            System.out.println(iterator.next().getValue());
        }

Quene集合

上一篇 下一篇

猜你喜欢

热点阅读