学习打卡4--集合类

该内容没有什么太难理解的地方,所以就只照书上的代码代码敲了一遍

ArrayList集合

package Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class Collectiontest {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("wahaha");
        list.add("kangshifu");
        list.add("ADgai");
        
        for(String str:list) {
            System.out.println(str);
        }
        //使用迭代器遍历集合
        Iterator<String> ite = list.iterator();
        while(ite.hasNext()) {  //判断是否有下一个元素
            System.out.println(ite.next());
        }
        String[] s = new String[list.size()];
        list.toArray(s);
        for(int i=0;i<list.size();i++) {
            System.out.println(s[i]);
        }
        }
    }

Set集合

package Collection;
import java.util.*;

public class UpdataStu implements Comparable<Object>{
       String name;
       long id;
       
       public UpdataStu(String name,long id) {
           this.id = id;
           this.name = name;
       }
     
       public int compareTo(Object o){
           UpdataStu upstu = (UpdataStu)o;
           int result = id>upstu.id?1:(id==upstu.id ?0:-1);
           return result;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    
       public long getid() {
           return id;
       }
        public void setid(long id) {
            this.id = id;
        }
       
       public static void main(String[] args) {
           UpdataStu stu1 = new UpdataStu("李同学",01011);
           UpdataStu stu2 = new UpdataStu("陈同学",01021);
           UpdataStu stu3 = new UpdataStu("王同学",01051);
           UpdataStu stu4 = new UpdataStu("马同学",01012);
           TreeSet<UpdataStu> tree = new TreeSet<UpdataStu>();
           HashSet<String> hash = new HashSet<String>();
           tree.add(stu1);
           tree.add(stu2);
           tree.add(stu3);
           tree.add(stu4);
           hash.add("first");
           hash.add("second");
           hash.add("first");
              System.out.println(hash);//集合中不能有重复元素,所以输出只有两个元素
           Iterator <UpdataStu> it = tree.iterator();
           while(it.hasNext()) {
               UpdataStu stu = (UpdataStu)it.next();//it.next()取出的是Object对象,所以这里需要强制转换
               System.out.println(stu.getid()+" "+stu.getName());
           }
           System.out.println("------------");
           it = tree.headSet(stu2).iterator();
           while(it.hasNext()) {
               UpdataStu stu = (UpdataStu)it.next();
               System.out.println(stu.getid()+"   "+stu.getName());
           }
       }
    

}

Map集合


package Collection;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class Test {
       public static void main(String[] args) {
           Map<String,String> map = new HashMap<String,String>();
           map.put("1", "lala");
           map.put("2","haha");
           map.put("3", "shadiao");
       for(String key:map.keySet()) {
           System.out.println(key+"   "+map.get(key));
       }
       
       //使用迭代器遍历map
       Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
       while(it.hasNext()) {
           Map.Entry<String,String>entry = it.next();
           System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
       }
    }
}

猜你喜欢

转载自www.cnblogs.com/susususu/p/10744138.html
4--