文章目录:
一、集合框架的概述
二、Java 集合可分为Collection和Map两种体系。
Collection 接口:单列数据,定义了存取一组对象方法的集合。
List :元素有序、可重复的集合。
set :元素无序、不可重复的集合。
Map 接口:双列数据、保存具有映射关系“Key – value” 对的集合。
三、Java 集合框架概述:Collection接口继承树
实线:继承关系 extends
虚线:实现关系 implement
四、Collection 常用方法的测试
1、add:向集合中添加元素
2、contains:判断当前集合中是否包含obj
3、remove:移除集合中的某个元素
4、romveAll:从当前集合中移除coll中所有得元素.
5、equals(Object obj) :要返回true,,需要當前集合和形參集合的元素相同.
6、equals 的使用(Obj obj) 比较内容值
7、hashcode() :返回當前對象的哈希值
8、集合---->數組:toArray()
9、数組---->集合:
package Com.Work_ZQ;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
- collection 接口中生明的方法測試
- 結論:
- 向collection 接口的實現類的對象中添加數據時,要obj所在類重寫equals().
*/
public class CollectionTest2 {
@Test
public void test() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(new String(“Tom”));
coll.add(false);
//3 contains(Object obj):判斷當前集合中是否包含obj
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String(“Tom”)));
System.out.println(coll.contains(new String(“Jreey”))); // 使用equals 比較的
}
@Test
public void Test2() {
// remove(Object obj) 移除集合中的某個元素
Collection coll = new ArrayList();
coll.add(123);
coll.add(new String("Tom"));
coll.add(new String("Jreey"));
coll.add(false);
coll.remove(123);
System.out.println(coll);
coll.remove(new String("Jreey"));
System.out.println(coll);
// 4. romveAll(collection coll1) 從當前集合中移除coll中所有得元素.
Collection coll1 = Arrays.asList(123, 456, 789);
coll.removeAll(coll1);
System.out.println(coll1);
//equals(Object obj) :要返回true,,需要當前集合和形參集合的元素相同.
}
@Test
public void Test3() {
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(new String("Tom"));
coll1.add(new String("Jreey"));
coll1.add(false);
Collection coll = Arrays.asList(123,456,789);
coll.removeAll(coll1);
System.out.println(coll.equals(coll1));
//6、equals 的使用(Obj obj)
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(new String("Jreey"));
coll.add(false);
System.out.println(coll1.equals(coll));
}
@Test
public void Test4() {
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(new String("Tom"));
coll1.add(new String("Jreey"));
coll1.add(false);
// 7、hashcode() :返回當前對象的哈希值
System.out.println(coll1.hashCode());
//8、集合--->數組:toArray()
Object[] arr = coll1.toArray();
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
// 數組-->集合
List<String> List = Arrays.asList("AA", "NN", "CC", "BB");
System.out.println(List);
List arr2 = Arrays.asList( new int[]{123,456});
System.out.println(arr2.size());
}
}
}
五、iterator 遍历常用方法:
while(iterator.hashNext) {
System.out.println(iterator.nxet());
}
package Com.Work_ZQ;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest3 {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(“Jreey”);
coll.add(false);
// 删除Jreey 操作
Iterator iterator =coll.iterator();
while (iterator.hasNext()){
Object obj = iterator.next();
if ("Jreey".equals(obj)){
iterator.remove();
}
}
// 遍历集合
iterator =coll.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
六、foreatch 循环: 用于 遍历 集合、数组
别名:增强for循环
二选一遍历:foreath 和 iterator 遍历
package Com.Work_ZQ;
import java.util.ArrayList;
import java.util.Collection;
JDk5.0 新增: for(集合元素的类型 局部变量:集合对象)
public class foreatchTest {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(“Jreey”);
coll.add(false);
/*
* for(集合元素的类型 局部变量:集合对象)
* 内部依然调用了迭代器。
*/
for (Object obj :coll){
System.out.println(obj);
}
}
}
如果我的博客对你有帮助、如果你喜欢我的博客内容,请“点赞” “评论”“收藏”
一键三连哦!听说点赞的人运气不会太差,每一天都会元气满满呦!如果实在要白嫖的话,那祝你开心每一天,欢迎常来我博客看看。