集合之Collection

本文详细介绍了Java集合框架中Collection接口的基本概念及其常用方法,包括add()、remove()、toArray()和contains()等方法的功能和使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      集合中有List、Set、Queue等等接口,而Collection是这些接口的父接口。而Collection接口是继承自Iterable接口的。也就是说实现Collection接口的集合元素是可迭代。Iterable接口只有一个iterator()方法,返回在这个集合上进行迭代的迭代器。

public interface Iterable<T> {

    /**
     * Returns an iterator over a set of elements of type T.
     * 返回一个在一组T类型元素集合上进行迭代的迭代器
     * @return an Iterator.
     */
    Iterator<T> iterator();
}

 Collection接口中常用的方法:

 

int size();
contains(Object o);
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);

 接下来简单的介绍下每个方法的用法:

add(E e)顾名思义是往集合中添加一个指定的元素,返回的是一个布尔型的变量,当此集合由于调用add()方法而发生改变的时候返回true,如果此集合不允许有重复的元素并且已包含有指定元素的时候就返回false,就是说当这个集合调用了add()方法而没有发生改变的就返回false。

public class CollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Collection<String> collection=new HashSet<String>();
		System.out.println("result1:"+collection.add("one"));
		System.out.println("result2:"+collection.add("two"));
		System.out.println("result3:"+collection.add("one"));
	}
}

 结果会是:

result1:true
result2:true
result3:false                                                                                 

可以看到前两此collection集合调用add()方法都改变了该集合的结构,所以返回的是true,而当第三此调用add()方法添加一个已经存在的元素时,这个元素并没有添加进去,集合的结构并没有发生改变,因此返回false。(当然这是Set集合的性质决定的)。                        

 

 

remove(Object o)是删除集合中指定的元素,返回的也是布尔型值。当删除元素改变了集合的结构时就返回true,如果删除没有改变集合的结构就返回false。

public class CollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Collection<String> collection=new ArrayList<String>();
		collection.add("one");
		collection.add("two");
		collection.add("three");
		System.out.println("result1:"+collection.remove("two"));
		System.out.println("result2:"+collection.remove("two"));

	}
}

 结果会是:

result1:true
result2:false
这是因为第一次删除时改变了集合的结构所以返回true,而第二次删除时并没有改变集合的结构因此返回false。

 

 

<T> T[] toArray(T[] a)返回包含此集合中所有元素的数组,此方法充当基于数组与基于集合之间的桥梁,对输出数组的运行时类型进行精确的控制。参数T[] a只是对转化的数组进行类型指定。

public class CollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Collection<String> collection=new ArrayList<String>();
		collection.add("one");
		collection.add("two");
		collection.add("three");
		collection.add("four");
		String str[]=null;
		String s1[]=new String[]{};
		str=collection.toArray(s1);
		System.out.println("result1:"+convertToStr(str));
		String s2[]=new String[0];
		str=collection.toArray(s2);
		System.out.println("result2:"+convertToStr(str));
		str=collection.toArray(new String[0]);
		System.out.println("result3:"+convertToStr(str));

	}
	
	public static String convertToStr(String str[]){
		StringBuffer strBuffer=new StringBuffer();
		for(int i=0;i<str.length;i++){
			strBuffer.append(str[i]);
			if(i!=str.length-1){
				strBuffer.append(",");
			}
		}
		return strBuffer.toString();
	}
}

 结果是:

result1:one,two,three,four
result2:one,two,three,four
result3:one,two,three,four

说明toArray()方法只用送一个类型参数就行了,以上程序中三种参数类型都行。

 

 

 contains(Object o)用于判断该集合中是否包含指定的元素,如果包含返回true,否则返回false。

public class CollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Collection<String> collection=new ArrayList<String>();
		collection.add("one");
		collection.add("two");
		collection.add("three");
		collection.add("four");
		System.out.println("result1:"+collection.contains("three"));
		System.out.println("result2:"+collection.contains("five"));
	}
}

 结果是:

 

 result1:true
result2:false

 

 在Collection接口中定义的最常用的方法大概就是这么几个。

 

 

 

 

 

 

 

 

 

 

 

 

 

内容概要:《中文大模型基准测评2025年上半年报告》由SuperCLUE团队发布,详细评估了2025年上半年中文大模型的发展状况。报告涵盖了大模型的关键进展、国内外大模型全景图及差距、专项测评基准介绍等。通过SuperCLUE基准,对45个国内外代表性大模型进行了六大任务(数学推理、科学推理、代码生成、智能体Agent、精确指令遵循、幻觉控制)的综合测评。结果显示,海外模型如o3、o4-mini(high)在推理任务上表现突出,而国内模型如Doubao-Seed-1.6-thinking-250715在智能体Agent和幻觉控制任务上表现出色。此外,报告还分析了模型性价比、效能区间分布,并对代表性模型如Doubao-Seed-1.6-thinking-250715、DeepSeek-R1-0528、GLM-4.5等进行了详细介绍。整体来看,国内大模型在特定任务上已接近国际顶尖水平,但在综合推理能力上仍有提升空间。 适用人群:对大模型技术感兴趣的科研人员、工程师、产品经理及投资者。 使用场景及目标:①了解2025年上半年中文大模型的发展现状与趋势;②评估国内外大模型在不同任务上的表现差异;③为技术选型和性能优化提供参考依据。 其他说明:报告提供了详细的测评方法、评分标准及结果分析,确保评估的科学性和公正性。此外,SuperCLUE团队还发布了多个专项测评基准,涵盖多模态、文本、推理等多个领域,为业界提供全面的测评服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值