标签:接口 顺序 size ima mamicode als list method split
1.Collection接口
// 查Collection公用的方法 Class c = Collection.class;
Method[] methods = c.getMethods();
int n = 1;
for (Method method : methods) {
System.out.print(n++ + " " + method.getName() + " ");
}
查到20个(含重写方法)
1 add 2 remove 3 equals 4 hashCode 5 clear 6 contains 7 isEmpty 8 iterator 9 size 10 toArray 11 toArray 12 spliterator 13 addAll 14 stream 15 containsAll 16 removeAll 17 removeIf 18 retainAll 19 parallelStream 20 forEach
下面每个方法使用一次
// 1、add(E e) 添加 System.out.println("________1________"); Collection<String> col = new ArrayList<>(); col.add("Jack"); System.out.println(col); // 2、remove 删除 System.out.println("________2________"); col.remove("Jack"); System.out.println(col); // 3、equals 判断集合相同(个数相同,位置相同) System.out.println("________3________"); Collection<String> col2 = new ArrayList<String>(); col.add("Jack"); col2.add("Jack"); System.out.println(col.equals(col2)); // 4、hashCode hashCode值 System.out.println("________4________"); System.out.println(col.hashCode()); // 5、clear 清除 System.out.println("________5________"); col.clear(); System.out.println(col); // 6、contains 是否包含 System.out.println("________6________"); col.add("Jack"); System.out.println(col.contains("Jack")); // 7、isEmpty 判断是否为空,空返回true System.out.println("________7________"); System.out.println(col.isEmpty()); // 8、iterator 迭代器 System.out.println("________8________"); col.add("Tom"); col.add("Alice"); System.out.println(col); Iterator<String> iter = col.iterator(); System.out.println(iter.next()); // 9、size 获取大小 System.out.println("________9________"); System.out.println(col.size()); // 10、toArray() 将集合转换为数组 System.out.println("________10________"); Object[] objs = col.toArray(); for (int i = 0; i < objs.length; i++) { System.out.print(objs[i] + " "); } System.out.println(); // 11、toArray(T[] a) 将集合转换为数组 System.out.println("________11________"); String[] strs = col.toArray(new String[] {}); for (String string : strs) { System.out.println(string); } // 12、spliterator 可分割迭代器//了解 System.out.println("________12________"); Spliterator<String> split = col.spliterator(); System.out.println(split.SIZED);// 返回特征值 // 13、addAll(Collection<? extends E> c) 将集合中的所有元素放入指定的集合中 System.out.println("________13________"); col.forEach(u -> System.out.println(u)); System.out.println("__"); col2.forEach(u -> System.out.println(u)); System.out.println("__"); col2.addAll(col); col2.forEach(u -> System.out.println(u)); // 14、stream //流 —— 计算空字符串 System.out.println("________14________"); Collection<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl"); System.out.println("列表: " + strings); long count = strings.stream().filter(string -> string.isEmpty()).count(); System.out.println("空字符串数量为: " + count); // 15、containsAll(Collection<?> c) 判断所有元素是否相同, 只看元素, 不看顺序 System.out.println("________15________"); System.out.println(col.containsAll(col2)); // 16、public boolean removeAll(Collection<?> c) 差集 System.out.println("________16________"); col.removeAll(col2); System.out.println(col); // 17、removeIf 过滤集合中的元素 System.out.println("________17________"); System.out.println(col2); col2.removeIf(name -> name.length() < 4);// 删掉名字长度小于4的 System.out.println(col2); // 18、retainAll(Collection<?> c) 交集 System.out.println("________18________"); System.out.println(col2.retainAll(col)); System.out.println(col2); // 19、parallelStream//了解 System.out.println("________19________"); Collection<String> strings2 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl"); System.out.println("列表: " + strings); long count2 = strings.parallelStream().filter(string -> string.isEmpty()).count(); System.out.println("空字符串数量为: " + count); // 20、forEach 遍历 System.out.println("________20________"); strings2.forEach(u -> System.out.println(u)); }
输出结果:
________1________ [Jack] ________2________ [] ________3________ true ________4________ 2300958 ________5________ [] ________6________ true ________7________ false ________8________ [Jack, Tom, Alice] Jack ________9________ 3 ________10________ Jack Tom Alice ________11________ Jack Tom Alice ________12________ 64 ________13________ Jack Tom Alice __ Jack __ Jack Jack Tom Alice ________14________ 列表: [abc, , bc, efg, abcd, , jkl] 空字符串数量为: 2 ________15________ true ________16________ [] ________17________ [Jack, Jack, Tom, Alice] [Jack, Jack, Alice] ________18________ true [] ________19________ 列表: [abc, , bc, efg, abcd, , jkl] 空字符串数量为: 2 ________20________ abc bc efg abcd jkl
以上。
标签:接口 顺序 size ima mamicode als list method split
原文地址:https://www.cnblogs.com/jzdbwdc/p/12598994.html