阐述三种集合的特点和使用场景

List:线性有序的,其中的元素可以重复,添加新的元素是添加到队列的末尾
ArrayList以队列方式存储,在进行大量数据的遍历操作时效率比较高
而LinkedLis是链表方式存储,在进行指定数据的随机访问时效率要高一些
/**
* 用于测试List的类
* @author mytream
*
*/
public class TestList {

/**
* 程序的入口
* @param args 从控制台接受的字符参数
*/
public static void main(String[] args) {

//定义一个List<Student>变量
List<Student> list;

//创建一个具有若干元素的List<Student>对象
list = getStudents();

//遍历1
//取得list的迭代器
Iterator<Student> it = list.iterator();

//遍历list中的Student元素并打印出他们的信息
Student s;
while(it.hasNext()) {
s = it.next();
s.showInfo();
}

//遍历2(遍历1的简单写法)
for(Student s:list) {
s.showInfo();
}

//遍历3
for(int i=0;i<list.size();i++) {
list.get(i).showInfo();
}

}

/**
* 创建一个List<Student>对象
* @return List<Student>对象
*/
private static List<Student> getStudents() {
//定义一个List<Student>变量
List<Student> list = new ArrayList<Student>();

//通过一个循环向list中加入若干Student对象
Student s;
for(int i=0;i<4;i++) {
s = new Student("学生"+(char)(65+i),19+i);
list.add(s);
}

//返回list
return list;

}

}


Set:非线性无序的,元素不允许重复,重复的元素无法添加到set中
Set不保证元素的有序存储,但并不一定就是无序的
/**
* Set的测试类
* @author mytream
*
*/
public class TestSet {

/**
* 程序的入口
* @param args
*/
public static void main(String[] args) {

//创建Set<Student>对象
Set<Student> set;

//向set中加入一组Student对象
set = getStudents();

//获取set的迭代器
Iterator<Student> it = set.iterator();

//遍历set中的Student对象,并打印出姓名
Student s;
while(it.hasNext()) {
s = it.next();
s.showInfo();
}
}

/**
* 获取一组学生的信息
* @return
*/
private static Set<Student> getStudents() {

//定义一个Set对象
Set<Student> s = new HashSet<Student>();

//通过一个循环想set中加入若干Student对象
Student student;
for(int i=0;i<4;i++){
student = new Student("学生"+(char)(65+i),18+i);
s.add(student);
}

//返回此set
return s;

}
}


Map:其中的元素是由一个个键值对(K-->V)组成的.因为键的的保存是无序的因此映射(Map)中的键值对存储是无序的。
映射中所有的键(K)组成一个set对象。添加新的映射时,如果键(Key)与映射中的某个键相等而值(Value)不相等。
则用新映射中的Value替代已存在映射的Value
/**
* Map的测试类
* @author mytream
*
*/
public class TestMap {

/**
* 程序的入口
* @param args 从控制台接收的字符信息
*/
public static void main(String[] args) {
//定义一个 Map<Integer,Student>变量
Map<Integer,Student> map;

//创建map对象
map = createMap();

//测试Map的clear()方法
//map.clear();

//用Map的entrySet()方法测试
/*Set<Map.Entry<Integer,Student>> set = map.entrySet();
Iterator<Map.Entry<Integer,Student>> it = set.iterator();
while(it.hasNext()) {
Map.Entry<Integer,Student> entry = it.next();
entry.getValue().showInfo();
}*/

//用Map的keySet()测试
/*Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while(it.hasNext()) {
Integer in = it.next();
map.get(in).showInfo();
}*/


//用Map的values()方法测试
Collection<Student> students = (Collection<Student>)map.values();//values()返回的是Collecion视图
for(Student s:students) {
s.showInfo();
}


}

/**
* 创建一个 若干元素的Map<Integer,Student>对象
* @return Map<Integer,Student>对象
*/
private static Map<Integer,Student> createMap() {

//定义创建一个Map<Integer,Student>对象
Map<Integer,Student> map = new HashMap<Integer,Student>();

//向map中 加入若干元素
Student s;
for(int i = 0;i < 4;i++) {
s = new Student("学生"+(char)(65+i),17+i);
map.put(i, s);//i会自动转型
}

//返回map
return map;
}

}[align=left][/align]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值