一、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的区别)的更多相关文章

  1. 介绍Collection框架的结构;Collection 和 Collections的区别

    介绍Collection框架的结构:Collection 和 Collections的区别 集合框架: Collection:List列表,Set集 Map:Hashtable,HashMap,Tre ...

  2. Java学习笔记--Collection和Collections的区别

    转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别.   1.java.util.Collection 是一个集合 ...

  3. Java集合框架Collection

    转自:http://www.cdtarena.com/javapx/201306/8891.html [plain] view plaincopyprint?01.在 Java2中,有一套设计优良的接 ...

  4. 浅谈集合框架五——集合框架扩展:Collections工具类的使用,自定义比较器

    最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...

  5. Collection 和 Collections的区别。

    Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...

  6. Collection 和 Collections的区别。(转)

    Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...

  7. HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别

    面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...

  8. Java中的集合框架-Collection(二)

    上一篇<Java中的集合框架-Collection(一)>把Java集合框架中的Collection与List及其常用实现类的功能大致记录了一下,本篇接着记录Collection的另一个子 ...

  9. Collection 和 Collections的区别?

    Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...

  10. 理解java集合——集合框架 Collection、Map

    1.概述: @white Java集合就像一种容器,可以把多个对象(实际上是对象的引用,但习惯上都称对象)"丢进"该容器中. 2.Java集合大致可以分4类: @white Set ...

随机推荐

  1. 【NOI 2009】诗人小G

    Problem Description 小 \(G\) 是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用 ...

  2. 【译】第21节---Fluent API

    原文:http://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx 在前面的学习中.我们已经看到不同的 ...

  3. _itemmod_unbind

    该表中的物品可以用一定代价进行解绑,解绑后可以移动,但下线将会导致物品重新绑定 `entry`物品entry `reqId` 解绑消耗模板Id `备注` 备注

  4. hive表的存储路径查找以及表的大小

    1.在hive中知道一个表的存储路径可以通过hive命令   desc formatted table_name 显示表的详细信息; 2.然后找到该表的存储路径 "Location:    ...

  5. Transactional cannot be resolved to a type

    SpringBoot整合Mybatis时遇到“ Transactional cannot be resolved to a type ” ,以为是没有导入相应的包 “ import org.sprin ...

  6. LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (key) 存 ...

  7. 01-python基础知识

    1.这两个参数是什么意思:*args,**kwargs?我们为什么要使用它们? 答案 如果我们不确定要往函数中传入多少个参数,或者我们想往函数中以列表和元组的形式传参数时,那就使要用*args:如果我 ...

  8. SQL service 中的 ”输入SQL命令窗口“ 打开了 “属性界面” 回到 ”输入SQL命令窗口“

    输入SQL命令窗口点击上面的菜单栏中的 “窗口”

  9. Python Selenium 文件上传之SendKeys

    昨天写了Web 文件下载的ui自动化,下载之后,今天就要写web 文件上传的功能了. 当然从折腾了俩小时才上传成功.下面写一下自己操作的步骤 首先网上说的有很多方法 如 input 标签的最好做了,直 ...

  10. 手机计算器1+1=2---Appium自动化

    要想计算1+1=2,首先要定位到按钮1,定位方式和selenium类似