1.Iterator<---produces------Collection(List与Set)<---produces------Map 五个接口
2.具体类;List(ArrayList LinkedList)
3.ArrayList;实现长度可变的数组,在内存连续分配空间,遍历元素和访问元素效率高
linkedList;采用链表存储方式,插入,删除元素时效率高
4.Set;HashSet是Collection接口的另外一个子接口,可以存储一组唯一,无序的对象。
5.Map;Map接口的数据都是键-值一对,可以通过键找到值,它们是相对应的
6.泛型;定义将对象的类型作为参数,指定到其它类或者方法上,从而保证类型转换的安全性和稳定性,本性就是参数化类型
7.Iterator;迭代器 hasnext()判断是否有值 next()取值 需要用HashSet具体类调用迭代器
8.public class Test {
public static void main(String[] args) {
News new1=new News();
new1.setIdd(1);
new1.setName("Java小王子");
new1.setAuthor("张三");
News new2=new News();
new2.setIdd(2);
new2.setName("骑士VS步行者");
new2.setAuthor("李四");
//集合
Set set=new HashSet();
set.add(new1);
set.add(new2);
//转换成迭代器
Iterator it=set.iterator();
// hasNext() 判断集合中是否有这个值
while (it.hasNext()) {
//上面判断有值,则next()接收这个值
News n=(News)it.next();
System.out.println(n.getName());
}
}
}
第二种方法
//增强型for
for(Object object:set){
//object是所有的对象,强制转换成News类对象
News n= (News) object;
System.out.println(n.getName());
}
6.Map的定义;它是由键和值一对一对组成的数组 用HashMap()来调用
public static void main(String[] args) {
Map map=new HashMap();
//Map是由键和值一对一对组成的数组
map.put("cn","中国");
map.put("usa", "美国");
map.put("jp", "日本");
//键集合keySet()
Set set=map.keySet();
//值集合values()
//第一种方法迭代器
Iterator it=set.iterator();
while (it.hasNext()) {
//key代表的是键"cn","usa","usa",通过键找到值
String key =(String)it.next();
System.out.println(map.get(key));
}
//第二种方法增强型for
for(Object object:set){
String key =(String)object;
System.out.println(map.get(key));
}
//方法增强型for
for(Object object:set){
System.out.println((String)object+"\t"+map.get((String)object));
}