Java集合框架
list接口和ArrayList类
示例:
public class FirstLevelTitle {
private int id; //ID
private String titleName; //名称
private String creater; //创建者
private Date createTime; //创建时间
public FirstLevelTitle(int id, String titleName, String creater,Date createTime) {
this.id = id;
this.titleName = titleName;
this.creater = creater;
this.createTime = createTime;
}
public String getTitleName() {
return titleName;
}
public void setTitleName(String titleName) {
this.titleName = titleName;
}
…..
}
示例:
public class FirstLevelTitleDB1 {
public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());
FirstLevelTitle test = new FirstLevelTitle(2, "高考", "管理员", new Date());
List newsTitleList = new ArrayList();
newsTitleList.add(car);
newsTitleList.add(test);
System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");
print(newsTitleList);
}
public static void print(List newsList) {
for (int i = 0; i < newsList.size(); i++) {
FirstLevelTitle title = (FirstLevelTitle) newsList.get(i);
System.out.println(i + 1 + ":" + title.getTitleName());
}
}
}
示例:
public class FirstLevelTitleDB2 {
public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "网站管理员", new Date());
FirstLevelTitle test = new FirstLevelTitle(2, "高考", "网站管理员", new Date());
FirstLevelTitle house = new FirstLevelTitle(3, "房产", "网站管理员", new Date());
List newsTitleList = new ArrayList();
newsTitleList.add(car);
newsTitleList.add(test);
newsTitleList.add(2,house);
if(newsTitleList.contains(test)){
System.out.println("有高考的新闻");
}else{
System.out.println("没有高考的新闻");
}
newsTitleList.remove(1);
System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");
}
}
List接口和LinkedList类
示例:
public class FirstLevelTitleDB3 {
public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());
FirstLevelTitle medical = new FirstLevelTitle(2, "医学", "管理员",new Date());
LinkedList newsTitleList = new LinkedList();
newsTitleList.addFirst(car);
newsTitleList.addLast(medical);
FirstLevelTitle first = (FirstLevelTitle) newsTitleList.getFirst();
System.out.println("头条的新闻标题为:" + first.getTitleName());
FirstLevelTitle last = (FirstLevelTitle) newsTitleList.getLast();
System.out.println("排在最后的新闻标题为:" + last.getTitleName());
newsTitleList.removeFirst();
newsTitleList.removeLast();
}
}
Interator接口
Iterator接口隐藏底层集合的数据结构,提供遍历各种类型的集合的统一接口
Set(集)
Set是最简单一种集合,集合的对象不按特定的方式排列,并且没有重复对象
Set接口主要有两个实现类:
HashSet
TreeSet
HashSet类按照哈希算法来存取集合中的对象,具有很好的存取和查找性能
//往集合中加入了两个元素
Set set = new HashSet();
String s1= new String("Hello");
String s2= new String("World");
set.add(s1);
set.add(s2);
System.out.println(set.size());
//往集合中加入了一个元素
Set set = new HashSet();
String s1= new String(“Hello”);
String s2= new String(“Hello”);
set.add(s1);
set.add(s2);
System.out.println(set.size());
遍历集合中的元素
Iterator it = set.iterator(); //取得Iterator对象
while (it.hasNext()) { //遍历集合中所有元素
Object element = it.next(); //取出集合中一个元素
System.out.println(element);
}
}
TreeSet
TreeSet类实现了SortedSet接口,能够对集合中的对象进行排序
Set set = new TreeSet();
set.add(new Integer(8));
set.add(new Integer(7));
set.add(new Integer(6));
set.add(new Integer(9));
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
TreeSet中添加自定义类的对象,则必须实现Comparable接口
class Student implements Comparable {
private int id; //学号
private String name; 姓名
……
//实现接口方法,指定按学号排序
public int compareTo(Object o) {
Student s = (Student)o;
if (id < s.getId()) return -1;
if (id > s.getId()) return 1;
return 0;
}
}
Set set = new TreeSet ();
set.add(new Student(3,"Tom"));
set.add(new Student(1,"Eddie"));
set.add(new Student(4,"Jane"));
set.add(new Student(2,"Mike"));
Iterator it = set.iterator();
while (it.hasNext()) {
Student s =(Student) it.next();
System.out.println(s.getId()+ " " +s.getName());
}
列表排序
Collections类是Java集合类库中的辅助类,它提供了操纵集合的各种静态方法,其中sort( )方法用于对List中的对象进行排序
List list = new ArrayList();
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(2));
Collections.sort(list);
for (int i=0; i<list.size(); i++){
System.out.print( list.get(i) + " " );
}
该sort方法要求集合中的元素实现了Comparable接口
Map接口和HashMap类
public class Student {
private String name; // 学员姓名
private String school; // 中心名称
public Student(String name, String school) {
this.name = name;
this.school = school;
}
public String toString() {
return school+"毕业的"+name;
}
}
示例:
public class MapTest {
public static void main(String[] args) {
Student student1 = new Student("李明", "北京中心");
Student student2 = new Student("刘丽", "天津中心");
Map students = new HashMap();
students.put("Jack", student1);
students.put("Rose", student2);
System.out.println("键集:"+students.keySet());
System.out.println("值集:"+students.values());
System.out.println("键-值对集合:"+students);
String key = "Jack";
if(students.containsKey(key))
System.out.println(students.get(key));
students.remove(key);
System.out.println("键-值对集合:"+students);
}
}
总结
Collection:集合层次中的根接口,JDK没有提供这个接口直接的实现类;
Set:不能包含重复的元素,实现类有HashSet。SortedSet是一个按照升序排列元素的Set,实现类有TreeSet;
List:是一个有序的集合,可以包含重复的元素。提供按索引访问的方式。实现类有ArrayList和LinkedList;
Map:包含了key-value对。key不能重复,value可重复,实现类有HashMap。SortedMap是一个按照升序排列key的Map,实现类有TreeMap;
集合对象的主要操作有:增加元素,删除元素,对元素排序,访问(遍历)元素等。