1. list的遍历
1.普通for循环
for(int i = 0 ;i<list.size();i++) {
int j= (Integer) list.get(i);
System.out.println(j);
}
2.增强for循环
for (Object object : list) {
System.out.println(object);
}
3.迭代器iterator
Iterator iterator = list.iterator();
while(iterator.hasNext()){
int i = (Integer) iterator.next();
System.out.println(i);
}
// 或者
for(Iterator iterator = list.iterator();iterator.hasNext();){
int i = (Integer) iterator.next();
System.out.println(i);
}
4.list.forEach()方法
list.forEach(v-> System.out.println(v));
各遍历方式的适用于什么场合?
1、传统的for循环遍历,基于计数器的:
顺序存储:读取性能比较高。适用于遍历顺序存储集合。
链式存储:时间复杂度太大,不适用于遍历链式存储的集合。
2、迭代器遍历,Iterator:
顺序存储:如果不是太在意时间,推荐选择此方式,毕竟代码更加简洁,也防止了Off-By-One的问题。
链式存储:意义就重大了,平均时间复杂度降为O(n),还是挺诱人的,所以推荐此种遍历方式。
3、foreach循环遍历:
foreach只是让代码更加简洁了,但是他有一些缺点,就是遍历过程中不能操作数据集合(删除等),所以有些场合不使用。而且它本身就是基于Iterator实现的,但是由于类型转换的问题,所以会比直接使用Iterator慢一点,但是还好,时间复杂度都是一样的。所以怎么选择,参考上面两种方式,做一个折中的选择。
2.set的遍历
1.迭代器遍历:
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
2.增强for循环遍历:
for (String str : set) {
System.out.println(str);
}
优点还体现在泛型 假如 set中存放的是Object
Set<Object> set = new HashSet<Object>();
for循环遍历:
for (Object obj: set) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
........
}
3. map的遍历:
1、通过获取所有的key按照key来遍历
//Set<Integer> set = map.keySet(); //得到所有key的集合
for (Integer in : map.keySet()) {
String str = map.get(in);//得到每个key多对用value的值
}
2、通过Map.entrySet使用iterator遍历key和value
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
3、通过Map.entrySet遍历key和value,推荐,尤其是容量大时
for (Map.Entry<Integer, String> entry : map.entrySet()) {
//Map.entry<Integer,String> 映射项(键-值对) 有几个方法:用上面的名字entry
//entry.getKey() ;entry.getValue(); entry.setValue();
//map.entrySet() 返回此映射中包含的映射关系的 Set视图。
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
4、通过Map.values()遍历所有的value,但不能遍历key
for (String v : map.values()) {
System.out.println("value= " + v);
}
4. 集合嵌套综合:
ackage org.westos.Demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
// 基础班
// 张三 20
// 李四 22
// 就业班
// 王五 21
// 赵六 23
// HashMap嵌套HashMap
// HashMap嵌套ArrayList
public class ZuoYe4 {
public static void main(String[] args) {
HashMap<String, Integer> hm = new HashMap<>();
hm.put("张三", 20);
hm.put("李四", 22);
HashMap<String, Integer> hm1 = new HashMap<>();
hm1.put("王五", 21);
hm1.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> HM = new HashMap<>();
HM.put("基础班", hm);
HM.put("就业办", hm1);
//第一种遍历方式
Set<String> strings = HM.keySet();
for (String string : strings) {
System.out.println(string);
HashMap<String, Integer> stringIntegerHashMap = HM.get(string);
Set<String> strings1 = stringIntegerHashMap.keySet();
for (String st : strings1) {
Integer integer = stringIntegerHashMap.get(st);
System.out.println("\t" + st + "\t\t" + integer);
}
}
System.out.println("--------------------------------------------");
//第二种遍历方式
Set<Map.Entry<String, HashMap<String, Integer>>> entries = HM.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
HashMap<String, Integer> value = entry.getValue();
Set<Map.Entry<String, Integer>> entries1 = value.entrySet();
for (Map.Entry<String, Integer> entry1 : entries1) {
String key1 = entry1.getKey();
Integer value1 = entry1.getValue();
System.out.println("\t" + key1 + "\t\t" + value1);
}
}
}
4.使用集合完成扑克牌游戏:
- 无序版:
package org.westos.collection.poker;
import java.util.ArrayList;
import java.util.Collections;
public class Poker {
// 3个玩家 和 底牌
public static ArrayList<String> Jack = new ArrayList<>();
public static ArrayList<String> Mary = new ArrayList<>();
public static ArrayList<String> Blank = new ArrayList<>();
public static ArrayList<String> Bottom = new ArrayList<>();
/**
* 生成一副牌并洗牌,ArrayList实现
*/
public static ArrayList<String> createPoker(){
ArrayList<String> pokerList = new ArrayList<>();
String[] colors = {"♥", "♠", "♣", "♦"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String num : nums) {
pokerList.add(color.concat(num));
}
}
//手动添加大小王
pokerList.add("☀");
pokerList.add("��");
//洗牌
Collections.shuffle(pokerList);
Collections.shuffle(pokerList);
Collections.shuffle(pokerList);
return pokerList;
}
/**
* 三人斗地主,发牌开始,留3张底牌
*/
public void display(ArrayList<String> poker){
for (int i = 0; i < poker.size(); i++) {
if(i >= poker.size() - 3){
Bottom.add(poker.get(i));
}else if(i % 3 == 0){
Jack.add(poker.get(i));
}else if(i % 3 == 1){
Mary.add(poker.get(i));
}else{
Blank.add(poker.get(i));
}
}
}
public void peekPoker(String name, ArrayList<String> poker){
System.out.println(name + " :");
for (String s : poker) {
System.out.print(s + " ");
}
System.out.println();
}
}
2.有序版:
package org.westos.working.collection.poker;
import java.util.*;
/**
* //排序版
* //1.思路:你要给每张牌编张索引 要用双列集合 键是Integer 值是String
* // 2.再创建一个索引集合,来装索引
* //3.洗牌 其实洗索引集合
* //4.发牌 发索引 每个人用TreeSet集合 能够对索引进行排序
* //5.看牌 键找值
*/
public class PokerImprove {
/**
* 3 个玩家 和 底牌
*/
public static TreeSet<Integer> Jack = new TreeSet<>();
public static TreeSet<Integer> Mary = new TreeSet<>();
public static TreeSet<Integer> Blank = new TreeSet<>();
public static TreeSet<Integer> Bottom = new TreeSet<>();
/**
* 扑克牌
*/
public static Map<Integer, String> map = new HashMap<>();
/**
* 创建一副扑克牌,并完成洗牌
* @return
*/
public static ArrayList<Integer> createPoker() {
ArrayList<Integer> list = new ArrayList<>();
String[] colors = {"♥", "♠", "♣", "♦"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
//定义一个索引
int index = 0;
for (String num : nums) {
for (String color : colors) {
//把生成的牌存进集合中
map.put(index, num.concat(color));
//顺便给索引集合里面存一份索引
list.add(index);
index++;
}
}
//手动添加大小王
map.put(index, "☀");
list.add(index);
index++;
map.put(index, "��");
list.add(index);
//洗牌
Collections.shuffle(list);
Collections.shuffle(list);
Collections.shuffle(list);
return list;
}
/**
* 三人斗地主,发牌开始,留3张底牌
*/
public void display(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
if (i >= list.size() - 3) {
Bottom.add(list.get(i));
} else if (i % 3 == 0) {
Jack.add(list.get(i));
} else if (i % 3 == 1) {
Mary.add(list.get(i));
} else if (i % 3 == 2) {
Blank.add(list.get(i));
}
}
}
/**
* 看玩家的牌 和 底牌
*
* @param name
* @param treeSet
*/
public void peekPoker(String name, TreeSet<Integer> treeSet) {
System.out.println(name + " :");
for (Integer i : treeSet) {
System.out.print(map.get(i) + " ");
}
System.out.println();
}
}
测试:
package org.westos.working.collection.poker;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
public class PokerTest {
@Test
public void testPoker() {
//实现斗地主的发牌,洗牌,看牌,
//思路:
// 1.生成一副牌,放在集合(牌盒)
//2.洗牌
//3.三个人斗地主,留三张底牌 发牌
//4.看牌 ,看谁拿了一副好牌
Poker poker = new Poker();
ArrayList<String> pokerBox = Poker.createPoker();
poker.display(pokerBox);
poker.peekPoker("Blank", Poker.Blank);
poker.peekPoker("Mary", Poker.Mary);
poker.peekPoker("Jack", Poker.Jack);
poker.peekPoker("Bottom", Poker.Bottom);
}
@Test
public void testImprovePoker(){
PokerImprove poker = new PokerImprove();
ArrayList<Integer> list = PokerImprove.createPoker();
// 发牌
poker.display(list);
// 看牌
poker.peekPoker("Blank", PokerImprove.Blank);
poker.peekPoker("Mary", PokerImprove.Mary);
poker.peekPoker("Jack", PokerImprove.Jack);
poker.peekPoker("Bottom", PokerImprove.Bottom);
}
}
1077

被折叠的 条评论
为什么被折叠?



