Collection
Collection中的主要方法
1.add
功能说明:向集合中添加元素
Collection <String> coll = new ArrayList<>();
System.out.println(coll);
coll.add("张三");
coll.add("李四");
coll.add("王武");
coll.add("赵六");
System.out.println(coll);//已经重写了toString方法
2.remove
功能说明:用来将集合中的某项元素移除
Collection <String> coll = new ArrayList<>();
coll.remove("王武");
System.out.println(coll);
3.contains (这里返回的是一个布尔值)
功能说明:用来验证集合中是否含有某项元素
boolean flag = coll.contains("王武");
System.out.println(flag);
boolean flag2 = coll.contains("赵六");
System.out.println(flag2);
4.isEmpty
功能说明:用来判断是否为空,如果为空返回ture,否则返回false
boolean flag3 = coll.isEmpty();
System.out.println(flag3);
5.size
功能说明:用来记录集合的大小,即其中所含元素的个数
int size = coll.size();
System.out.println(size);
6.toArray
功能说明:用来将集合转化为数组
Collection <String> coll = new ArrayList<>();
System.out.println(coll);
coll.add("张三");
coll.add("李四");
coll.add("王武");
coll.add("赵六");
Object[] arr = coll.toArray();
System.out.print("[");
for (int i = 0; i <arr.length ; i++) {
if (i==arr.length-1){
System.out.print(arr[i]);
}else{
System.out.print(arr[i]+",");
}
}
System.out.print("]");
}
测试结果:
Collections
Collections中的主要方法
1.addAll
功能描述:该方法是向集合中一次添加多个元素
List <Integer> list =new ArrayList<>();
Collections.addAll(list,1,2,3,4,5);
System.out.println(list);
2.shuffle
功能描述:用来打乱集合中的元素,使其随机排列,一般比如斗地主之类将牌随机打乱等会用到该方法
Collections.shuffle(list);
System.out.println(list);
3.sort
功能描述:该方法为排序方法,将集合内的元素按照一定的顺序排序,不过要注意的是该方法内部已经实现了Comparable接口, 如果自己使用sort方法比较其他的数据类型的时候,应该实现Comparable接口
Collections.sort(list);
System.out.println(list);
若是想要比较自己创建的引用类型,需要实现Comparable接口,重写compareTo方法,例如下面直接建立一个Person类进行测试
package com.massz;
import java.io.Serializable;
import java.util.Objects;
/**
* @Author: mcc
* @Date: 2021/7/30
* @Description: com.massz
* @version: 1.0
*/
public class Person implements Serializable,Comparable<Person> {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age;
}
@Override
public int hashCode() {
return Objects.hash(age);
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
/*
*按照年龄排序的自定义方法
*/
@Override
public int compareTo(Person o) {
return this.getAge()-o.getAge();
}
}
Test类:
List <Person> list =new ArrayList<>();
list.add(new Person(56,"zs"));
Collections.addAll(list,new Person(12,"zhangsan"),new Person(23,"listi"));
Collections.sort(list);
System.out.println(list);
for (Person person : list) {
System.out.println(person);
}
测试结果,可以按照年龄正确排序
简述Comparable和Comparator两个接口的区别。
Comparable:强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法 被称为它的自然比较方法。只能在类中实现compareTo()一次,不能经常修改类的代码实现自己想要的排序。实现 此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序,对象可以用作有序映射中 的键或有序集合中的元素,无需指定比较器。
Comparator强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或 有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。
集合的遍历(迭代器的使用)
Collection<String> coll = new ArrayList<>();
coll.add("张三");
coll.add("李四");
coll.add("王武");
coll.add("赵六");
System.out.print("[");
Iterator<String> it = coll.iterator();
while (it.hasNext()){
String next = it.next();
System.out.print(next);
}
集合的遍历也可以使用增强for
Collection<String> coll = new ArrayList<>();
coll.add("张三");
coll.add("李四");
coll.add("王武");
coll.add("赵六");
for (String s : coll) {
System.out.println(s);
}
Collection与Collections有什么区别?
1.Collection是最基本的集合接口,Collection派生了两个子接口,List和Set,分别定义了两种不同的存储方式
2.Collectons是一个包装类,他包含各种有关集合操作的静态方法
3.此类不能实例化,就像是一个工具类,服务于Collection框架