一、集合框架(Collection和Collections的区别)
一、Collection和Map
是一个接口
Collection是Set,List,Queue,Deque的接口
Set:无序集合,List:链表,Queue:先进先出队列,Deque:双向链表
Collection和Map之间没有关系,Collection里放一个一个对象的,Map是放键值对key-value。

二、Collections
Collections是一个类,也就是容器的工具类,如同Arrays就是数组的工具类
里面有很多方法:
常用的:reverse():反转,逆序
shuffle():混淆,就跟洗牌一样,随机打乱顺序
sort():排序从小到大的顺序
swap():交换
rotate():向右滚动
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class TestCollection{
public static void main(String[] args){
//初始化集合
List<Integer> num=new ArrayList<Integer>();
for(int i=0;i<10;i++){
num.add(i);
}
System.out.println("集合中的数据:");
System.out.println(num);//0 1 2 3 4 5 6 7 8 9
Collections.reverse(num);
System.out.println("翻转后集合中的数据:");
System.out.println(num);//9 8 7 6 5 4 3 2 1 0
Collections.shuffle(num);
System.out.println("混淆后集合中的数据:");
System.out.println(num);//7 5 4 1 2 6 9 3 0 8
Collections.sort(num);
System.out.println("排序后集合中的数据:");
System.out.println(num);//0 1 2 3 4 5 6 7 8 9
Collections.swap(num,0,6);
System.out.println("交换0和6位置的数据后,集合的数据为:");
System.out.println(num);//6 1 2 3 4 5 0 7 8 9
Collections.rotate(num,2);//向右滚动
System.out.println("把集合向右滚动2个单位,集合中的数据为");//也就是把集合中最后面的两个数,放到最前面来。其余不变。
System.out.println(num);//8 9 6 1 2 3 4 5 0
}
}
synchronizedList():线程安全化
也就是把不安全线程转化为安全线程,比如ArrayList是不安全线程,在多线程中不能用,而Vector是多线程的安全。
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class TestCollection{
public static void main(String[] args){
List<Integer> num=new ArrayList<Integer>();
System.out.println("把非安全线程的List转换为线程安全的List");
Collections.synchronizedList(num);
}
}
一、集合框架(Collection和Collections的区别)的更多相关文章
- 介绍Collection框架的结构;Collection 和 Collections的区别
介绍Collection框架的结构:Collection 和 Collections的区别 集合框架: Collection:List列表,Set集 Map:Hashtable,HashMap,Tre ...
- Java学习笔记--Collection和Collections的区别
转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别. 1.java.util.Collection 是一个集合 ...
- Java集合框架Collection
转自:http://www.cdtarena.com/javapx/201306/8891.html [plain] view plaincopyprint?01.在 Java2中,有一套设计优良的接 ...
- 浅谈集合框架五——集合框架扩展:Collections工具类的使用,自定义比较器
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- Collection 和 Collections的区别。
Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...
- Collection 和 Collections的区别。(转)
Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...
- HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别
面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...
- Java中的集合框架-Collection(二)
上一篇<Java中的集合框架-Collection(一)>把Java集合框架中的Collection与List及其常用实现类的功能大致记录了一下,本篇接着记录Collection的另一个子 ...
- Collection 和 Collections的区别?
Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...
- 理解java集合——集合框架 Collection、Map
1.概述: @white Java集合就像一种容器,可以把多个对象(实际上是对象的引用,但习惯上都称对象)"丢进"该容器中. 2.Java集合大致可以分4类: @white Set ...
随机推荐
- Leetcode167-Two Sum II Input array is sorted-Easy
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- function CONVERSION_EXIT ****INPUT/OUTPUT说明
CONVERSION_EXIT_ALPHA_INPUT和CONVERSION_EXIT_ALPHA_OUTPUT 函数说明 CONVERSION_EXIT_MATN1_INPUT 物料号码转换函数 C ...
- 【Java】【集合】
[1. ]HashMap,LinkedHashMap,TreeMap对比 共同点: HashMap,LinkedHashMap,TreeMap都属于Map:Map 主要用于存储键(key)值(valu ...
- spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the ...
- win10,配置python3.6,虚拟环境
1.安装python3.6(官网下载) 2.pip install virtualenv(安装虚拟环境) 3.virtualenv TestEnv(创建名为TestEnv的虚拟环境) 4.进入Test ...
- python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息
背景介绍: 发送搜索请求时,需要用到登录接口返回值中的token值 代码实现: 登录代码: 搜索接口:
- Rest数据服务查询类-根据id查询
Rest数据服务查询类 需要iserver data服务支持,但请求的时候,不依赖SuperMap js lib包. 构造函数:QueryById=function(p_params): p_para ...
- cocos2dx 如何获得节点的类型
1. 需求:在所有子节点中得到是ui::Text类型的节点,并对其进行操作. 2. 解决方案:在根节点Node中有一个如下的函数: /** * Gets the description string. ...
- ionic 搜索双向数据绑定失效
1.用data对象存储变化的数据 js: $scope.data={}; $scope.data.keywords = ""; $scope.search = function() ...
- MySQL utf8 和 utf8mb4 的区别
utf-8 时变化长度的编码,储存一个code point 需要1~4个字节. 然而,mysql的utf8只存储最多3个字节per code point. 所以,utf8字符集不能存储所有的unico ...