- package org.vv.game.test;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Hashtable;
- import java.util.LinkedHashSet;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.TreeSet;
- import java.util.concurrent.ConcurrentLinkedQueue;
- public class TypeTest {
- private void arrayListTest() {
- ArrayList<String> arrayList = new ArrayList<String>();
- // 添加
- arrayList.add(String.valueOf(1));
- arrayList.add(new Integer(2).toString());
- arrayList.add("3");
- // 在指定位置插入对象
- arrayList.add(0, new String("0"));
- for (String str : arrayList) {
- System.out.println(str); // 0,1,2,3
- }
- // 查询
- // 返回位置
- System.out.println(arrayList.indexOf("1")); // 1
- // 返回指定位置的对象
- System.out.println(arrayList.get(1)); // 1
- // 判断对象是否存在
- System.out.println(arrayList.contains("3")); // true
- // 修改
- System.out.println(arrayList.set(1, "11"));
- // 删除
- arrayList.remove(0); // remove index
- arrayList.remove(String.valueOf(2)); // remove obj
- // JDK 1.5 后的遍历方式
- for (String str : arrayList) {
- System.out.println(str); // 11,3
- }
- // 原始迭代器
- ListIterator<String> listIterator = arrayList.listIterator();
- String string = null;
- while (listIterator.hasNext()) {
- string = (String) listIterator.next();
- System.out.println(string);
- }
- // 转换为数组
- // 此方法在多线程同时修改 {@code List} 的情况下可能抛出异常
- // String[] l = (String[])arrayList.toArray(new
- // String[arrayList.size()]);
- // 其它的一些方法
- arrayList.size(); // 获取大小
- arrayList.clear(); // 清空
- arrayList.isEmpty(); // 是否为空
- arrayList.subList(0, 1); // 截取
- arrayList.lastIndexOf("0"); //返回指定的对象在列表中最后一次出现的位置索引。
- arrayList.trimToSize();//将此 ArrayList 实例的容量调整为列表的当前大小。
- }
- private void linkedListTest() {
- // 多个线程同时访问链接的哈希集合时保持同步
- LinkedList<String> list = new LinkedList<String>();
- list.add("a");
- list.add("b");
- list.add("c");
- list.add("d");
- list.addFirst("first");
- list.addLast("end");
- list.getFirst();
- list.getLast();
- // 其它方法参考ArrayList
- }
- private void hashSetTest() {
- HashSet<String> hashSet = new HashSet<String>();
- // 添加数据 注意每次插入的数据是无序的
- hashSet.add("a");
- hashSet.add("b");
- hashSet.add("c");
- hashSet.add("d");
- // 删除数据
- hashSet.remove("a");
- // 清空数据
- hashSet.clear();
- // 查询数据
- hashSet.contains("a");
- hashSet.size();
- hashSet.isEmpty();
- for (String str : hashSet) {
- System.out.println(str);
- }
- // 转换为数组
- String[] strs = (String[]) hashSet.toArray(new String[0]);
- }
- private void linkedHashSetTest() {
- // 多个线程同时访问链接的哈希集合时保持同步
- LinkedHashSet<String> s = new LinkedHashSet<String>();
- s.add("a");
- s.add("b");
- s.add("c");
- s.add("d");
- for (String str : s) {
- System.out.println(str); // a,b,c,d
- }
- }
- private void treeSetTest() {
- TreeSet<Student> s = new TreeSet<Student>(new Student.compareToStudent());
- s.add(new Student(1, "a"));
- s.add(new Student(2, "b"));
- s.add(new Student(4, "d"));
- s.add(new Student(4, "c"));
- s.first();
- s.last();
- //其它tree节点操作参考jdk文档
- for (Student stu : s) {
- System.out.println(stu);
- }
- }
- private void concurrentLinkedQueueTest() {
- ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();
- concurrentLinkedQueue.add("a");
- concurrentLinkedQueue.add("b");
- concurrentLinkedQueue.add("c");
- concurrentLinkedQueue.offer("d"); // 将指定元素插入到此队列的尾部。
- concurrentLinkedQueue.peek(); // 检索并移除此队列的头,如果此队列为空,则返回 null。
- concurrentLinkedQueue.poll(); // 检索并移除此队列的头,如果此队列为空,则返回 null。
- for (String str : concurrentLinkedQueue) {
- System.out.println(str);
- }
- }
- private void hashMapTest() {
- HashMap<Integer, Student> map = new HashMap<Integer, Student>();
- map.put(1, new Student(1, "a"));
- map.put(2, new Student(2, "a"));
- map.put(3, new Student(3, "a"));
- map.put(4, new Student(4, "a"));
- System.out.println(map.containsKey(1));// true
- System.out.println(map.containsValue(new Student(4, "a")));// true
- System.out.println(map.get(2)); // Student [id=2, name=a]
- // 获取所有的 key
- Set<Integer> keySet = map.keySet();
- for (Integer i : keySet) {
- System.out.println(i); // 1,2,3,4
- }
- // 遍历 Map
- Set<Map.Entry<Integer, Student>> entries = map.entrySet();
- for (Map.Entry<Integer, Student> entry : entries) {
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- // 1:Student [id=1, name=a]
- // 2:Student [id=2, name=a]
- // 3:Student [id=3, name=a]
- // 4:Student [id=4, name=a]
- }
- private void hashTableTest() {
- Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
- numbers.put("one", new Integer(1));
- numbers.put("two", new Integer(2));
- numbers.put("three", new Integer(3));
- Integer n = (Integer) numbers.get("two");
- if (n != null) {
- System.out.println("two = " + n);
- }
- // 其操作方法参考 HashMap
- }
- private void treeMapTest() {
- TreeMap<String, Integer> numbers = new TreeMap<String, Integer>();
- numbers.put("one", new Integer(1));
- numbers.put("two", new Integer(2));
- numbers.put("three", new Integer(3));
- // 其操作方法参考jdk文档
- }
- private void propertiesTest() {
- Properties properties = new Properties();
- OutputStream os = null;
- try {
- os = new FileOutputStream(getClass().getResource("test.properties")
- .getFile());
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try {
- properties.load(getClass().getResourceAsStream("test.properties"));
- properties.getProperty("key");
- properties.getProperty("key", "default");
- properties.setProperty("new key", "new value");
- properties.store(os, "comment");
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // 测试 ArrayList
- // TypeTest.arrayListTest();
- // TypeTest.hashSetTest();
- // TypeTest.linkedHashSetTest();
- new TypeTest().hashMapTest();
- }
- }
- class Student implements Comparable {
- private int id;
- private String name;
- public Student(int id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + "]";
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + id;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Student other = (Student) obj;
- if (id != other.id)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- static class compareToStudent implements Comparator // 定义一个内部类来实现比较器
- {
- public int compare(Object o1, Object o2) {
- Student s1 = (Student) o1;
- Student s2 = (Student) o2;
- int rulst = s1.id > s2.id ? 1 : (s1.id == s2.id ? 0 : -1);
- if (rulst == 0) {
- rulst = s1.name.compareTo(s2.name);
- }
- return rulst;
- }
- }
- @Override
- public int compareTo(Object o) {
- int result;
- Student s = (Student) o;
- result = id > s.id ? 1 : (id == s.id ? 0 : -1);
- if (result == 0) {
- result = name.compareTo(s.name);
- }
- return result;
- }
- }
来源:https://www.cnblogs.com/tiandile/archive/2013/03/20/2970677.html