一、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. 每日质量NPM包模态框_react-modal

    一.react-modal 官方定义: Accessible modal dialog component for React.JS 理解: 一个容易使用的React模态框组件 二.用法 有时候我们不 ...

  2. JSONObject的parseArray方法作用。

    该方法将字符串数据转换成集合对象. String dep_tree = JedisUtils.getInstance().get(CacheConstant.DEP_TREE, user.getId( ...

  3. 【一】php 基础知识

    1 php标记,<?php php代码 ?> 2 注释:代码的解释和说明 多行注释 /**/ 单行注释 //.# 3“.”连接符 echo "hello".date(& ...

  4. vs 2015 编译cocos2d-x-3.9

    下载地址:链接: https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ 提取码: p1pb 或者去官网下载. 解压后,进入build文件夹,点击如下工程链接 ...

  5. idea使用教程(1)

    引言:本教程主要讲解一下常用的配置安装方法,不包含软件安装,按照以下教程配置后,可以直接用于生产环境. 参考网址:参考了尚硅谷关于idea的使用教学视屏 idea注册码地址:http://idea.l ...

  6. 小程序模板template

    WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 定义模板 使用 name 属性,作为模板的名字.然后在<template/>内定义代码片段,如: & ...

  7. Algorithm 算法基础知识(未完成

    基础概念不讲,记录课上关键部分 时间复杂度(Time Complexity) 算法所需要花的时间 比较时间复杂度(主要看问题的规模) 时间频度(算法执行次数)T(n)和T(n1),如果两个时间频度为等 ...

  8. Qt5_当前exe所在路径

    可以通过以下方式来获取: 1. #include <QDir>#include <QDebug> QDir dir; qDebug() << "curre ...

  9. Codeforces 920G - List Of Integers

    920G - List Of Integers 思路:容斥+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  10. 虚拟机vmnet0、vmnet1和vmnet8的区别

    vmnet0,实际上就是一个虚拟的网桥 vmnet0,实际上就是一个虚拟的网桥,这个网桥有很若干个端口,一个端口用于连接你的Host,一个端口用于连接你的虚拟机,他们的位置是对等的,谁也不是谁的网关. ...