遍历ArrayList、Set和Map的方法

1.四种遍历ArrayList的方法

package cn.itcast.chapter0607.foreach;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
	//1)利用普通for循环
		Date begin = new Date();
		for(int i=0;i<list.size();i++){
			String temp = (String) list.get(i);
			System.out.println(temp);
		}
	//2)增强for
		for(String temp:list){
			System.out.println(temp);
		}
	//3)迭代器1
		for(Iterator<String> it=list.iterator();it.hasNext();){
			String temp = it.next();
			System.out.println(temp);
		}
	//4)迭代器2  --遍历的时候要删除元素时,使用这种方法
		Iterator it = list.iterator();
		while(it.hasNext()){
			Object obj = it.next();
			it.remove();
			System.out.println(obj);
		}

	}

}

2.遍历Set的两种方法

import java.util.HashSet;
import java.util.Iterator;

/*
 * HashSet:可存放重复元素
 * 	
 */
class Student{
	String name;
	String id;
	public Student(String id, String name){
		this.id = id;
		this.name = name;
	}
	public String toString(){
		return id+":"+name;
	}
}

public class Example {
		public static void main(String[] args) {
			HashSet hs = new HashSet();
		    Student stu1=new Student ("1","DaBao");
		    Student stu2 = new Student("3","XiaoBao");
		    Student stu3 = new Student ("3","XiaoBao");
		    hs.add(stu1);
		    hs.add(stu2);
		    hs.add(stu3);
		//1)增强for循环    
		  for(Object obj:hs){
			  System.out.println(obj);
		  }
		 //2)迭代器
		  for(Iterator<Object> it =hs.iterator();it.hasNext();){
			  Object obj = it.next();
			  System.out.println(obj);
		  }
		}
}

3.遍历Map

1.根据key获得value

package cn.itcast.chapter0610.HashMap01;

import java.util.HashMap;
import java.util.Map;

/*
 * HashMap的用法
 * 
 */
public class Example9 {
		public static void main(String[] args) {
			//创建HashMap
			Map map = new HashMap();
			//存储
			map.put(1, "abc");
			map.put("2", "Tom");
			map.put("3", "java");
			map.put("3", "asd");
			//根据key获得value		
			System.out.println("1:"+map.get(1));
			System.out.println("2:"+map.get("2"));
			System.out.println("3:"+map.get("3"));
		}
}

2.通过键查找值方式遍历

package cn.itcast.chapter0610.HashMap02;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * HashMap
 * 遍历集合中元素 通过键查找值方式遍历
 * 使用迭代器
 */
public class Example {
		public static void main(String[] args) {
			Map map = new HashMap();
			
			map.put("1", "asd");
			map.put("2","Rose");
			map.put("3", "Lucy");
			//1.获取集合中所有的键key
			Set keySet = map.keySet();
			//2.使用迭代器进行key的集合遍历 得到key
			 Iterator  it = keySet.iterator();
			 while(it.hasNext()){
				 Object key = it.next();
			 
			//3.通过Map中get()方法,得到键对
			Object value = map.get(key);
			System.out.println(key+":"+value);
		}
		}
}

3.通过entrySet方法

public class Example12 {
		public static void main(String[] args) {
			Map map = new HashMap();
			//存储键和值
			map.put("1", "Jack");
			map.put("2", "tom");
			map.put("3", "janf");
			
			
			//1.获取所有的键值对元素
			Set entrySet  = map.entrySet();
			//2.通过迭代器,获取每一个对应的键值对元素
			Iterator it = entrySet.iterator();
			while(it.hasNext()){
				Map.Entry entry = (Map.Entry)it.next();
				//3.通过键值对元素,获取里面的键和值
				
				//Object key = entry.getKey();
				//Object value = entry.getValue();
		
				System.out.println("key:"+entry.getKey());
				System.out.println("value:"+entry.getValue());
			}
			
		}
}

4.获得Map集合中所有值的value

public class Example13 {
		public static void main(String[] args) {
			Map map  = new HashMap();
			map.put("1", "Jack");
			map.put("2", "tom");
			map.put("3", "janf");
			
			//获取Map集合中所有值的value
			Collection values = map.values(); 
			Iterator it = values.iterator();
			while(it.hasNext()){
				Object value = it.next();
				System.out.println(value);
				
			}
			System.out.println("value:"+map.values());
		}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值