Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class

转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Collections.html

Java offers two types of constructs where you can store multiple values or objects of the same type: arrays and collections (for System Dynamics models AnyLogic also offers HyperArray, also known as "subscripts", – a special type of collection for dynamic variables).

Array or collection? Arrays are simple constructs with linear storage of fixed size and therefore they can only store a given number of elements. Arrays are built into the core of Java language and the array-related Java syntax is very easy and straightforward, for example the nth element of the array can be obtained as array[n]. Collections are more sophisticated and flexible. First of all, they are resizable: you can add any number of elements to a collection. A collection will automatically handle deletion of an element from any position. There are several types of collections with different internal storage structure (linear, list, hash set, tree, etc.) and you can choose a collection type best matching your problem so that your most frequent operations will be convenient and efficient. Collections are Java classes and syntax for obtaining, e.g., the nth element of acollection of type ArrayList is collection.get(n).

From a capability perspective, while both can store references to objects:

  • Arrays can store primitives
  • Collections can not store primitives (although they can store the primitive wrapper classes, such as Integer etc)

One important difference, commonly not understood by programmers new to java, is one of usability and convenience, especially given that Collections automatically expand in size when needed.

Collection, as its javadoc says is "The root interface in the collection hierarchy." This means that every single class implementing Collection in any form is part of the Java Collections Framework.

The Collections Framework is Java‘s native implementation of data structure classes (with implementation specific properties) which represent a group of objects which are somehow related to each other and thus can be called a collection.

Collections is merely an utility method class for doing certain operations, for example adding thread safety to your ArrayList instance by doing this:

List<MyObj> list = Collections.synchronizedList(new Arraylist<MyObj>());

The main difference in my opinion is that Collection is base interface which you may use in your code as a type for object (although I wouldn‘t directly recommend that) while Collections just provides useful operations for handling the collections.

时间: 2024-10-24 13:03:26

Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class的相关文章

Split / Partition a collection into smaller collections - Java - Tutorial

Partition a collection into smaller collections This article describes how to partition a collections into a given number of smaller collections. Table of Contents 1. Partition a collection 2. Partition collection in Java 2.1. Implementation 2.2. Tes

I学霸官方免费教程三十五:Java集合框架之Collection接口和Collections类

Collection接口 Collection接口是List和Set接口的父接口,其中主要定义了一些集合基本操作的方法,包括与Iterator之间的关系List  extends  CollectionArrayList  implements  ListLinkedList  implements  ListVector  implements  ListSet  extends  CollectionHashSet  implements  SetSortedSet  extends  Se

Collection接口和Collections类的简单区别和讲解

这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度. 1.Collection: 是集合类的上层接口.本身是一个Interface,里面包含了一些集合的基本操作. Collection接口时Set接口和List接口的父接口 里面的常用操作有如下内容: 2.Collections Collections是一个集合框架的帮助类,里面包含一些对集合的排序,搜索以及序列化的操作. 最根本的是Collections是一个类哦. 下面是Collections类中的常用操作: 好吧,就先到这里

Comparable和Comparator比较实现排序 场景分析

源码分析 - Collections.sort() 一.Collection.sort使用 Collections.sort():是对一个集合进行正向排序的方法首先,传入Collections.sort()的集合的元素类型要继承Comparator<T>,这样才能保证可以比较并排序. 根据源码分析,实现方式主要有两种: (1)对普通类型和String等类型排序 (2)对JavaBean某个属性(比如时间,价格,年龄排序)进行排序 使用案例: List<String> listOne

【原】Java学习笔记029 - 映射

1 package cn.temptation; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class Sample01 { 7 public static void main(String[] args) { 8 /* 9 * Collection接口及其子接口.实现类 等 称为 单列集合 10 * Map接口及其子接口.实现类等 称为 双列集合 11 * 12 * 接口 Map<K,V>:映射,将键映射

NET设计规范(二) 命名规范

http://blog.csdn.net/richnaly/article/details/6280294 第2章       命名规范 2.1.   大小写约定 2.1.1.    标识符的大小写规则     ü 用PascalCasing命名多个单词构成的命名空间.类型以及成员的名字. 例如:使用TextColor而不使用Textcolor,单个单词(Button)的首字母大写,当一个单词为复合词(endpoint)作为一个单词,只有第一字母大写. ü 用camelCasing命名参数的名字

Java集合(三)--Collection、Collections和Arrays

Collection: Collection是集合类的顶级接口,提供了对集合对象进行基本操作的通用接口方法.Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式,其直接继承接口有List与Set. public interface Collection<E> extends Iterable<E> { int size(); //集合的容量 boolean isEmpty(); //是否为空 boolean contains(Object var1); //是

Collection与Arrays

1. Collections与Arrays 集合框架中的工具类:特点:该工具类中的方法都是静态的. Collections:常见方法: 1, 对list进行二分查找: 前提该集合一定要有序. int binarySearch(list,key); //必须根据元素自然顺序对列表进行升级排序 //要求list 集合中的元素都是Comparable 的子类. int binarySearch(list,key,Comparator); 2,对list集合进行排序. sort(list); //对li

java===枚举,Arrays,Collections

枚举 package 枚举; /** * 枚举与Iterator用法相同,但是Iterator简化书写*/ import java.util.Enumeration; import java.util.Vector; public class Enum { public static void main(String[] args) { Vector<String> v1 = new Vector<String>(); v1.add("a"); v1.add(&