菜单

Step By Step(Java 集合篇)

2019年3月12日 - 金沙编程资讯

ArrayDeque  一种用循环数组达成的双端队列

1.ArrayList   能够动态增进和压缩的目录种类

boolean isExists=false;
    Iterator iterator=set.iterator();
    while(it.hasNext())           {
    String oldStr=it.next();
    if(newStr.equals(oldStr)){
    isExists=true;
    }
    }

    12.   
EnumSet和EnumMap,那七个汇集能够当作是项目参数特化后的Set<E extends
Enum<E>>和Map<E extends
Enum<E>,V>,EnumSet没有显示的构造函数,而是经过一组织工作厂方法来创制了,见如下代码:

HashSet 一种没有重新元素的冬日,冬辰汇集

3.HashSet  没有重新成分的冬天汇集

    Map(映射):
    Map
是一种把键对象和值对象映射的汇聚,它的每3个因素都包蕴一对键指标和值对象。
    Map没有持续于Collection接口
    从Map集合中检索成分时,只要给出键对象,就会回来对应的值对象。
    Map 的常用方法:     1 增进,删除操作:
    Object put(Object key, Object value): 向集合中参澳成分
    Object remove(Object key):   删除与KEY相关的因素
    void putAll(Map t):   以往自特定影像的具备因素添加给该影像
    void clear(): 从影像中剔除全数映射
    2 查询操作:
    Object get(Object key): 获得与第三字key相关的值
   
Map集合中的键对象不允许再度,也就说,任意四个键对象通过equals()方法比较的结果都以false.
    可是能够将随意多个键独享映射到同1个值对象上。
    Conllections : 集合实用类
    Conllections提供了供JAVA集合实用的静态方法
    总结:    
JAVA集合的主导用法,都总结了,下边这个是平凡最常用的JAVA集合,具体的别样的,还要参考JDK帮助文书档案了,呵呵
关于 Map的利用,还有不少,具体就是以此,Conllections提供了众多 List /Map
实用的艺术,对平庸开发格外管用。

 1     public static void showSubMapAndHeadMapAndTailMap() {
 2         TreeMap<String, String> sortedMap = new TreeMap<String, String>();
 3         sortedMap.put("Adobe", "Mountain View, CA");
 4         sortedMap.put("IBM", "White Plains, NY");
 5         sortedMap.put("Learning Tree", "Los Angeles, CA");
 6         sortedMap.put("Microsoft", "Redmond, WA");
 7         sortedMap.put("Netscape", "Mountain View, CA");
 8         sortedMap.put("O'Reilly", "Sebastopol, CA");
 9         sortedMap.put("Sun", "Mountain View, CA");
10         System.out.println(sortedMap);
11         //firstKey and lastKey 是SortedMap中提供的方法,HashMap中没有。
12         String low = sortedMap.firstKey(), high = sortedMap.lastKey();
13         System.out.println(low);
14         System.out.println(high);
15         Iterator<String> it = sortedMap.keySet().iterator();
16         int i = 0;
17         while (it.hasNext()) {
18             if (i == 3)
19                 low = it.next();
20             if (i == 6)
21                 high = it.next();
22             else
23                 it.next();
24             i++;
25         }
26         System.out.println(low);
27         System.out.println(high);
28         //以下3个方法也是SortedMap中提供的方法,HashMap中没有。
29         System.out.println(sortedMap.subMap(low, high));
30         System.out.println(sortedMap.headMap(high));
31         System.out.println(sortedMap.tailMap(low));
32     }

TreeSet 一种有序集

5.HashMap  键值对涉及的数据结构

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

 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 一种能够动态拉长和减弱的目录体系

13.IdentityHashMap   用==而不是equals比较键值的映射表

  (1)
使用办法iterator()供给容器再次来到多少个Iterator。第三回调用Iterator的next()方法时,它回到体系的首先个要素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。

  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     }

PriorityQueue 一种能够非常的慢去除最小成分的聚合

2.LinkedList 能够在其余任务急速插入和删除错左的平稳体系

  (3)
使用hasNext()检查系列中是否还有成分。

 1     public static void main(String[] args) {
 2         List<Integer> v1 = new ArrayList<Integer>();
 3         for (int i = 0; i < 10; ++i)
 4             v1.add(i);
 5         
 6         List<Integer> l1 = new LinkedList<Integer>();
 7         for (int i = 0; i < 20; ++i)
 8             l1.add(i);
 9         
10         Collections.sort(v1);
11         Collections.sort(l1);
12         Collections.binarySearch(v1, 5);
13         Collections.binarySearch(l1, 5);
14     }

HashMap 一种存款和储蓄key:value关联的炫耀

10.PriorityQueue  同意高效去除最小成分的会见

    Set 的 add()方法是怎样判定指标是不是曾经存放在集合中?

 1     public static void showGetAndPut() {
 2         HashMap<String, Double> hm = new HashMap<String, Double>();
 3         hm.put("A", new Double(3.34));
 4         hm.put("B", new Double(1.22));
 5         hm.put("C", new Double(1.00));
 6         hm.put("D", new Double(9.22));
 7         hm.put("E", new Double(-19.08));
 8         //随机的获取HashMap中的数据
 9         Set<Map.Entry<String, Double>> set = hm.entrySet();
10         for (Map.Entry<String, Double> me : set) {
11             System.out.print(me.getKey() + ": ");
12             System.out.println(me.getValue());
13         }
14         double balance = hm.get("A");
15         //输出1003.34
16         hm.put("A", balance + 1000);
17         System.out.println(hm.get("A"));
18     }
19     
20     public static void showContainsAndRemove() {
21         HashMap<String, String> hashMap = new HashMap<String, String>();
22         hashMap.put("1", "One");
23         hashMap.put("2", "Two");
24         hashMap.put("3", "Three");
25         System.out.println(hashMap.containsValue("Three"));
26         String str = hashMap.remove("2");
27         System.out.println("old value of 2 = " + str);
28         System.out.println(hashMap.containsValue("Two"));
29         Set<String> st = hashMap.keySet();
30         st.remove("1");
31         System.out.println("The size is equal to " + hashMap.size());
32         System.out.println(hashMap.containsKey("3"));
33     }
34     /* 结果如下
35         true
36         old value of 2 = Two
37         false 
38         The size is equal to 1 
39         true */
40         
41     public static void showIterator() {
42         String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter"
43             , "Saturn", "Uranus", "Neptune", "Pluto" };
44         float diameters[] = { 4800f, 12103.6f, 12756.3f, 6794f, 142984f
45             , 120536f, 51118f, 49532f, 2274f };
46         Map<String,Float> map = new HashMap<String,Float>();
47         for (int i = 0, n = names.length; i < n; i++)
48             map.put(names[i], new Float(diameters[i]));
49         Iterator<String> it = map.keySet().iterator();
50         while (it.hasNext()) {
51             String str = it.next();
52             System.out.println(str + ": " + map.get(str));
53         }
54     }
55 
56     public static void showSynchronizedMap() {
57         TreeMap<String,String> treeMap = new TreeMap<String,String>();
58         Map<String,String> map = Collections.synchronizedMap(treeMap);
59         //Map之后的并发操作将不再需要synchronized关键字来进行同步了。
60     }

TreeMap 一种key有序的映射

11.LinkedHashMap  能够记住键值添加次序的映射表

2.java set

 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     }    

相关文章

发表评论

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

网站地图xml地图