●一方面,面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对 象进行存储。另一方面,使用Array存储对象方面具有一些弊端, 而ava 集合就像一种容器, 可以动态地把多个对象的引用放入容器中。
数组在内存存储方面的特点:
--数组初始化以后,长度就确定了
--数组声明的类型,就决定了进行元素初始化时的类型
数组在存储数据方面的弊端:
--数组初始化以后,长度就不可变了,不便于扩展
--数组中提供的属性和方法少,不便于进行添加、删除、插入等操作,且效率不高。同时无法直 接获取存储元素的个数
– 数组存储的数据是有序的、可以重复的。-存储数据的特点单一
●Java集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组。
java集合可以分为Collection和Map两种体系
- Collection接口:单列集合数据,定义了存储一组对象的方法的集合
- List:元素有序,可重复的集合,集合中的每个元素都有其对应的顺序索弓|(动态数组)
- List接口的实现类常用的有: ArrayList, LinkedList和Vector
- Set:存储元素无序,不可重复的集合
- List:元素有序,可重复的集合,集合中的每个元素都有其对应的顺序索弓|(动态数组)
- Map接口:双列数据,保存具有映射关系"key-value对”的集合
1 | boolean contains(object obj) | 通过equals方法来判断是否是同一个对象 |
2 | boolean ContainsAll(Collection coll1) | 判断形参coll1中所有元素是否都存在于当前集合中 |
3 | boolean remove(object obj) | 通过元素的equals方法判断是否是要删除的那个元素,只会删除找到的第一个元素 |
4 | boolean removeAll(Collection coll1) | 从当前集合中移除coll1中所有的元素,即去当前集合的差集 |
5 | boolean retainAll(Collection c) | 相当于取交集,把交集的结果存在当前集合中,不影响c |
6 | boolean equals(Object obj) | 要想返回true,需要当前集合当前集合和形参集合的对应位置元素相同 |
7 | hashCode() | 返回当前对象的哈希值 |
8 | toArray() | 集合—转换为—>数组 |
9 | iterator() | 返回Iterator接口的实例,用于遍历集合元素 |
测试用例:Person.java
import java.util.Objects;
public class Person {
private String name;
private int age;
Person() {}
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName(String name) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(int age) {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{" +
"name=" + name + '\'' +
",age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
System.out.println("Person equals().....");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
// @Override
// public int hashCode() {
// return Objects.hash(name, age);
// }
}
Collection接口中声明的方法的测试
向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()方法
boolean contains(object obj) 通过equals方法来判断是否是同一个对象
boolean ContainsAll(Collection coll1) 判断形参coll1中所有元素是否都存在于当前集合中
public class CollectionTest {
@Test
public void test1() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
// Person p = new Person("mofei", 22);
// coll.add(p);
coll.add(coll.add(new Person("mofei", 22)));
//1,contains(object obj) : 判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()方法
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("mofei")));
// System.out.println(coll.contains(p)); //true
System.out.println(coll.contains(new Person("mofei", 22)));
System.out.println("*********************以下为ContainsAll()方法*************************");
//2,ContainsAll(Collection coll1): 判断形参coll1中所有元素是否都存在于当前集合中
Collection coll1 = Arrays.asList(123, 456);
System.out.println(coll.containsAll(coll1));
}
}
/**
true
true
Person equals().....
Person equals().....
Person equals().....
Person equals().....
Person equals().....
true
*********************以下为ContainsAll()方法*************************
true
*/
boolean remove(object obj) 通过元素的equals方法判断是否是要删除的那个元素,只会删除找到的第一个元素
boolean removeAll(Collection coll1) 从当前集合中移除coll1中所有的元素,即去当前集合的差集
public class CollectionTest {
@Test
public void test2() {
//3, remove(object obj): 从当前集合中移除obj,返回值为true表示移除成功
//通过元素的equals方法判断是否是要删除的那个元素,只会删除找到的第一个元素
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
coll.add(new Person("mofei", 22));
System.out.println(coll.remove(123));
System.out.println(coll);
System.out.println(coll.remove(new Person("mofei", 22)));
System.out.println(coll);
System.out.println("*****************以下为removeAll()方法*****************");
//4, removeAll(Collection coll1): 从当前集合中移除coll1中所有的元素
Collection coll1 = Arrays.asList(123, 456);
coll.removeAll(coll1);
System.out.println(coll);
}
}
/**
[456, mofei, false, Person{name=mofei',age=22}]
Person equals().....
Person equals().....
Person equals().....
Person equals().....
true
[456, mofei, false]
*****************以下为removeAll()方法*****************
[mofei, false]
*/
boolean retainAll(Collection c) 相当于取交集,把交集的结果存在当前集合中,不影响c
public class CollectionTest {
@Test
public void test3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
coll.add(new Person("mofei", 22));
//5, retainAll(): 相当于取交集,获取当前集合与coll1集合得交集,并返回给当前集合,返回值为boolean
Collection coll1 = Arrays.asList(123, 456, 789);
boolean c = coll.retainAll(coll1);
System.out.println(c);
System.out.println(coll);
}
}
/**
Person equals().....
Person equals().....
Person equals().....
true
[123, 456]
*/
boolean equals(Object obj) 要想返回true,需要当前集合当前集合和形参集合的对应位置元素相同
public class CollectionTest {
@Test
public void test3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
coll.add(new Person("mofei", 22));
//6, equals(Object obj):要想返回true,需要当前集合当前集合和形参集合的对应位置元素相同
//ArrayList():产生有序的列表
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(new String("mofei"));
coll1.add(false);
coll1.add(new Person("mofei", 22));
System.out.println(coll.equals(coll1));
}
}
/**
Person equals().....
true
*/
hashCode() 返回当前对象的哈希值
toArray() 集合—转换为—>数组
public class CollectionTest {
@Test
public void test4() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
coll.add(new Person("mofei", 22));
//7, hashCode(), 返回当前对象的哈希值
System.out.println(coll.hashCode());
//8, 集合---转换为--->数组:toArray()
Object[] arr = coll.toArray();
for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
//拓展:数组---转换成--->集合;调用Arrays类的静态方法asList()
//Arrays.asList()方法接收一个数组或者是一个逗号分隔的元素列表(使用可变从参数),并将其转换为一个List对象
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(list);
List<Integer> arr2 = Arrays.asList(123, 456);
// List<Integer> arr2 = Arrays.asList(new Integer[]{123, 456});
System.out.println(arr2);//Integer类型的元素是两个
List<int[]> arr3 = Arrays.asList(new int[]{123, 456});
System.out.println(arr3);//将int[]当成一个元素
}
}
/**
-994820807
123
456
mofei
false
Person{name=mofei',age=22}
[AA, BB, CC]
[123, 456]
[[I@5b80350b]
*/
iterator() 返回Iterator接口的实例,用于遍历集合元素
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 集合元素的遍历操作,使用Iterator接口(迭代器接口)
*方法1,hasNext() 2,next() 3,remove()
* 注意:>Iterator可以删除集合的元素,但是是遍历过程中通过迭代器对象的remove()方法,不是集合对象的remove()方法
* >如果还未调用next()或在上一次调用next()方法之后已经调用了remove()方法,再调用remove()方法都会报IllegalStateException
* 原因:1,指针未动,没有元素 2,连续删除同一位置的元素,第一次已经删除掉了,第二次再删除
*
*/
public class IteratorTest {
@Test
public void test1() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
Iterator iterator = coll.iterator();
// System.out.println(iterator.next());
// for(int i = 0; i < coll.size(); i++)
// System.out.println(iterator.next());
//推荐使用组合,Iterator下的hasNext()方法和next()方法
//hasNext()方法返回值类型为boolean型,判断是否还有下一个元素
while(iterator.hasNext()) {
//next(); 行为:1,指针下移 2,将下移以后集合位置上的元素返回
System.out.println(iterator.next());
}
/**
* //错误方式:死循环
* //原因:集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
* while(coll.iterator().hasNext())
* System.out.println(iterator.next());
*/
}
/**
* 测试Iterator中的remove()方法
* 注意:>Iterator可以删除集合的元素,但是是遍历过程中通过迭代器对象的remove()方法,不是集合对象的remove()方法
* >如果还未调用next()或在上一次调用next()方法之后已经调用了remove()方法,再调用remove()方法都会报IllegalStateException
* 原因:1,指针未动,没有元素 2,连续删除同一位置的元素,第一次已经删除掉了
*/
@Test
public void test2() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("mofei"));
coll.add(false);
Iterator iterator = coll.iterator();
//删除集合中"mofei"这个数据
while(iterator.hasNext()){
Object obj = iterator.next();
if("mofei".equals(obj)) {
iterator.remove();
}
}
System.out.println(coll);
//遍历集合,上个遍历集合中指针为位置已经变动,所以要给集合coll建立一个新的Iterator对象
Iterator iterator1 = coll.iterator();
while(iterator1.hasNext()) {
System.out.println(iterator1.next());
}
}
}
********************** List接口 **********************