Java collection framework architecture:
1,collection
- List: ArrayList, ordered, repeats allowed
- Queue: LinkedList, ordered, repeats allowed
- Set: HashSet, no need, no repeat
2,Map <key,value>
- HashMap, store key-value pairs
ArrayList:
package com.ulikeme.set;
import java.util.ArrayList;
import java.util.Date;
public class NoticeTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
// 创建Notice对象,生成三条公告
Notice notice1 = new Notice(1, "天气预报", "管理员", new Date());
Notice notice2 = new Notice(2, "失物招领", "学生", new Date());
Notice notice3 = new Notice(3, "考勤通知", "老师", new Date());
// 添加公告,add(内容)在列表末尾添加
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
// 显示公告
System.out.println("公告内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
System.out.println("--------------------------");
// 在第一条公告后面添加一条新公告,add(index,内容)在index下标处添加
Notice notice4 = new Notice(4, "放假通知", "教务处", new Date());
noticeList.add(1, notice4);
System.out.println("添加放假通知后,公告内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
System.out.println("--------------------------");
// 删除失物招领公告
noticeList.remove(2);
// noticeList.remove( notice2 );
System.out.println("删除失物招领公告后,公告内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
System.out.println("--------------------------");
//将最后2条公告改成 ,放假通知:寒假假期
notice4.setTitle("放假通知:寒假假期");
noticeList.set(1, notice4);
System.out.println("将最后2条公告改成 ,放假通知:寒假假期后,公告内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
}
}
HashSet
package com.ulikeme.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//将英文单词添加到HashSet中
Set set=new HashSet();
//向集合添加元素
set.add("blue");
set.add("red");
set.add("black");
set.add("yellow");
set.add("white");
//显示集合的内容,用迭代器Iterator :hasNext()检测是否有下一个元素,next()返回集合中下一个元素
System.out.println("集合中的元素为");
//遍历迭代器输出元素
Iterator it=set.iterator();
while(it.hasNext()) {
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("------------------");
//在集合中插入新的单词 hello
//若插入重复元素,插入失败,但是不会报错
set.add("hello");
it=set.iterator();
System.out.println("插入hello后,集合中的元素为");
while(it.hasNext()) {
System.out.print(it.next()+" ");
}
}
}
HashMap
package com.ulikeme.set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Map<String, String> animal=new HashMap<String,String>();
System.out.println("从键盘输入三组单词的注释,并存到HashMap中");
//添加数据
Scanner console=new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.println("请输入key值(单词)");
String key=console.next();
System.out.println("请输入value值(注释)");
String value=console.next();
animal.put(key, value);
}
//打印输出value的值,使用迭代器
System.out.println("——----------------------");
Iterator<String> it=animal.values().iterator();
System.out.print("使用迭代器输出value的值:");
while(it.hasNext()) {
System.out.print(it.next()+" ");
}
//打印输出key,value的值,通过entrySet方法打印
System.out.println();
System.out.println("——----------------------");
System.out.println("通过entrySet方法打印key,value的值");
Set<Entry<String, String>> entrySet=animal.entrySet();
for(Entry<String, String> entry:entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
//通过单词找到注释并输出
System.out.println();
System.out.println("——----------------------");
System.out.println("请输入要查找的单词:");
String strSearch=console.next();
//取得keySet
Set<String> keySet=animal.keySet();
for(String key:keySet) {
if(strSearch.equals(key)) {
System.out.println("找到了,键值对 "+key+":"+animal.get(key));
break;
}
}
}
}