一、概述
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():truecollection.contains(2):truecollection.containsAll 1,2:truecollection.containsAll 1,20:falseiterator.next():1iterator.next():2iterator.next():3collection.size():3collection.remove(2):truecollection.remove(20):falsecollection:[1, 3]Integer integer :1Integer integer :3collection:[]
注意: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);}}
运行结果: