Step By Step(Java 集合篇)

LinkedHashMap 1种能够记住插入次序的炫耀

7.  EunmMap  枚举类型的映射表

    Set 的 add()方法是哪些判断目的是否早已存放在联谊中?

    8.    PriorityQueue(优先级对象):
该容器也是因循古板聚集,和TreeSet区别的是,该集合只是保险当从集合的尾部取出数据的时候,总是取出队列中细小(优先级最高)的因素。该集合内部是通
过”堆”的数据结构完毕的,该组织唯有首先个元素(底部成分)保险为该集合中最大的或一点都不大的要素,别的成分未有一贯的次第,然而当集合有新的靶子从尾巴部分插
入或是从头顶取出时,集合内部的连带要素会议及展览开相比的相比较最终再度决定出何人是最大或一点都不大的因素作为底部元素。在JDK提供的classes中Timer是
通过该数据结构达成的,从而保障了Timer每回得到职责时都以最应当被调度的TimerTask,见如下代码:

HashMap 一种存款和储蓄key:value关联的投射

捌.EnumSet   包蕴枚举类型值的值

 list l = new ArrayList();
 l.add("aa");
 l.add("bb");
 l.add("cc");
 for (Iterator iter = l.iterator(); iter.hasNext();) {
  String str = (String)iter.next();
  System.out.println(str);
 }
 /*迭代器用于while循环
 Iterator iter = l.iterator();
 while(iter.hasNext()){
  String str = (String) iter.next();
  System.out.println(str);
 }
 */

    九.   
HashMap:其里面数据结构的逻辑类似于HashSet,只是Map存款和储蓄的都以key/value的键值映射关系,key相当于HashSet中的对
象,value只是附着于key的靶子,使用者一般都是通过key的值去HashMap中搜索,找到并赶回该key以及相关联的value,其里面贯彻机
制可参考HashSet。在接口方面重点的分裂点是Map对象足以扭转二个聚众的视图(仅仅是视图,未有对象copy,视图的底层容器依然来自于原映射对
象集合,假设在内部任意视图或原集合删除和丰盛数据都会立马反馈与任何视图和照耀集合)。该规则1样适用于TreeMap。见之下常用代码示例:

LinkedList 一种能够在别的地点进行高效地插入和删除操作的平稳系列

三.HashSet  未有再度成分的冬日聚集

JAVA集合能够储存和操作数目不固定的1组数据。

 1     public static void main(String[] a) {
 2         Map<String, String> map = new LinkedHashMap<String, String>();
 3         map.put("1", "value1");
 4         map.put("2", "value2");
 5         map.put("3", "value3");
 6         map.put("2", "value4");
 7         for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
 8             String key = it.next();
 9             String value = map.get(key);
10             System.out.println(value);
11         }
12     }
13     /* 结果如下:
14         value1
15         value4
16         value3 */
17 
18     //基于Values的排序后输出Keys
19     public static void main(String[] a) {
20         Map<String, String> yourMap = new HashMap<String, String>();
21         yourMap.put("1", "one");
22         yourMap.put("2", "two");
23         yourMap.put("3", "three");
24 
25         Map<String, String> map = new LinkedHashMap<String, String>();
26         List<String> keyList = new ArrayList<String>(yourMap.keySet());
27         List<String> valueList = new ArrayList<String>(yourMap.values());
28         Set<String> sortedSet = new TreeSet<String>(valueList);
29         String[] sortedArray = sortedSet.toArray(new String[0]);
30         int size = sortedArray.length;
31 
32         for (int i = 0; i < size; i++)
33             map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]);
34 
35         Set<String> ref = map.keySet();
36         Iterator<String> it = ref.iterator();
37         while (it.hasNext()) {
38             String i = (String) it.next();
39             System.out.println(i);
40         }
41     }    

HashSet 一种未有重新成分的冬辰集聚

九.ArrayQueue  循环数组完结的双端队列

 

 1     public class TestMain {
 2         public static void main(String[] args) {
 3             PriorityQueue<String> pq = new PriorityQueue<String>();
 4             pq.add("1");
 5             pq.add("6");
 6             pq.add("4");
 7             pq.offer("5");
 8             pq.offer("3");
 9             pq.offer("2");
10             pq.offer("7");
11             //以下输出将以无序的结果输出
12             System.out.println(pq);
13             //以下输出将以有序的结果输出
14             while (pq.peek() != null) {
15                 String str = pq.poll();
16                 System.out.println(str);
17             }
18             int initCapacity = 20;
19             PriorityQueue<TestComparator> pq1 = new PriorityQueue<TestComparator>(initCapacity,
20                 new Comparator<TestComparator>() {
21                     public int compare(TestComparator t1, TestComparator t2) {
22                         return t1.getID() - t2.getID();
23                     }
24                 });
25             pq1.offer(new TestComparator(1));
26             pq1.offer(new TestComparator(6));
27             pq1.offer(new TestComparator(4));
28             pq1.offer(new TestComparator(5));
29             pq1.offer(new TestComparator(3));
30             pq1.offer(new TestComparator(2));
31             pq1.offer(new TestComparator(7));
32             System.out.println("The following is for TestComparator.");
33             System.out.println(pq1);
34             while (pq1.peek() != null) {
35                 int id = pq1.poll().getID();
36                 System.out.println(id);
37             }
38         }
39     }
40     
41     class TestComparator {
42         public TestComparator(int id) {
43             _id = id;
44         }
45         public int getID() {
46             return _id;
47         }
48         public String toString() {
49             return Integer.toString(_id);
50         }
51         private int _id;
52     }

ArrayList 1种能够动态增进和压缩的目录系列

陆.TreeMap  键值有序排列的映射表

  Iterator是Java迭代器最简易的达成,为List设计的ListIterator具有越来越多的功用,它能够从五个趋势遍历List,也得以从List中插入和删除成分。

 1     public static void showNewFeatures(String[] args) {
 2         LinkedList<String> lList = new LinkedList<String>();
 3         lList.add("1");
 4         lList.add("2");
 5         lList.add("3");
 6         lList.add("4");
 7         lList.add("5");
 8         
 9         System.out.println("First element of LinkedList is : " + lList.getFirst());
10         System.out.println("Last element of LinkedList is : " + lList.getLast());
11         System.out.println(lList);
12         
13         String str = lList.removeFirst();
14         System.out.println(str + " has been removed");
15         System.out.println(lList);
16         str = lList.removeLast();
17         System.out.println(str + " has been removed");
18         System.out.println(lList);
19         
20         lList.addFirst("1");
21         System.out.println(lList);
22         lList.addLast("5");
23         System.out.println(lList);    
24     }

LinkHashSet 一种能够记住成分插入次序的汇聚

十.PriorityQueue  允许高效去除最小成分的联谊

 for(int i=0; i<list.size();i++){
    System.out.println(list.get(i));
    }
    2: 使用 迭代器(Iterator):
    Iterator it=list.iterator();
    while(it.hashNext){
    System.out.println(it.next);
    }

   
在Java中平等提供了如此的泛型算法,分化的是这么些算法函数皆以Collections中静态方法。见如下Java代码:

TreeMap 1种key有序的炫耀

14.LinkedHashSet  可以记住插入次序的联谊

  (叁)
使用hasNext()检查连串中是或不是还有成分。

  1     public static void showIterator() {
  2         ArrayList<String> list = new ArrayList<String>();
  3         list.add("Monday");
  4         list.add("Tuesdag");
  5         list.add("Wednesday");
  6         Iterator<String> iterator = null;
  7         iterator = list.iterator();
  8         //while
  9         while (iterator.hasNext()) {
 10             String element = iterator.next();
 11             System.out.println(element);
 12         }
 13         //for
 14         for (iterator = list.iterator(); iterator.hasNext();) {
 15             String element = iterator.next();
 16             System.out.println(element);
 17         }
 18         //for each
 19         for (String element : list) {
 20             System.out.println(element);
 21         }
 22     }
 23 
 24     public static void showSetAndGet() {
 25         ArrayList<String> nums = new ArrayList<String>();
 26         nums.clear();
 27         nums.add("One");
 28         nums.add("Two");
 29         nums.add("Three");
 30         System.out.println(nums);
 31         nums.set(0, "Uno");
 32         nums.set(1, "Dos");
 33         nums.set(2, "Tres");
 34         for (int i = 0; i < nums.size(); ++i)
 35             System.out.println(nums.get(i));
 36     }
 37     
 38     public static void showRemoveAndSize() {
 39         ArrayList<String> al = new ArrayList<String>();
 40         System.out.println("Initial size of al: " + al.size());
 41         al.add("C");
 42         al.add("A");
 43         al.add("E");
 44         al.add("B");
 45         al.add(1, "A2");
 46         System.out.println("Size of al after additions: " + al.size());
 47         System.out.println("Contents of al: " + al);
 48         al.remove("F");
 49         al.remove(2);
 50         System.out.println("Size of al after deletions: " + al.size());
 51         System.out.println("Contents of al: " + al);
 52         Iterator<String> it = al.iterator();
 53         //Notes:remove() must be called after next()
 54         it.next();
 55         it.remove();
 56         System.out.println("Size of al after deletions: " + al.size());
 57         System.out.println("Contents of al: " + al);        
 58     }
 59     
 60     public static void showSubListAndCopyToArray() {
 61         ArrayList<String> arrayList = new ArrayList<String>();
 62         arrayList.add("1");
 63         arrayList.add("2");
 64         arrayList.add("3");
 65         arrayList.add("4");
 66         arrayList.add("5");
 67         List<String> lst = arrayList.subList(1, 3);
 68         for (int i = 0; i < lst.size(); i++)
 69             System.out.println(lst.get(i));        
 70         // remove one element from sub list
 71         String obj = lst.remove(0);
 72         System.out.println(obj + " is removed");
 73         for (String str: arrayList)
 74             System.out.println(str);
 75         //get object array with normal method
 76         Object[] objArray = arrayList.toArray();
 77         for (Object obj1 : objArray)
 78             System.out.println(obj1);
 79         //get object array with generic method
 80         String[] strArray = arrayList.toArray(new String[0]);
 81         for (String str : strArray)
 82             System.out.println(str); 
 83     }
 84     
 85     public static void showListIterator() {
 86         ArrayList<String> aList = new ArrayList<String>();
 87         aList.add("1");
 88         aList.add("2");
 89         aList.add("3");
 90         aList.add("4");
 91         aList.add("5");
 92         
 93         ListIterator<String> listIterator = aList.listIterator();
 94         while (listIterator.hasNext()) {
 95             System.out.println(listIterator.next());
 96             System.out.println("Previous: " + listIterator.previousIndex());
 97             System.out.println("Next: " + listIterator.nextIndex());
 98         }
 99         while (listIterator.hasPrevious()) {
100             System.out.println(listIterator.previous());
101             System.out.println("Previous: " + listIterator.previousIndex());
102             System.out.println("Next: " + listIterator.nextIndex());
103         }
104         listIterator = aList.listIterator(2);
105         listIterator.next();
106         listIterator.set("100");
107         listIterator.next();
108         listIterator.remove();
109         for (String str : aList)
110             System.out.println(str);
111 
112         if (aList.contains("4"))
113             System.out.println("True");
114         else
115             System.out.println("False");
116     }
117     
118     public static void showFillAndReplace() {
119         ArrayList<String> arrayList = new ArrayList<String>();
120         arrayList.add("A");
121         arrayList.add("B");
122         arrayList.add("A");
123         arrayList.add("C");
124         arrayList.add("D");
125         Collections.replaceAll(arrayList, "A", "Replace All");
126         System.out.println(arrayList);
127         Collections.fill(arrayList, "REPLACED");
128         System.out.println(arrayList);
129     }
130 
131     public static void showCollectionOperation() {
132         List<String> colours = new ArrayList<String>();
133         colours.add("red");
134         colours.add("green");
135         colours.add("blue");
136 
137         System.out.println(colours);
138         Collections.swap(colours, 0, 2);
139         System.out.println(colours);
140 
141         Collections.reverse(colours);
142         System.out.println(colours);
143 
144         Collections.sort(colours);
145         System.out.println(Arrays.toString(colours.toArray()));
146         Collections.sort(colours, Collections.reverseOrder());
147         System.out.println(Arrays.toString(colours.toArray()));
148 
149         int index = Collections.binarySearch(colours, "green");
150         System.out.println("Element found at : " + index);
151         ArrayList<Integer> arrayList = new ArrayList<Integer>();
152         arrayList.add(new Integer("3"));
153         arrayList.add(new Integer("1"));
154         arrayList.add(new Integer("8"));
155         arrayList.add(new Integer("3"));
156         arrayList.add(new Integer("5"));
157         System.out.println(Collections.min(arrayList));
158         System.out.println(Collections.max(arrayList));
159     }
160     
161     public static void showMinMax() {
162         ArrayList<Integer> arrayList = new ArrayList<Integer>();
163         arrayList.add(new Integer("3"));
164         arrayList.add(new Integer("1"));
165         arrayList.add(new Integer("8"));
166         arrayList.add(new Integer("3"));
167         arrayList.add(new Integer("5"));
168         System.out.println(Collections.min(arrayList));
169         System.out.println(Collections.max(arrayList));
170     }
171     
172     public static void showSynchronizedList() {
173         ArrayList arrayList = new ArrayList();
174         List list = Collections.synchronizedList(arrayList);
175         //list之后的并发操作将不再需要synchronized关键字来进行同步了。
176     }

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图