1、定义线性表及其链表
package com.zhengHong.zh;
import java.util.*;
public class TestPhoto {
public static void main(String[] args) {
List album = new LinkedList<>(); //创建链表
// List album = new ArrayList<>();//创建数组
album.add(new Photo("one ",new Date(),“classroom”));
album.add(new Photo(“two”,new Date(),“library”));
album.add(new Photo(“three”,new Date(),“gym”));
album.add(new Photo(“four”,new Date(),“dorm”));
Iterator<Photo> iterator = album.iterator();
while (iterator.hasNext()){
Photo photo = iterator.next();
System.out.println(photo.toString());
}
for (Photo photo:album){
System.out.println(photo);
}
}
}
2、队
Queue q = new LinkedList<>();
for (int i = 0;i<5;i++){
q.offer(i);
}
((LinkedList) q).addFirst(10);
((LinkedList) q).addLast(100);
while (!q.isEmpty())
System.out.println(q.poll());
3、Set
Set set = new HashSet<>();
set.add(“China”);
set.add(“Brazil”);
set.add(“Russia”);
set.add(“India”);
set.add(“South Afria”);
System.out.println(set.contains("China"));
//HashSet的遍历无序,并不一定根据其定义或者插入顺序进行遍历
//它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。
for (String obj:set) {
System.out.println(obj);
}
4、Map
//该映射根据其键的自然顺序进行排序,
// 或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。
Map<String,String> map = new TreeMap<>();
map.put(“c”,“China”);
map.put(“b”,“Brizil”);
map.put(“回家”,“我只是想回家”);
map.put(“r”,“Russia”);
map.put(“你”,“你好”);
map.put(“健康”,“爸妈身体健康”);
//放重复的值时(此处的重复判断标准为key值),后面的重复项会覆盖前面相应的key值的那一项
// System.out.println(map.get(“健康”));
// System.out.println(map.get(“回家”));
// for (String key:map.keySet()) {
// System.out.println(key + “:” + map.get(key));
// }
// for (String value:map.values()) {
// System.out.println(value);
// }
//映射项(键-值对)。Map.entrySet 方法返回映射的 collection 视图,其中的元素属于此类。
// for (Map.Entry<String,String> entry:map.entrySet()) {
// System.out.println(entry.getKey() + “:” + entry.getValue());
// }
// Iterator it = map.entrySet().iterator();
// while (it.hasNext()){
// Map.Entry<String,String> entry = (Map.Entry<String, String>)it.next();
// System.out.println(entry.getKey() + “:” + entry.getValue());
// }