集合类(List,Set,Map)

心已入冬 提交于 2019-12-25 17:55:21
  1. package org.vv.game.test;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.Comparator;  
  9. import java.util.HashMap;  
  10. import java.util.HashSet;  
  11. import java.util.Hashtable;  
  12. import java.util.LinkedHashSet;  
  13. import java.util.LinkedList;  
  14. import java.util.ListIterator;  
  15. import java.util.Map;  
  16. import java.util.Properties;  
  17. import java.util.Set;  
  18. import java.util.TreeMap;  
  19. import java.util.TreeSet;  
  20. import java.util.concurrent.ConcurrentLinkedQueue;  
  21.   
  22. public class TypeTest {  
  23.   
  24.       
  25.     private void arrayListTest() {  
  26.         ArrayList<String> arrayList = new ArrayList<String>();  
  27.         // 添加  
  28.         arrayList.add(String.valueOf(1));  
  29.         arrayList.add(new Integer(2).toString());  
  30.         arrayList.add("3");  
  31.         // 在指定位置插入对象  
  32.         arrayList.add(0new String("0"));  
  33.   
  34.         for (String str : arrayList) {  
  35.             System.out.println(str); // 0,1,2,3  
  36.         }  
  37.   
  38.         // 查询  
  39.         // 返回位置  
  40.         System.out.println(arrayList.indexOf("1")); // 1  
  41.         // 返回指定位置的对象  
  42.         System.out.println(arrayList.get(1)); // 1  
  43.         // 判断对象是否存在  
  44.         System.out.println(arrayList.contains("3")); // true  
  45.   
  46.         // 修改  
  47.         System.out.println(arrayList.set(1"11"));  
  48.   
  49.         // 删除  
  50.         arrayList.remove(0); // remove index  
  51.         arrayList.remove(String.valueOf(2)); // remove obj  
  52.   
  53.         // JDK 1.5 后的遍历方式  
  54.         for (String str : arrayList) {  
  55.             System.out.println(str); // 11,3  
  56.         }  
  57.   
  58.         // 原始迭代器  
  59.         ListIterator<String> listIterator = arrayList.listIterator();  
  60.         String string = null;  
  61.         while (listIterator.hasNext()) {  
  62.             string = (String) listIterator.next();  
  63.             System.out.println(string);  
  64.         }  
  65.   
  66.         // 转换为数组  
  67.         // 此方法在多线程同时修改 {@code List} 的情况下可能抛出异常  
  68.         // String[] l = (String[])arrayList.toArray(new  
  69.         // String[arrayList.size()]);  
  70.   
  71.         // 其它的一些方法  
  72.         arrayList.size(); // 获取大小  
  73.         arrayList.clear(); // 清空  
  74.         arrayList.isEmpty(); // 是否为空  
  75.         arrayList.subList(01); // 截取  
  76.           
  77.         arrayList.lastIndexOf("0");   //返回指定的对象在列表中最后一次出现的位置索引。  
  78.         arrayList.trimToSize();//将此 ArrayList 实例的容量调整为列表的当前大小。  
  79.     }  
  80.   
  81.       
  82.     private void linkedListTest() {  
  83.         // 多个线程同时访问链接的哈希集合时保持同步  
  84.           
  85.         LinkedList<String> list = new LinkedList<String>();  
  86.           
  87.         list.add("a");  
  88.         list.add("b");  
  89.         list.add("c");  
  90.         list.add("d");  
  91.           
  92.         list.addFirst("first");  
  93.         list.addLast("end");  
  94.         list.getFirst();  
  95.         list.getLast();  
  96.         // 其它方法参考ArrayList  
  97.     }  
  98.   
  99.       
  100.     private void hashSetTest() {  
  101.         HashSet<String> hashSet = new HashSet<String>();  
  102.         // 添加数据 注意每次插入的数据是无序的  
  103.         hashSet.add("a");  
  104.         hashSet.add("b");  
  105.         hashSet.add("c");  
  106.         hashSet.add("d");  
  107.   
  108.         // 删除数据  
  109.         hashSet.remove("a");  
  110.   
  111.         // 清空数据  
  112.         hashSet.clear();  
  113.   
  114.         // 查询数据  
  115.         hashSet.contains("a");  
  116.         hashSet.size();  
  117.         hashSet.isEmpty();  
  118.         for (String str : hashSet) {  
  119.             System.out.println(str);  
  120.         }  
  121.   
  122.         // 转换为数组  
  123.         String[] strs = (String[]) hashSet.toArray(new String[0]);  
  124.   
  125.     }  
  126.   
  127.       
  128.     private void linkedHashSetTest() {  
  129.         // 多个线程同时访问链接的哈希集合时保持同步  
  130.           
  131.         LinkedHashSet<String> s = new LinkedHashSet<String>();  
  132.         s.add("a");  
  133.         s.add("b");  
  134.         s.add("c");  
  135.         s.add("d");  
  136.         for (String str : s) {  
  137.             System.out.println(str); // a,b,c,d  
  138.         }  
  139.     }  
  140.   
  141.       
  142.     private void treeSetTest() {  
  143.         TreeSet<Student> s = new TreeSet<Student>(new Student.compareToStudent());  
  144.         s.add(new Student(1"a"));  
  145.         s.add(new Student(2"b"));  
  146.         s.add(new Student(4"d"));  
  147.         s.add(new Student(4"c"));  
  148.         s.first();  
  149.         s.last();  
  150.         //其它tree节点操作参考jdk文档  
  151.         for (Student stu : s) {  
  152.             System.out.println(stu);  
  153.         }  
  154.           
  155.     }  
  156.   
  157.       
  158.     private void concurrentLinkedQueueTest() {  
  159.         ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();  
  160.         concurrentLinkedQueue.add("a");  
  161.         concurrentLinkedQueue.add("b");  
  162.         concurrentLinkedQueue.add("c");  
  163.         concurrentLinkedQueue.offer("d"); // 将指定元素插入到此队列的尾部。  
  164.         concurrentLinkedQueue.peek(); // 检索并移除此队列的头,如果此队列为空,则返回 null。  
  165.         concurrentLinkedQueue.poll(); // 检索并移除此队列的头,如果此队列为空,则返回 null。  
  166.   
  167.         for (String str : concurrentLinkedQueue) {  
  168.             System.out.println(str);  
  169.         }  
  170.     }  
  171.   
  172.       
  173.     private void hashMapTest() {  
  174.         HashMap<Integer, Student> map = new HashMap<Integer, Student>();  
  175.         map.put(1new Student(1"a"));  
  176.         map.put(2new Student(2"a"));  
  177.         map.put(3new Student(3"a"));  
  178.         map.put(4new Student(4"a"));  
  179.   
  180.         System.out.println(map.containsKey(1));// true  
  181.         System.out.println(map.containsValue(new Student(4"a")));// true  
  182.         System.out.println(map.get(2)); // Student [id=2, name=a]  
  183.   
  184.         // 获取所有的 key  
  185.         Set<Integer> keySet = map.keySet();  
  186.         for (Integer i : keySet) {  
  187.             System.out.println(i); // 1,2,3,4  
  188.         }  
  189.         // 遍历 Map  
  190.         Set<Map.Entry<Integer, Student>> entries = map.entrySet();  
  191.         for (Map.Entry<Integer, Student> entry : entries) {  
  192.             System.out.println(entry.getKey() + ":" + entry.getValue());  
  193.         }  
  194.         // 1:Student [id=1, name=a]  
  195.         // 2:Student [id=2, name=a]  
  196.         // 3:Student [id=3, name=a]  
  197.         // 4:Student [id=4, name=a]  
  198.     }  
  199.   
  200.       
  201.     private void hashTableTest() {  
  202.         Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();  
  203.         numbers.put("one"new Integer(1));  
  204.         numbers.put("two"new Integer(2));  
  205.         numbers.put("three"new Integer(3));  
  206.   
  207.         Integer n = (Integer) numbers.get("two");  
  208.         if (n != null) {  
  209.             System.out.println("two = " + n);  
  210.         }  
  211.         // 其操作方法参考 HashMap  
  212.     }  
  213.   
  214.       
  215.     private void treeMapTest() {  
  216.         TreeMap<String, Integer> numbers = new TreeMap<String, Integer>();  
  217.         numbers.put("one"new Integer(1));  
  218.         numbers.put("two"new Integer(2));  
  219.         numbers.put("three"new Integer(3));  
  220.         // 其操作方法参考jdk文档  
  221.     }  
  222.   
  223.       
  224.     private void propertiesTest() {  
  225.         Properties properties = new Properties();  
  226.         OutputStream os = null;  
  227.         try {  
  228.             os = new FileOutputStream(getClass().getResource("test.properties")  
  229.                     .getFile());  
  230.         } catch (FileNotFoundException e1) {  
  231.             // TODO Auto-generated catch block  
  232.             e1.printStackTrace();  
  233.         }  
  234.         try {  
  235.             properties.load(getClass().getResourceAsStream("test.properties"));  
  236.             properties.getProperty("key");  
  237.             properties.getProperty("key""default");  
  238.             properties.setProperty("new key""new value");  
  239.             properties.store(os, "comment");  
  240.             os.close();  
  241.         } catch (IOException e) {  
  242.             e.printStackTrace();  
  243.         }  
  244.   
  245.     }  
  246.   
  247.       
  248.     public static void main(String[] args) {  
  249.         // 测试 ArrayList  
  250.         // TypeTest.arrayListTest();  
  251.         // TypeTest.hashSetTest();  
  252.         // TypeTest.linkedHashSetTest();  
  253.         new TypeTest().hashMapTest();  
  254.     }  
  255.   
  256. }  
  257.   
  258. class Student implements Comparable {  
  259.     private int id;  
  260.     private String name;  
  261.   
  262.     public Student(int id, String name) {  
  263.         super();  
  264.         this.id = id;  
  265.         this.name = name;  
  266.     }  
  267.   
  268.     public int getId() {  
  269.         return id;  
  270.     }  
  271.   
  272.     public void setId(int id) {  
  273.         this.id = id;  
  274.     }  
  275.   
  276.     public String getName() {  
  277.         return name;  
  278.     }  
  279.   
  280.     public void setName(String name) {  
  281.         this.name = name;  
  282.     }  
  283.   
  284.     @Override  
  285.     public String toString() {  
  286.         return "Student [id=" + id + ", name=" + name + "]";  
  287.     }  
  288.   
  289.     @Override  
  290.     public int hashCode() {  
  291.         final int prime = 31;  
  292.         int result = 1;  
  293.         result = prime * result + id;  
  294.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  295.         return result;  
  296.     }  
  297.   
  298.     @Override  
  299.     public boolean equals(Object obj) {  
  300.         if (this == obj)  
  301.             return true;  
  302.         if (obj == null)  
  303.             return false;  
  304.         if (getClass() != obj.getClass())  
  305.             return false;  
  306.         Student other = (Student) obj;  
  307.         if (id != other.id)  
  308.             return false;  
  309.         if (name == null) {  
  310.             if (other.name != null)  
  311.                 return false;  
  312.         } else if (!name.equals(other.name))  
  313.             return false;  
  314.         return true;  
  315.     }  
  316.   
  317.     static class compareToStudent implements Comparator // 定义一个内部类来实现比较器  
  318.     {  
  319.         public int compare(Object o1, Object o2) {  
  320.             Student s1 = (Student) o1;  
  321.             Student s2 = (Student) o2;  
  322.             int rulst = s1.id > s2.id ? 1 : (s1.id == s2.id ? 0 : -1);  
  323.             if (rulst == 0) {  
  324.                 rulst = s1.name.compareTo(s2.name);  
  325.             }  
  326.             return rulst;  
  327.         }  
  328.     }  
  329.   
  330.     @Override  
  331.     public int compareTo(Object o) {  
  332.         int result;  
  333.         Student s = (Student) o;  
  334.         result = id > s.id ? 1 : (id == s.id ? 0 : -1);  
  335.         if (result == 0) {  
  336.             result = name.compareTo(s.name);  
  337.         }  
  338.         return result;  
  339.   
  340.     }  
  341.   
  342. }  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!