码迷,mamicode.com
首页 > 其他好文 > 详细

J2SE之容器_Collection_Iterator_Set_List_Comparable

时间:2015-09-03 23:27:28      阅读:467      评论:0      收藏:0      [点我收藏+]

标签:

容器的概念

技术分享

容器API

技术分享

技术分享

Collection接口

技术分享

技术分享

技术分享
import java.util.*;

public class BasicContainer {
    public static void main(String[] args) {
        Collection c = new HashSet();
        c.add("hello");
        c.add(new Name("f1","l1"));
        c.add(new Integer(100));
        c.remove("hello"); 
        c.remove(new Integer(100));
        System.out.println
                  (c.remove(new Name("f1","l1")));
        System.out.println(c);
    }


}

class Name implements Comparable {
    private String firstName,lastName;
    public Name(String firstName, String lastName) {
        this.firstName = firstName; this.lastName = lastName;
    }
    public String getFirstName() {  return firstName;   }
    public String getLastName() {   return lastName;   }
    public String toString() {  return firstName + " " + lastName;  }
    
    public boolean equals(Object obj) {
        if (obj instanceof Name) {
            Name name = (Name) obj;
            return (firstName.equals(name.firstName))
                && (lastName.equals(name.lastName));
        }
        return super.equals(obj);
        }
        public int hashCode() {
            return firstName.hashCode();
        }
        
        
        
        public int compareTo(Object o) {
        Name n = (Name)o;
        int lastCmp = 
            lastName.compareTo(n.lastName);
        return 
             (lastCmp!=0 ? lastCmp :
              firstName.compareTo(n.firstName));
    }
        
}
BasicContainer.java

技术分享

Iterator接口

技术分享

Iterator 方法举例

技术分享

技术分享

补充:JDK1.5增强的for循环

技术分享

技术分享
import java.util.*;

public class EnhancedFor {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        for(int i : arr) {
            System.out.println(i);
        }
        
        Collection c = new ArrayList();
        c.add(new String("aaa"));
        c.add(new String("bbb"));
        c.add(new String("ccc"));
        for(Object o : c) {
            System.out.println(o);
        }
    }
}
EnhancedFor.java

Set接口

技术分享

Set方法举例

技术分享

技术分享

List 接口

技术分享

List方法举例

技术分享

List常用算法

技术分享

List常用算法举例

技术分享

Comparable接口

技术分享

技术分享

技术分享

如何选择数据接口※

技术分享

 

J2SE之容器_Collection_Iterator_Set_List_Comparable

标签:

原文地址:http://www.cnblogs.com/gimin/p/4780839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!