您的位置:首页 > 职场人生

黑马程序员 java 集合框架

2012-12-31 05:34 369 查看
那些情况使用集合框架?

写程序时并不知道程序运行时会需要多少对象,或者需要更复杂的方式存储对象---那么可以使用java集合框架



collection接口存储一组不唯一无序的对象

list接口存储不唯一、有序插入顺序的对象

set接口存储一组唯一、无序的对象

map接口存储一组键组对象,提供key键到value值得 映射

ArrayList实现了长度可变的数组,在内存中分配连续的空间遍历元素和随机访问元素的效率比较高

LikedList 采用链表存储方式,采取引用的方式指定的,插入删除元素时效率比较高

1、使用ArrayList编写新闻管理系统

package cn.edu.csdn.net;

public class NewTitle {
private  int  id;
private String titleName;
private String creater;
public NewTitle(int id,String titleName,String creater){
this.id=id;
this.titleName=titleName;
this.creater=creater;

}
public String getCreater() {
return creater;
}
public void setCreater(String creater) {
this.creater = creater;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitleName() {
return titleName;
}
public void setTitleName(String titleName) {
this.titleName = titleName;
}
}


package cn.edu.csdn.net;
import java.util.ArrayList;
import java.util.List;

public class NewTitleDemo {

public static void main(String[] args) {
NewTitle car=new NewTitle(1,"汽车","管理员");
NewTitle test=new NewTitle(2,"高考","管理员");
List<NewTitle> newTitleList=new ArrayList<NewTitle>();
newTitleList.add(car);
newTitleList.add(test);
System.out.println("新闻标题数目为:"+newTitleList.size()+"条");
for (int i = 0; i < newTitleList.size(); i++) {
NewTitle  title=(NewTitle) newTitleList.get(i);
System.out.println((i+1)+":"+title.getTitleName());
}
//使用ForEach循环遍历集合对象
System.out.println("forEach 方式");
for (NewTitle title : newTitleList) {
System.out.println(title.getTitleName());
}
}
}


2、使用Set接口

①、Set接口存储一组唯一、无序的对象

②、HashSet接口常用实现类

③、Set接口不存在get方法(通过java集合框架中一个迭代器的接口实现遍历输出)

package cn.edu.csdn.net;
import java.util.LinkedList;
public class NewTitleDemo2 {

public static void main(String[] args) {
NewTitle car=new NewTitle(1,"汽车","管理员");
NewTitle medieal=new NewTitle(2,"医学","管理员");
LinkedList<NewTitle> newsTitleList=new LinkedList<NewTitle>();
newsTitleList.addFirst(car);
newsTitleList.addFirst(medieal);
NewTitle first=(NewTitle)newsTitleList.getFirst();
NewTitle last=(NewTitle)newsTitleList.getLast();
newsTitleList.removeLast();
for (int i = 0; i < newsTitleList.size(); i++) {
NewTitle title =(NewTitle)newsTitleList.get(i);
System.out.println((i+1)+":"+title.getTitleName());
}

}

}


3、Map接口

Map接口存储一组键值对象,提供key键到value值得映射Map接口最常用的实现类是HashMap

package cn.edu.csdn.net;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {

public static void main(String[] args) {
Student student1=new Student("李明","男");
Student student2=new Student("刘丽","女");
Map<String,Student> studentMap=new HashMap<String,Student>();
studentMap.put("jack",student1);
studentMap.put("Rose",student2);
System.out.println("键集"+studentMap.keySet());
System.out.println("值集"+studentMap.values());
System.out.println("键值集合"+studentMap);
String key="jack";
if(studentMap.containsKey(key)){
Student student=studentMap.get(key);
System.out.println("学员姓名"+student.getName());
}
//迭代Map集合的方法
System.out.println("使用Iterator遍历集合");
Set<String> keySet=studentMap.keySet();
for(String keys:keySet){
System.out.println(keys);
}
Iterator<String> iterator=keySet.iterator();
while(iterator.hasNext()){
String keys=iterator.next();
Student student =studentMap.get(keys);
System.out.println("学员姓名:"+student.getName());
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: