<b>java的collections调集</b>[Java编程]
本文“<b>java的collections调集</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
下面这张表格(表一)总结了用一个调集能做的全部事情(亦可对Set和List做一样的事情,固然List还供应了一些额外的功效).Map不是从Collection担当的,所以要单独对待.
boolean add(Object) *保证调集内包含了自变量.假如它没有增添自变量,就返回false(假)
boolean addAll(Collection) *增添自变量内的全部元素.假如没有增添元素,则返回true(真)
void clear() *删除调集内的全部元素
boolean contains(Object) 若调集包含自变量,就返回“真”
boolean containsAll(Collection) 若调集包含了自变量内的全部元素,就返回“真”
boolean isEmpty() 若调集内没有元素,就返回“真”
Iterator iterator() 返回一个反复器,以用它遍历调集的各元素
boolean remove(Object) *如自变量在调集里,就删除那个元素的一个实例.假如已举行了删除,就返回“真”
boolean removeAll(Collection) *删除自变量里的全部元素.假如已举行了任何删除,就返回“真”
boolean retainAll(Collection) *只保存包含在一个自变量里的元素(一个理论的“交集”).假如已举行了任何改变,就返回“真”
int size() 返回调集内的元素数目
Object[] toArray() 返回包含了调集内全部元素的一个数组
Boolean add(Object) |
*Ensures that the Collection contains the argument. Returns false if it doesn’t add the argument. |
Boolean addAll(Collection) |
*Adds all the elements in the argument. Returns true if any elements were added. |
void clear() |
*Removes all the elements in the Collection. |
Boolean contains(Object) |
True if the Collection contains the argument. |
Boolean containsAll(Collection) |
True if the Collection contains all the elements in the argument. |
Boolean isEmpty() |
True if the Collection has no elements. |
Iterator iterator() |
Returns an Iterator that you can use to move through the elements in the Collection. |
Boolean remove(Object) |
*If the argument is in the Collection, one instance of that element is removed. Returns true if a removal occurred. |
Boolean removeAll(Collection) |
*Removes all the elements that are contained in the argument. Returns true if any removals occurred. |
Boolean retainAll(Collection) |
*Retains only elements that are contained in the argument (an “intersection” from set theory). Returns true if any changes occurred. |
int size() |
Returns the number of elements in the Collection. |
Object[] toArray() |
Returns an array containing all the elements in the Collection. |
Object[] toArray(Object[] a) |
Returns an array containing all the elements in the Collection, whose type is that of the array a rather than plain Object (you must cast the array to the right type). |
*This is an “optional” method, which means it might not be implemented by a particular Collection. If not, that method throws an UnsupportedOperationException. Exceptions will be covered in Chapter 9. |
表一
*这是一个“可选的”办法,有的调集大概并未实现它.若确切如此,该办法就会碰到一个UnsupportedOperatiionException,即一个“操作不支持”违例,详见第9章.
下面这个例子向大家演示了全部办法.一样地,它们只对从调集担当的东西有效,一个ArrayList作为一种“不常用的分母”利用:
通过第一个办法,我们可用测试数据填充当何调集.在当前这种情形下,只是将int转换成String.第二个办法将在本章别的的部份常常采取.//: Collection1.java // Things you can do with all Collections package c08.newcollections; import java.util.*; public class Collection1 { // Fill with 'size' elements, start // counting at 'start': public static Collection fill(Collection c, int start, int size) { for(int i = start; i < start + size; i++) c.add(Integer.toString(i)); return c; } // Default to a "start" of 0: public static Collection fill(Collection c, int size) { return fill(c, 0, size); } // Default to 10 elements: public static Collection fill(Collection c) { return fill(c, 0, 10); } // Create & upcast to Collection: public static Collection newCollection() { return fill(new ArrayList()); // ArrayList is used for simplicity, but it's // only seen as a generic Collection // everywhere else in the program. } // Fill a Collection with a range of values: public static Collection newCollection(int start, int size) { return fill(new ArrayList(), start, size); } // Moving through a List with an iterator: public static void print(Collection c) { for(Iterator x = c.iterator(); x.hasNext();) System.out.print(x.next() + " "); System.out.println(); } public static void main(String[] args) { Collection c = newCollection(); c.add("ten"); c.add("eleven"); print(c); // Make an array from the List: Object[] array = c.toArray(); // Make a String array from the List: String[] str = (String[])c.toArray(new String[1]); // Find max and min elements; this means // different things depending on the way // the Comparable interface is implemented: System.out.println("Collections.max(c) = " + Collections.max(c)); System.out.println("Collections.min(c) = " + Collections.min(c)); // Add a Collection to another Collection c.addAll(newCollection()); print(c); c.remove("3"); // Removes the first one print(c); c.remove("3"); // Removes the second one print(c); // Remove all components that are in the // argument collection: c.removeAll(newCollection()); print(c); c.addAll(newCollection()); print(c); // Is an element in this Collection System.out.println( "c.contains(\"4\") = " + c.contains("4")); // Is a Collection in this Collection System.out.println( "c.containsAll(newCollection()) = " + c.containsAll(newCollection())); Collection c2 = newCollection(5, 3); // Keep all the elements that are in both // c and c2 (an intersection of sets): c.retainAll(c2); print(c); // Throw away all the elements in c that // also appear in c2: c.removeAll(c2); System.out.println("c.isEmpty() = " + c.isEmpty()); c = newCollection(); print(c); c.clear(); // Remove all elements System.out.println("after c.clear():"); print(c); } } ///:~
newCollection()的两个版本都成立了ArrayList,用于包含差别的数据集,并将它们作为调集对象返回.所以很明显,除了Collection接口之外,不会再用到其他什么.
print()办法也会在本节常常用到.由于它用一个反复器(Iterator)在一个调集内遍历,而任何调集都可以产生这样的一个反复器,所以它实用于List和Set,也实用于由一个Map生成的Collection.
main()用简单的手段显示出了调集内的全部办法.
在后续的小节里,我们将对比List,Set和Map的差别实现筹划,同时指出在各种情形下哪一种筹划应成为首选(带有星号的那个).大家会发现这里并未包含一些传统的类,如Vector,Stack以及Hashtable等.因为不管在什么情形下,新调集内都有自己首选的类.
以上是“<b>java的collections调集</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |