Collection集合常用API
By
xyp-hf
Update date:
欢迎访问CSDN博客专栏CSDN专栏 Java全栈之路,Github主页。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public class TestCollection {
public static void main(String[] args) { Collection c1 = new ArrayList(); boolean b1 = c1.add(new Integer(1)); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); b1 = c1.add(new String("two")); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); b1 = c1.add(new Student(1001, "zhangfei", 30)); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); System.out.println("--------------------------------------"); Collection c2 = new ArrayList(); c2.add(3); System.out.println("c2 = " + c2); c2.add("four"); System.out.println("c2 = " + c2); System.out.println("当前集合的元素个数是:" + c2.size()); System.out.println("当前集合的元素个数是:" + c1.size()); System.out.println("--------------------------------------"); b1 = c1.addAll(c2); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("--------------------------------------"); b1 = c1.contains(new Integer(1)); System.out.println("b1 = " + b1); b1 = c1.contains(new Integer(2)); System.out.println("b1 = " + b1); b1 = c1.contains(new String("two")); System.out.println("b1 = " + b1); b1 = c1.contains(new Student(1001, "zhangfei", 30)); System.out.println("b1 = " + b1); System.out.println("--------------------------------------"); b1 = c1.contains(c2); System.out.println("b1 = " + b1); System.out.println("--------------------------------------"); b1 = c1.remove(new String("2")); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); b1 = c1.remove(new String("two")); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); System.out.println("--------------------------------------"); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); b1 = c1.removeAll(c2); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("--------------------------------------"); System.out.println("--------------------------------------"); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); b1 = c1.retainAll(c2); System.out.println("b1 = " + b1); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("--------------------------------------"); b1 = c2.retainAll(c2); System.out.println("b1 = " + b1); System.out.println("c2 = " + c2); }
}
|