java中各种容器类的比较
1.ArrayList类:
ArrayList容器里面装的对象是按你添加的顺序来排列的,你向容器里面加了什么,它就有什么。并且能根据索引来找出你添加的对象。
2.HashSet类:
HashSet容器装的对象是无序的,并且是唯一的,即容器里面不会出现两个相同的对象。它也不能根据索引来寻找对象。
3.HashMap类:
HashMap容器装的对象是键值对的形式,如:HashMap
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Text2 {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
HashSet<String> b = new HashSet<String>();
HashMap<Integer, String> c = new HashMap<Integer, String>();
a.add("first");
a.add("second");
a.add("first");
System.out.println(a); //[first, second, first]
System.out.println("--------");
b.add("first");
b.add("second");
b.add("first");
System.out.println(b); //[second, first]
System.out.println("--------");
c.put(1, "first");
c.put(2, "second");
c.put(3, "third");
c.put(3, "four");
System.out.println(c); //{1=first, 2=second, 3=four}
}
}
上述代码中的注释即它们的输出结果。
还没有评论,来说两句吧...