1.ArrayList类。
相当于c++中的链表。提供了一些方法供操作。可以链接一切object。放入的时候也必须是放入对象。
当开始申请一个对象的时候它会维护一个大小10的数组(调用不带参数的构造函数),当超过了数组的角标最大,那么就重新申请空间,大小为:
原数组大小 * 1.5 + 1;
2.LinkedList类。
这个类集合了链表的操作,和c++差不多。具体用法在j2se中查找就行了。
3.HashSet类。
这个类可以快速定位一个对象,通过hash code。并且要确保数据的唯一性。使用这个类的时候就需要调用HashCode方法来获取这个唯一的值。用来标记。
4.HashCode函数
每一个Object类都有这个函数,也就是说每一个类都有这个函数。在Object类中这个函数的值表示了地址(通过某种方式转换过),每一个地址不一样那么它的值也就是不一样的。
例如:
String a = new String("hello");
String b = new String("hello”);
HashSet hash = new HashSet();
hash.add(a);
hash.add(b);
这个时候就b对象加不进去了。上述两个对象的地址肯定是不一样的,但是加不进去,说明String判断唯一性的方法不是通过地址,刚才说了加不加的进去要看hash code 和equals方法,这里String重写了HashCode方法,判断String类容是不是一致。
5.迭代器。
Iterator类,
Iterator ite = set.iterator();
while(ite.hasNext())
{
System.out.println((String)ite.next());
}
6.TreeSet类
主要用于排序。
例如:
import java.util.*;
public class Com
{
public static void main(String[] args)
{
TreeSet sort = new TreeSet(new Student());
sort.add(new Student(10));
sort.add(new Student(20));
sort.add(new Student(30));
sort.add(new Student(40));
System.out.println(sort);
}
}
class Student implements Comparator
{
int value;
public Student()
{}
public Student(int value)
{
this.value = value;
}
public int compare(Object a,Object b)
{
Student a1 = (Student)a;
Student b1 = (Student)b;
return a1.value > b1.value ? 1 : -1;
}
public String toString()
{
return String.valueOf(this.value);
}
}
需要实现Comparator接口它中有一个compare的方法。很简单的操作。
7.Collections 类。
该类有许多的静态方法,用来对集合的操作。
8.Map类。
相当于映射,可以通过一个k(键值)关联一个对象,当需要取出一个对象的时候就可以通过这个键值来取出。例如:
HashMap map = new HashMap();
map.put("1","zhang xin");
map.put("2","ming xi");
map.put("3","pang zi");
System.out.println(map.get("1"));
就可以打印出zhang xin
键值是不可以重复的。就相当于一个键值只可以映射一个对象,而一个对象可以被多个键值映射。
import java.util.*;
public class Com
{
public static void main(String[] args)
{
HashMap map = new HashMap();
map.put("1","zhang xin");
map.put("2","ming xi ");
map.put("3","pang zi ");
Set set = map.keySet();
for(Iterator i = set.iterator();i.hasNext();)
{
String a = (String)i.next();
String value = (String)map.get(a);
System.out.println(value);
}
}
}
这样来遍历map中的内容。
map可以用来对数据的统计。
在java中是允许定义内部类的。
例如:
B类是在A类中定义的,那么就要这样: A.B b = new A.B(); 来申请对象。
Map.entry 就是Map类的一个内部类。
可以通过Set = map.entrySet();来获取一个Set集合。然后用
Map.Entry entry = (Map.Entry)object;
String str1 = entry.getKey();
String str2 = entry.getValue();
来获取成对的key和值。