Collection集合的基本结构
(6个接口:Collection、Set、List、Map、Iterator、Comparable)
所有实现了Collection接口的容器类都有iterator方法,用于返回一个实现了Iterator接口的对象。Iterator对象称作迭代器,Iterator接口方法能以迭代方式逐个访问集合中各个元素,并可以从Collection中除去适当的元素。
Comparable可以用于比较的实现,实现了Comparable接口的类可以通过实现comparaTo方法从而确定该类对象的排序方式。
(http://blog.csdn.net/jiuqiyuliang/article/details/32697903)
Collection接口-定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。
Set中的数据对象没有顺序且不可重复List中的数据对象有顺序且可以重复
Map接口定义了存储“键(key)--值(value)映射对”的方法(hashCode方法)
(http://blog.csdn.net/jinhongliang123/article/details/7924382)
总结
Java容器实际上只有三种:Map , List, Set;但每种接口都有不同的实现版本.它们的区别可以归纳为由什么在背后支持它们.也就是说,你使用的接口是由什么样的数据结构实现的.
List的选择:
比如:ArrayList(读快改慢)和LinkedList(改快读慢)都实现了List接口.因此无论选择哪一个,基本操作都一样.但ArrayList是由数组提供底层支持.而LinkedList是由双向链表实现的.所以,如果要经常向List里插入或删除数据,LinkedList会比较好.否则应该用速度更快的ArrayList。
Set的选择
HashSet总是比TreeSet 性能要好.而后者存在的理由就是它可以维持元素的排序状态.所以,如果需要一个排好序的Set时,才应该用TreeSet。
Map选择:
同上,尽量选择HashMap。
(http://blog.csdn.net/dandanzmc/article/details/23447827)
List的选择:
比如:ArrayList(读快改慢)和LinkedList(改快读慢)都实现了List接口.因此无论选择哪一个,基本操作都一样.但ArrayList是由数组提供底层支持.而LinkedList是由双向链表实现的.所以,如果要经常向List里插入或删除数据,LinkedList会比较好.否则应该用速度更快的ArrayList。
Set的选择
HashSet总是比TreeSet 性能要好.而后者存在的理由就是它可以维持元素的排序状态.所以,如果需要一个排好序的Set时,才应该用TreeSet。
Map选择:
同上,尽量选择HashMap。
容器类对象在调用remove,contains等方法时需要比较对象是否相等,这会涉及到对象类型的equals方法和hashCode方法;对于自定义的类型,需要要重写equals 和 hashCode方法以及实现自定义的对象相等规则。
import java.util.*;//包含Collection接口
/*范例名称:
* 原文件名称:
* 要点:
* 1. Collection基本结构图
* 2. Collections类
* 3. For、Generic、Auto-boxing / unboxing (循环、泛型、自动打包/解包)
* 4. 六个接口: Collection、Set、List、Map、Iterator、Comparable
*/
public class CollectionTest {
public static void main(String[] args){
Collection c=new ArrayList();
//放入不容类型的对象
c.add("hello");//添加元素(增)
c.add(new Name("f11","11"));
c.add(new Integer(100));
System.out.println(c);
Collection c2=new HashSet();
c2.add("hello");
c2.add(new Name2("f11","11"));
c2.add(new Integer(100));
c2.remove("hello");
c2.remove(new Integer(100));
System.out.println(c2.remove(new Name2("f11","11")));
System.out.println(c2);
//迭代器Iterator遍历(查)
Collection c3=new HashSet();//无序
c3.add(new Name2("ffff1","11"));
c3.add(new Name2("f2", "12"));
c3.add(new Name2("fff3", "13"));
Iterator i=c3.iterator();
while(i.hasNext()){
//next()的返回值是Object类型,需要强制类型转换
Name2 n=(Name2)i.next();
System.out.print(n.getFirstName()+" ");
}
System.out.println("");
//删除元素(删)
for(Iterator i2=c3.iterator();i2.hasNext();){
Name2 name=(Name2)i2.next();
if(name.getFirstName().length()<5){
i2.remove();//不能换成c3.remove(name2);
}
}
System.out.println(c3);
//List实例
List list1=new LinkedList();
for(int j=0;j<9;j++){list1.add("Hello"+j);}
Collections.shuffle(list1);//Collections类;随机排列list对象元素
System.out.println(list1);
Collections.sort(list1);//排序
System.out.println(list1);
Collections.reverse(list1);//逆序
System.out.println(list1);
//对Name3类进行排序
List list2=new LinkedList();
list2.add(new Name3("China", "ch"));
list2.add(new Name3("Japan", "jp"));
list2.add(new Name3("Singapore", "sg"));
list2.add(new Name3("Hongkong", "hk"));
System.out.println(list2);
Collections.sort(list2);//Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.
System.out.println(list2);
//Map实例
Map m1=new HashMap();
Map m2=new TreeMap();
m1.put("one", new Integer(1));//1.5版本以前;之后可以用m1.put("one",1)代替,自动打包功能
m1.put("two", new Integer(3));
m2.put("A",new Integer(1));
m2.put("B", new Integer(2));
if(m1.containsKey("two")){
int k=((Integer)m1.get("two")).intValue();
System.out.println(k);
}
Map m3=new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
//Map实例_泛型
Map<String, Integer> m11=new HashMap<String, Integer>();
Map<String, Integer> m12=new HashMap<String, Integer>();
m11.put("one", 1);
m11.put("two", 3);
m12.put("A", 1);
m12.put("B", 2);
if(m11.containsKey("two")){
int m=m11.get("two");//Integer自动解包为int
System.out.println(m);
}
Map<String, Integer> m13=new HashMap<String, Integer>(m11);
m13.putAll(m12);
System.out.println(m13);
//检测String数组中相同字符串的数目
String[] testString=new String[5];
testString[0]="aa";testString[1]="bb";testString[2]="cc";testString[3]="bb";testString[4]="aa";
Map<String, Integer> mTest=new HashMap<String, Integer>();
for(int ii=0;ii<testString.length;ii++){
if(!mTest.containsKey(testString[ii])){
mTest.put(testString[ii], 1);
}else{
int freq=mTest.get(testString[ii]);
mTest.put(testString[ii], freq+1);
}
}
System.out.println(mTest.size()+" distinct words detected:");
System.out.println(mTest);
//对Name4类进行排序_泛型写法
List<Name4> list3=new LinkedList<Name4>();
list3.add(new Name4("China", "ch"));
list3.add(new Name4("Japan", "jp"));
list3.add(new Name4("Singapore", "sg"));
list3.add(new Name4("Hongkong", "hk"));
System.out.println(list3);
Collections.sort(list3);//Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.
System.out.println(list3);
}
}
class Name{
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;}//重写了toString方法
}
class Name2{
private String firstName,lastName;
public Name2(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;}//重写了toString方法
//重写equals跟hashCode方法
public boolean equals(Object obj){
if(obj instanceof Name2){
Name2 name=(Name2)obj;
return (firstName.equals(name.firstName) && lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode(){
return firstName.hashCode();
}
}
//Comparable接口
class Name3 implements Comparable {
private String firstName,lastName;
public Name3(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;}//重写了toString方法
//重写equals跟hashCode方法
public boolean equals(Object obj){
if(obj instanceof Name3){
Name3 name=(Name3)obj;
return (firstName.equals(name.firstName) && lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode(){
return firstName.hashCode();
}
//重写compareTo方法
public int compareTo(Object o){
Name3 n=(Name3)o;
int lastCmp=lastName.compareTo(n.lastName);
return (lastCmp!=0?lastCmp:firstName.compareTo(n.firstName));
}
}
//Comparable接口_泛型写法
class Name4 implements Comparable<Name4> {
private String firstName,lastName;
public Name4(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;}//重写了toString方法
//重写equals跟hashCode方法
public boolean equals(Name4 name){
return (firstName.equals(name.firstName) && lastName.equals(name.lastName));
}
public int hashCode(){
return firstName.hashCode();
}
//重写compareTo方法
public int compareTo(Name4 o){
int lastCmp=lastName.compareTo(o.lastName);
return (lastCmp!=0?lastCmp:firstName.compareTo(o.firstName));
}
}
输出结果:
[hello, f11 11, 100]
true
[]
fff3 ffff1 f2
[ffff1 11]
[Hello4, Hello2, Hello3, Hello0, Hello7, Hello6, Hello8, Hello1, Hello5]
[Hello0, Hello1, Hello2, Hello3, Hello4, Hello5, Hello6, Hello7, Hello8]
[Hello8, Hello7, Hello6, Hello5, Hello4, Hello3, Hello2, Hello1, Hello0]
[China ch, Japan jp, Singapore sg, Hongkong hk]
[China ch, Hongkong hk, Japan jp, Singapore sg]
3
{A=1, B=2, two=3, one=1}
3
{A=1, B=2, two=3, one=1}
3 distinct words detected:
{aa=2, bb=2, cc=1}
[China ch, Japan jp, Singapore sg, Hongkong hk]
[China ch, Hongkong hk, Japan jp, Singapore sg]
这一章其实是训练数据结构的好地方,可以自己实现一下基本数据结构的源码(ArrayList、LinkedList、Stack、Queue、Tree等)
在Eclipse中查看Java类库的源代码
首先要找到JDK安装目录,然后该目录下会有个src.zip文件,我们把它加载到Eclipse中就行了,然后在Eclipse中想查看某个方法或者某个类的源代码,直接选中按F3
下面那个Path就是你所安装JDK目录下的src.zip的路径
【引自:http://www.2cto.com/kf/201612/571310.html】
使用:
可以在 Java 源代码编辑器或代码片段编辑测试窗中选择类型、方法或字段的名称,然后对元素的定义打开编辑器。
在 Java 编辑器中,选择类型、方法或字段的名称。您也可以仅仅在名称中单击一次。
执行下列其中一项操作:
1.从菜单栏中,选择浏览 > 打开声明
2.从编辑器的弹出菜单中,选择打开声明
3.按 F3 键
或者
1.按下 Ctrl 键。
2.在 Java 编辑器中,将鼠标移到类型、方法或字段的名称上,直到名称变为带有下划线为止。
3.单击一次超链接。
如果具有多个同名的定义,则会显示一个对话框,您可以选择想要打开的一个定义。一个编辑器打开,它包含所选择的元素。
原文地址:http://ajava.org/course/tool/14485.html