| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> Java知识库 -> 集合相关操作 -> 正文阅读 |
|
|
[Java知识库]集合相关操作 |
package com.wanjin.jihe;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
?* Collection接口中声明的方法的测试
*
?* 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().
* @author shkstart
?* @create 2021.10.1
?* */
public class CollectionTest {
? ? @Test
? ? public void test1(){
? ? ? ? Collection coll=new ArrayList();
? ? ? ? coll.add(123);
? ? ? ? coll.add(456);
? ? ? ? coll.add(new String("Tom"));
? ? ? ? coll.add(false);
// ? ? ? ?Person p=new Person("Jerry",20);
// ? ? ? ?coll.contains(p);
? ? ? ? coll.add(new Person("Jerry",20));
? ? ? ? ?//Contains(Object obj):判断当前集合中是否包含obj
? ? ? ? //我们在判断时会调用obj对象所在类的equals()
? ? ? ? boolean contains = coll.contains(123);//true
? ? ? ? System.out.println(contains);//true
// ? ? ? ?System.out.println(coll.contains(p)); //true
? ? ? ? System.out.println(coll.contains(new String("Tom")));//true
? ? ? ? System.out.println(coll.contains(new Person("Jerry",20)));//false-->true
? ? ? ? System.out.println("===============================================");
? ? ? ? //在Person类中Alt+INS equals() and hashcode()
//2.containsAll(containsAll coll1):判断形参coll1中的所有元素是否都存在于当前集合中
? ? ?Collection coll1= Arrays.asList(123,456); ?// ?Arrays.asList方法是将数组转化成List集合的方法。
? ? ? ? System.out.println(coll.containsAll(coll1));
? ? }
? ? @Test
? ? public void test2(){
? ? ? ? //3.remove(Object obj):从当前集合中移除obj元素。
? ? ? ? Collection coll=new ArrayList();
? ? ? ? coll.add(123);
? ? ? ? coll.add(456);
? ? ? ? coll.add(new String("Tom"));
? ? ? ? coll.add(false);
? ? ? ? coll.add(new Person("Jerry",20));
? ? ? ? coll.remove(123); //删除掉123
? ? ? ? System.out.println(coll);
? ? ? ? coll.remove(new Person("Jerry",20));
? ? ? ? System.out.println(coll);
? ? ? ? //4.removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素
? ? ? ? Collection coll1=Arrays.asList(123,456);//删除掉123,456
? ? ? ? coll.removeAll(coll1);
? ? ? ? System.out.println(coll);
? ? }
? ? @Test
? ? public void test3() {
? ? ? ? Collection coll = new ArrayList();
? ? ? ? coll.add(123);
? ? ? ? coll.add(456);
? ? ? ? coll.add(new String("Tom"));
? ? ? ? coll.add(false);
? ? ? ? coll.add(new Person("Jerry", 20));
? ? ? ? //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合(保留一样的,删掉不一样的)
// ? ? ? ?Collection coll1=Arrays.asList(123,456,789);
// ? ? ? ?coll.retainAll(coll1);
// ? ? ? ?System.out.println(coll);
//
? ? ? ? //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同,若是ArrayList,则顺序也要相同
? ? ? ? Collection coll1=new ArrayList();
? ? ? ? coll1.add(123);
? ? ? ? coll1.add(456);
? ? ? ? coll1.add(new String("Tom"));
? ? ? ? coll1.add(false);
? ? ? ? coll1.add(new Person("Jerry",20));
? ? ? ? System.out.println(coll.equals(coll1));
? ? }
? ? @Test
? ? public void test4() {
? ? ? ? Collection coll = new ArrayList();
? ? ? ? coll.add(123);
? ? ? ? coll.add(456);
? ? ? ? coll.add(new String("Tom"));
? ? ? ? coll.add(false);
? ? ? ? coll.add(new Person("Jerry", 20));
? ? ? ? //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()
? ? ? ? List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
? ? ? ? System.out.println(list);
? ? ? ?// List<int[]> arr1 = Arrays.asList(new int[]{123, 456});//这样写的话new int[]{123, 456}会被当成是一个元素
? ? ? ? List arr1 = Arrays.asList(123, 456);
? ? ? ? System.out.println(arr1);
? ? ? ? List arr2 = Arrays.asList(new Integer[]{123,456});//可以写成这样
? ? ? ? System.out.println(arr2);
? ? ? ? //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中
? ? }
}
? ? package com.wanjin.jihe; import org.junit.Test; import java.util.ArrayList;
? ? ? ? java.util.Iterator iterator = coll.iterator(); ? ? ? ? while (iterator.hasNext()){ ? ? ? ? ? ? System.out.println(iterator.next()); //迭代器,输出coll集合中所有元素 ? ? ? ? } ? ? @Test ? ? ? ? //错误方式:集合对象每次调用iterator()方法都会得到一个全新的迭代器对象,默认游标都在集合的第一个,所以此方法会陷入一直输出123的死循环 ? ? ? ? //删除集合中"Tom"
package com.wanjin.jihe; import java.util.Objects; /** ? ? public Person() { ? ? public Person(String name, int age) { ? ? public String getName() { ? ? public void setName(String name) { ? ? public int getAge() { ? ? public void setAge(int age) { ? ? @Override ? ? @Override // ? ?@Override ? |
|
|
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年12日历 | -2025/12/10 1:20:38- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |