java collection 学习一

参考:原文地址

1.java collection overview


2.Map 接口 概览

3、常用接口方法使用示例

package com.interview.base.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MyCollectionUtil {
     public static void doSomething(Collection collection) {
            
            Iterator iterator = collection.iterator();
            while(iterator.hasNext()){
              Object object = iterator.next();

              //do something to object here...
            }
          }
     public static void main(String[] args) {
        
         String     anElement  = "an element";
         String     twoElement  = "an element";
         Collection setCollection = new HashSet();
         Collection listCollection = new ArrayList();
         
         boolean setChange = setCollection.add(anElement);
         boolean setChange2 = setCollection.add(anElement);
         System.out.println(setChange2);
         
         System.out.println("setCollection: "+ setCollection.size());
         
         System.out.println("contain test: " + setCollection.contains("aobo"));
         
         boolean  removeExist = setCollection.remove(anElement);
         boolean  removeNotExist = setCollection.remove("aobo");
         
         System.out.println("removeExist: " + removeExist);
         
         System.out.println("removeNotExist: " + removeNotExist);

         boolean listChange = listCollection.add(anElement);
         boolean listChange2 = listCollection.add(anElement);
         
         System.out.println("listCollection: "+ listCollection.size());
         
         
         // the first output method
         for(Object s : listCollection){
             System.out.println(s.toString());
         }
         
         //the secondn output  method
         Iterator iterator = listCollection.iterator();
         
         System.out.println("----------------------华丽分割线---------------------------------");
         while(iterator.hasNext()){
                Object object = iterator.next();
                //do something to object;    
                System.out.println(object.toString());
            }
    }
}

Collection下集合包含有以下接口

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容