new collection java_Java Collection集合的基本操作

Collection接口常用方法

int size();//集合大小

boolean isEmpty();//是否为空

boolean contains(Object o);//是否包含某个元素

Iterator iterator();//返回迭代器对象

Object[] toArray();//转为数组

boolean add(E e);//添加元素

boolean remove(Object o);//删除元素

boolean containsAll(Collection> c);//是否包含集合c中的全部元素

boolean addAll(Collection extends E> c);//将集合c中的全部元素添加进来

boolean removeAll(Collection> c);//如果有存在于集合c中的元素则删除

void clear();//清空集合

存放String类型

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class Collection1 {

public static void main(String[] args) {

Collection collection = new ArrayList<>();

//添加

collection.add("Java1");

collection.add("Java2");

collection.add("Java3");

collection.add("Java4");

//遍历

//增强for

System.out.println("**增强for**");

for (String string : collection) {

System.out.println(string);

}

//迭代器

System.out.println("**迭代器iterator**");

Iterator iterator = collection.iterator();

while (iterator.hasNext()){

String s = iterator.next();

System.out.println(s);

//使用Collection的remove会有ConcurrentModificationException

//需要使用Iterator中的方法

// collection.remove("Java1");

// iterator.remove();

}

System.out.println("**判断方法**");

System.out.println("大小:"+collection.size());

System.out.println("包含:"+collection.contains("Java1"));

System.out.println("是否为空:"+collection.isEmpty());

System.out.println("**其他操作**");

//转为数组 方法一

//方法一不用考虑初始化数组的大小,但不能转为特定类型的数组

Object[] array = collection.toArray();

System.out.println(array.length);

//转为数组 方法二

//需要指定初始化数组的大小

//数组过大,大的部分会赋值为null,过小则全部赋值为null

String[] strs = new String[5];

collection.toArray(strs);

for (String str : strs) {

System.out.println(str);

}

}

}

**增强for**

Java1

Java2

Java3

Java4

**迭代器iterator**

Java1

Java2

Java3

Java4

**判断方法**

大小:4

包含:true

是否为空:false

**其他操作**

4

Java1

Java2

Java3

Java4

null

存放自建对象

public class Student {

private String name;

private int age;

public Student(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Student student = (Student) o;

return age == student.age &&

Objects.equals(name, student.name);

}

@Override

public int hashCode() {

return Objects.hash(name, age);

}

}

public class Collection2 {

public static void main(String[] args) {

Collection collection = new ArrayList<>();

//添加

collection.add(new Student("Java1",18));

collection.add(new Student("Java2",19));

collection.add(new Student("Java3",20));

collection.add(new Student("Java4",21));

//遍历

//增强for

System.out.println("**增强for**");

for (Student student : collection) {

System.out.println(student);

if (student.getName().equals("Java1")){

student.setAge(0);

}

}

System.out.println(collection);

//迭代器

System.out.println("**迭代器iterator**");

Iterator iterator = collection.iterator();

while (iterator.hasNext()){

Student student = iterator.next();

System.out.println(student);

}

System.out.println("**判断方法**");

System.out.println("大小:"+collection.size());

System.out.println("包含:"+collection.contains(new Student("Java2",19)));

System.out.println("是否为空:"+collection.isEmpty());

// System.out.println("**其他操作**");

//转为数组 方法一

//方法一不用考虑初始化数组的大小,但不能转为特定类型的数组

// Object[] array = collection.toArray();

// System.out.println(array.length);

//转为数组 方法二

//需要指定初始化数组的大小

//数组过大,大的部分会赋值为null,过小则全部赋值为null

// String[] strs = new String[5];

// collection.toArray(strs);

// for (String str : strs) {

// System.out.println(str);

// }

}

}

**增强for**

Student{name='Java1', age=18}

Student{name='Java2', age=19}

Student{name='Java3', age=20}

Student{name='Java4', age=21}

[Student{name='Java1', age=0}, Student{name='Java2', age=19}, Student{name='Java3', age=20}, Student{name='Java4', age=21}]

**迭代器iterator**

Student{name='Java1', age=0}

Student{name='Java2', age=19}

Student{name='Java3', age=20}

Student{name='Java4', age=21}

**判断方法**

大小:4

包含:true

是否为空:false

使用boolean contains(Object o);方法时,传入的对象要重写equals方法

Collection和Collections的区别

java.util.Collection 是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式,其直接继承接口有List与Set。

Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值