集合接口Collection案例
来源:互联网 发布:手机红外夜视仪软件 编辑:程序博客网 时间:2024/05/16 19:16
以java8为基础。
package com.neutron.collection.demo;import java.util.ArrayList;import java.util.List;/** * collection接口说明 * @author zhanght * */@SuppressWarnings({"rawtypes", "unchecked"})public class CollectionDemo {public static void main(String[] args) {insert();containsAndRemove();toArray();}/** * 使用collection中添加方法 */public static void insert() {ArrayList arr = new ArrayList();System.out.println("arr is empty ? " + arr.isEmpty());/** * 判断list是否为空,主要使用size()方法判断是否为0 * public boolean isEmpty() { return size == 0; } */arr.add(123);arr.add("Str");arr.add(true);// insert not replacearr.add(1, "replace Str"); //集合框架元素个数size方法System.out.println("arr before size: " + arr.size());//arr size: 4System.out.println("arr before: " + arr);//arr[123, replace Str, Str, true]List list = new ArrayList();list.add("here");list.add(123);arr.addAll(list);System.out.println("arr size: after: " + arr.size());//arr size: after: 6System.out.println("arr after: " + arr);//arr after: [123, replace Str, Str, true, here, 123]arr.addAll(0, list);System.out.println("arr end: " + arr);//arr end: [here, 123, 123, replace Str, Str, true, here, 123]}/** * 使用集合类中的判断和删除方法 */public static void containsAndRemove() {ArrayList arr = new ArrayList();arr.add(124);arr.add(0);arr.add(true);arr.add(1, "replace Str"); List list = new ArrayList();list.add("here");list.add(123);arr.addAll(list);System.out.println("arr中是否存在123: " + arr.contains(123));//arr中是否存在123: trueList list2 = new ArrayList();list2.add("here");list2.add(123);System.out.println("arr中是否存在list2: " + arr.containsAll(list2));//arr中是否存在list2: trueif (arr.contains(true)) {arr.remove(0);sop("arr remove(0) remove 123 or remove 0", arr);// arr remove(0) remove 123 or remove 0[replace Str, 0, true, here, 123]// we can know delete index 0, not data 0}arr.addAll(0, list2);arr.addAll(list2);sop("add 2times list2 result: ", arr);// add 2times list2 result: [here, 123, replace Str, 0, true, here, 123, here, 123]if (arr.containsAll(list2)) {arr.removeAll(list2);sop("remove list2 result: ", arr);// remove list2 result: [replace Str, 0, true] 结果表明重复数据都删掉}// 根据条件去删除数据,java8新特性,使用lamda表达式arr.removeIf(obj -> obj.toString().startsWith("re"));sop("removeIf: ", arr);/* * default boolean removeIf(Predicate<? super E> filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator<E> each = iterator(); while (each.hasNext()) { if (filter.test(each.next())) { each.remove(); removed = true; } } return removed; } */// 清除collection中的数据arr.clear();sop("after clear: ", arr);}/** * 集合编程数组 */public static void toArray() {List arr = new ArrayList();arr.add(17);arr.add(12);arr.add(13);arr.add(14);arr.add(15);arr.add(16);Object[] nums = arr.toArray();sop("to array: ", nums);// [Ljava.lang.Object;@1fb3ebeb// we can see, if data is collection, println data is real data!// if array, we can see addressList list2 = new ArrayList();list2.add(new Person("name1"));list2.add(new Person("name2"));list2.add(new Person("name3"));list2.add(new Person("name4"));/* * toArray(T[] a) * 方法作用是,将调用此方法转化成数组的数据存储在数组a中 */Person[] persons = new Person[list2.size()];list2.toArray(persons);sop("T[] toArray: ", persons);for (Person p : persons) {sop("--:", p);}/* * name1name2name3name4 * * */}public static void sop(String msg, Object obj) {System.out.println(obj.toString());}}class Person {public String name;public Person(String name) {this.name = name;}@Overridepublic String toString() {return this.name;}}
0 0
- 集合接口Collection案例
- 集合框架-Collection接口
- Java Collection集合接口
- Collection接口集合方法
- java 集合Collection接口
- Collection接口,集合框架
- 集合collection 接口
- Collection--Java中的collection(集合)接口综述
- 集合框架之Collection接口
- 集合及Collection接口简介
- 10002--Java集合--Collection 接口
- Java集合之Collection接口
- Java集合之Collection接口
- 集合:根接口Collection Part1
- 集合:根接口Collection Part2
- Collection接口下的集合
- collection集合接口子类---set接口
- 集合接口collection与Map接口
- JAVA基础之异常处理
- 大话设计模式-命令模式
- 即时通讯协议 MQTT
- 让Grub 2来拯救你的 bootloader
- Android控件架构与自定义控件详解(一)——Android控件架构与View的绘制原理
- 集合接口Collection案例
- Android Studio: Missing 32 bit compatbility library
- POJ 1160 DP
- 1.xctx文本编辑器
- 小米S2手机刷android5.0原生版
- cPanel下如何设定流量统计工具
- SQL优化--发现问题
- spring boot搭建多数据源,记录一下
- 哈夫曼树与哈夫曼编码