一、概述

Java单列集合中最顶层的是Collection接口,Collection下又分List和Set两大类:

学习Java集合的时候,建议自顶向下学:先学Collection,再学List和Set,最后学各种具体的集合。

二、Collection接口

1、Collection接口继承了Iterable接口,于是就有了Iterable的一些特性:

a、具有迭代器方法:

b、能够使用增强for循环

示例:

    public static void main(String[] args) {Collection<String> collection=new HashSet<>();collection.add("Tom");collection.add("Mary");Iterator<String> iterator = collection.iterator(); //使用迭代器while(iterator.hasNext()) System.out.println(iterator.next());for (String s : collection) System.out.println(s); //增强for循环}

2、Collection的常用方法

示例:

a、代码示例import java.util.*;public class Main{public static void main(String[] args) {Collection<Integer> collection=new HashSet<>(); //这里使用了多态,collection这个对象只能使用Collection的方法,不能使用HashSet的特有方法!System.out.println("collection.isEmpty():"+collection.isEmpty()); //集合是否为空collection.add(1); //使用add方法,一次只能添加一个元素collection.addAll(Arrays.asList(new Integer[]{2,3})); //addAll可一次性添加多个元素。Arrays.asList(new Integer[]{2,3})返回的是一个List!System.out.println("collection.contains(2):"+collection.contains(2)); //是否含有2System.out.println("collection.containsAll 1,2:"+collection.containsAll(Arrays.asList(new Integer[]{1,2})));System.out.println("collection.containsAll 1,20:"+collection.containsAll(Arrays.asList(new Integer[]{1,20})));Iterator<Integer> iterator = collection.iterator(); //使用迭代器while (iterator.hasNext()) System.out.println("iterator.next():"+iterator.next());System.out.println("collection.size():"+collection.size()); //返回集合中的元素个数System.out.println("collection.remove(2):"+collection.remove(2)); //移除成功,因为2存在System.out.println("collection.remove(20):"+collection.remove(20)); //移除失败,因为20不存在System.out.println("collection:"+collection); //无需遍历,直接输出:[1, 3]。这是因为toString方法被重写了Integer[] integers= collection.toArray(new Integer[]{});for (Integer integer : integers) System.out.println("Integer integer :"+integer);collection.clear();System.out.println("collection:"+collection);}
}b、输出
collection.isEmpty():true
collection.contains(2):true
collection.containsAll 1,2:true
collection.containsAll 1,20:false
iterator.next():1
iterator.next():2
iterator.next():3
collection.size():3
collection.remove(2):true
collection.remove(20):false
collection:[1, 3]
Integer integer :1
Integer integer :3
collection:[]

注意:Collection的子类继承了Collection的方法,即List和List的后代、Set和Set的后代,都可以使用上面这些方法!

三、List接口

List的特有方法:

1、

a、代码示例
import java.util.*;public class Main{public static void main(String[] args) {List<Integer> list=new ArrayList<>();list.addAll(Arrays.asList(new Integer[]{3,1,4,1,5}));list.sort(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1-o2; //o1-o2>0,即o1>o2,交换;否则不交换。所以是升序!}});System.out.println(list);}
}b、结果
[1, 1, 3, 4, 5]

2、

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{3,1,4,1,5}));
System.out.println(list.get(0));

结果:3

3、

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{3,1,4,1,5}));
System.out.println(list.set(0,666));
System.out.println(list.get(0));

结果:

3
666

4、

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{3,1,4,1,5}));
list.add(1,666); //原来1号及1号以后的所有元素后移一位!
System.out.println(list);

结果:[3, 666, 1, 4, 1, 5]

5、

结果:

5
[3, 1, 4, 1]
false
[3, 1, 4, 1]

6、7:

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{11,22,33,33,22,11}));
System.out.println(list.indexOf(11));
System.out.println(list.lastIndexOf(11));
System.out.println(list.indexOf(55));

结果:

0
5
-1

8、9:

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{0,1,2}));
ListIterator<Integer> iterator = list.listIterator(); //相当于list.listIterator(0)
while(iterator.hasNext()) System.out.println(iterator.next());

结果:

0
1
2

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{0,1,2}));
ListIterator<Integer> iterator = list.listIterator(1); //迭代index>=1的元素
while(iterator.hasNext()) System.out.println(iterator.next());

结果:

1
2

10、

List<Integer> list=new ArrayList<>();
list.addAll(Arrays.asList(new Integer[]{0,1,2,3,4}));
System.out.println(list.subList(1,3)); //index=1(含)、2、3(不含)

结果:[1, 2]

四、Set接口

Set中的方法如下:

从上图可知,Set中都是它老子Collection的方法!真是个啃老族啊!

由于篇幅太长,有关ArrayList、LinkedList、Vector、HashMap、LinkedHashMap、TreeSet的内容将在下一篇博客进行介绍!

(其实把父类的方法掌握了基本就够用了)


最后附上一个斗地主小游戏:

import java.util.ArrayList;
import java.util.Collections;public class PokerGameOfFightingTheLandlord {public static void main(String[] args) {//1、准备好牌String[] pokerNumbers={"2","1","K","Q","J","10","9","8","7","6","5","4","3"};String[] pokerSuits={"♥","♠","♦","♣"};ArrayList<String> pokers=new ArrayList<>();for(int pokerNumbersIndex=0;pokerNumbersIndex<pokerNumbers.length;pokerNumbersIndex++){for(int pokerSuitsIndex=0;pokerSuitsIndex<pokerSuits.length;pokerSuitsIndex++){String poker=pokerSuits[pokerSuitsIndex]+pokerNumbers[pokerNumbersIndex];pokers.add(poker);}}pokers.add("大王");pokers.add("小王");//2、洗牌Collections.shuffle(pokers);//3、把人凑齐ArrayList<String> player0_StephenChow=new ArrayList<>();ArrayList<String> player1_AndyLau=new ArrayList<>();ArrayList<String> player2_ChouYunfat=new ArrayList<>();ArrayList<String> threeCards=new ArrayList<>(); //底牌//4、发牌threeCards.add(pokers.get(0));threeCards.add(pokers.get(1));threeCards.add(pokers.get(2));for(int pokerIndex=3;pokerIndex<pokers.size();pokerIndex++){String poker=pokers.get(pokerIndex);switch (pokerIndex%3){case 0:player0_StephenChow.add(poker);break;case 1:player1_AndyLau.add(poker);break;case 2:player2_ChouYunfat.add(poker);}}//5、打牌(亮一下牌而已)System.out.println("player0_StephenChow:"+player0_StephenChow);System.out.println("player1_AndyLau:"+player1_AndyLau);System.out.println("player2_ChouYunfat:"+player2_ChouYunfat);System.out.println("threeCards:"+threeCards);}
}

运行结果:

#Java教程:集合 #Collection、List、Set #ArrayList、LinkedList、Vector、HashSet、TreeSet #一个斗地主小游戏@FDDLC相关推荐

  1. ArrayList, LinkedList, Vector - dudu:史上最详解

    ArrayList, LinkedList, Vector - dudu:史上最详解 我们来比较一下ArrayList, LinkedLIst和Vector它们之间的区别.BZ的JDK版本是1.7.0 ...

  2. ArrayList,LinkedList,Vector的异同点

    先总结下ArrayList和LinkedList的区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayLi ...

  3. ArrayList ,LinkedList,Vector,StringBuilder,StringBuffer ,String,HashMap,LinkedHashMap,TreeMap

    ArrayList ,LinkedList,Vector 区别和联系 从上面的类层次结构图中,我们可以发现他们都实现了List接口,它们使用起来非常相似.区别主要在于它们各自的实现,不同的实现导致了不 ...

  4. 斗地主小游戏(JAVA实现)

    hello,我是忘鱼.  目录 前言 一.案例所需要具备知识 二.代码 2.运行结果 总结 前言 斗地主小游戏,属于Collection体系综合案例,学习帮助我们加深理解. 一.案例所需要具备知识 常 ...

  5. JAVA猜数字 斗地主小游戏

    import Game; //Game为所创建的文件夹 import java.text.BreakIterator; import java.util.Random; import java.uti ...

  6. 用JAVA编程实现斗地主小游戏(建牌,发牌,选地主,排序)

    JAVA编程实现斗地主小游戏 主程序 排序算法 主程序 public static void main(String[] args) {//牌List<String> pokes = ne ...

  7. 【iVX 初级工程师培训教程 10篇文拿证】03 事件及猜数字小游戏

    目录 [iVX 初级工程师培训教程 10篇文拿证]01 了解 iVX 完成新年贺卡 [iVX 初级工程师培训教程 10篇文拿证]02 数值绑定及自适应网站制作 [iVX 初级工程师培训教程 10篇文拿 ...

  8. java人点灯问题_JAVA学习,写的一个点灯小游戏

    之前写的一个点灯小游戏,没什么技术含量. 截图: 代码: 类Start: import java.io.File; import java.io.IOException; import javax.i ...

  9. 基于java制作一个飞机小游戏(学习心得)

    emmm,第一篇博客 用了这么久的csdn,都是在观摩大佬们发帖,一只小萌新瑟瑟发抖,不敢说话,甚 至连代码也不能完全读懂.假期在b站自学(可以说是复习)了java课程,尚学堂的 2018版.链接如下 ...

最新文章

  1. Realm发布Realm .NET,扩展支持.NET技术栈
  2. ORA-27102: out of memory并伴随OSD-00031的处理
  3. tensorflow源码编译教程_源码编译安装tensorflow 1.8
  4. 玩转oracle 11g(26):误删表数据和误删表回滚
  5. python实现决策树算法sklearn_GitHub - cbyonder/lihang_algorithms: 用python和sklearn两种方法实现李航《统计学习方法》中的算法...
  6. linux未使用磁盘空间满,linux磁盘空间用满的处理方法
  7. 智能推荐系统之数据预处理
  8. 折扇的保养方法是什么?
  9. 关于计算机工作的诗歌,提高计算机工作及上网效率的方法
  10. Latex格式投稿需注意的问题_以BSPC和BMC BioInformatics为例
  11. 50句形容美人的绝佳诗句,只看一眼,惊艳千年
  12. VMware Workstation 10序列号
  13. tekla二次开发用C语言,Tekla二次开发怎么弄?一篇文章入门tekla二次开发!
  14. 怎么学计算机中级,计算机二级自学要多久 怎样复习
  15. 坑爹的No matching method
  16. 2017-2020(4周年)读书年度总结及书单
  17. 为什么被证明“拖后腿”的苹果Mini手机,仍然还会再次发布新款?
  18. 软件质量管理体系 type:pdf_昆明医疗器械软件注册流程,软件评估_上海峦灵
  19. 京东Java后台开发岗社招面试经验分享,4面的面经
  20. 《keep studying》————《持续学习》英译汉【istrangeboy精品英文励志短文系列】

热门文章

  1. OSChina 周一乱弹 —— 鱼生不值得
  2. 服务器端返回的状态码是什么意思
  3. Unity Camera Filed Of View
  4. GIT无法提交到码云。原因可能是所在提交位置不对
  5. btb和oto,btc模式是什么意思
  6. 270天不回家的“空中飞人们” 下一步要去哪里?
  7. H5满屏彩色泡泡小特效(适合表白哦~做完发给让你每天想念的人吧~)
  8. 金河电站被困216小时女工获救生还记
  9. Rework 读后感
  10. /usr/bin/ld cannot find -lGL