许久不写Java代码,许多基础性的东西都已经忘却了,今天写了几个关于Set、List、Map、Stack的小例子发布于此,以供自己以后参考~
1、首先定义几个实体类
2、Map测试代码
package com.xzw.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.xzw.beans.Student;
/**
* 测试Map
*/
public class TestMap {
public static void main(String[] args) {
Map map = new HashMap();
map.put(100, new Student("张三", 12));
map.put(200, new Student("李四", 13));
map.put(300, new Student("王五", 15));
map.put(400, new Student("赵六", 17));
map.put(500, new Student("孙七", 18));
Student stu1 = (Student) map.get(100);
System.out.println(stu1.getName() + ":" + stu1.getAge());
//是否包含这个key
System.out.println(map.containsKey(1));
//移除key对应的元素
System.out.println(map.remove(500));
System.out.println(map.size());
//打印
System.out.println(map.toString());
//遍历
Set keySet = map.keySet();
for (Object key : keySet) {
System.out.println(key + ": " + map.get(key));
}
Set entrySet = map.entrySet();
for (Object entry : entrySet) {
System.out.println(((Entry) entry).getKey() + ":" + ((Entry) entry).getValue());
}
}
}
3、List测试代码
package com.xzw.test;
import com.xzw.beans.Dog;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**
* 测试List
*/
public class TestList {
/**
* 测试ArrayList
*/
public static void testArrayList() {
ArrayList arrayList = new ArrayList();
Dog dog = new Dog("欧欧", "雪纳瑞");
Dog dog2 = new Dog("亚亚", "拉布拉多");
Dog dog3 = new Dog("菲菲", "拉布拉多");
Dog dog4 = new Dog("美美", "雪纳瑞");
arrayList.add(dog);
arrayList.add(dog2);
arrayList.add(dog3);
arrayList.add(3, dog3); //指定位置添加
arrayList.add(dog4);
//删除第一条
arrayList.remove(0);
//删除指定的狗
arrayList.remove(dog3);
System.out.println("共有" + arrayList.size() + "条狗");
System.out.println("分别是:");
//是否包含
System.out.println(arrayList.contains("亚亚"));
//遍历
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
for (int i = 0; i < arrayList.size(); i++) {
if (((Dog) arrayList.get(i)).getName().equals("美美")) {
System.out.println("集合中包含美美的信息");
}
}
if (arrayList.contains(dog4)) {
System.out.println("集合中包含" + dog4.getName() + "的信息");
}
//清空元素
arrayList.clear();
System.out.println(arrayList.isEmpty());
}
/**
* 测试LinkedList
*/
public static void testLinkedList() {
LinkedList linkedList = new LinkedList();
Dog dog = new Dog("欧欧", "雪纳瑞");
Dog dog2 = new Dog("亚亚", "拉布拉多");
Dog dog3 = new Dog("菲菲", "拉布拉多");
Dog dog4 = new Dog("美美", "雪纳瑞");
linkedList.addFirst(dog);
linkedList.addLast(dog4);
linkedList.addFirst(dog2);
linkedList.addLast(dog3);
System.out.println(linkedList);
System.out.println("第一条狗的昵称:" + ((Dog) linkedList.get(0)).getName());
System.out.println("最后一条狗的昵称:" + ((Dog) linkedList.get(linkedList.size() - 1)).getName());
linkedList.removeFirst();
linkedList.removeLast();
System.out.println("剩下" + linkedList.size() + "条狗");
System.out.println("分别是:");
for (Object object : linkedList) {
System.out.println(object);
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
new TestList().testArrayList();
new TestList().testLinkedList();
long end = System.currentTimeMillis();
System.out.println("计算程序运行时间:" + (end-start));
}
}
4、Set测试代码
package com.xzw.test;
import com.xzw.beans.Student;
import java.util.HashSet;
import java.util.Iterator;
/**
* 测试Set
*/
public class TestSet {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("1234");
hashSet.add(new Student("xzw", 23));
hashSet.add(new Student("xzw", 23));
hashSet.add(new Student("xzw", 33));
hashSet.add(new Student("xzw1", 33));
hashSet.add(123);
hashSet.add(true);
//打印 (无序)
System.out.println(hashSet);
//hashset比较相同使用的是equals方法,相同的元素中只保存第一个
//清空
hashSet.clear();
System.out.println(hashSet);
//移除的时候也是使用equals进行比较
hashSet.remove(new Student("xzw", 23));
System.out.println(hashSet);
//使用equals进行比较
System.out.println(hashSet.contains(new Student("xzw", 13)));
//遍历
for (int i = 0; i < hashSet.size(); i++) {
System.out.println();
}
for (Object object : hashSet) {
System.out.println(object);
}
Iterator it = hashSet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
5、Stack测试代码
package com.xzw.test;
import java.util.Stack;
/**
* 测试Stack
*/
public class TestStack {
public static void main(String[] args) {
Stack stack = new Stack();
//添加元素
stack.push(12);
stack.push(34);
stack.push("test");
stack.push(false);
stack.push(31);
System.out.println(stack);
//查看栈顶元素
System.out.println(stack.peek());
//移除栈顶元素
System.out.println(stack.pop());
}
}