Java-Iterator遍历集合

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TestIterator {
	public static void main(String[] args) {
		//testIteratorList();
		//testIteratorSet();
		testIteratorMap();
	}

	public static void testIteratorList() {
		List<String> list = new ArrayList<>();
		list.add("aa");
		list.add("bb");
		list.add("cc");

		for (Iterator<String> item = list.iterator(); item.hasNext();) {
			String temp = item.next();
			System.out.println(temp);
		}
	}
	
	public static void testIteratorSet(){
		Set<String> set=new HashSet<>();
		set.add("11");
		set.add("22");
		set.add("33");
		
		for(Iterator<String> iter=set.iterator();iter.hasNext();){
			String temp=iter.next();
			System.out.println(temp);
		}
	}
	
	//第一种
	public static void testIteratorMap(){
		Map<Integer,String> map=new HashMap<>();
		map.put(100, "aa");
		map.put(400, "bb");
		map.put(500, "cc");
		
	   Set<Entry<Integer,String>> ss=map.entrySet();
	   for(Iterator<Entry<Integer,String>> iter= ss.iterator();iter.hasNext();){
		   Entry<Integer,String> temp=iter.next();
		   System.out.println(temp.getKey()+"--"+temp.getValue());
	   }
	   
	   //第二種
	   Set<Integer> keySet=map.keySet();
	   for(Iterator<Integer> iter=keySet.iterator();iter.hasNext();){
		   Integer key=iter.next();
		   System.out.println(key+"-------"+map.get(key));
	   }
	}
}

  TreeSet实现排序:

import java.util.Set;
import java.util.TreeSet;

public class TestTreeSet {
public static void main(String[] args) {
	Set<Integer> set=new TreeSet<>();
	set.add(300);
	set.add(200);
	set.add(600);
	
	for(Integer m:set)
		System.out.println(m);
	
	
	Set<Emp2>  set2 = new TreeSet<>();
	set2.add(new Emp2(100,"张三",3000));
	set2.add(new Emp2(50,"李四",2000));
	set2.add(new Emp2(150,"王五",8000));
	set2.add(new Emp2(30,"赵六",20000));
	
	for(Emp2 m:set2){
		System.out.println(m);
	}
}
}
class Emp2  implements Comparable<Emp2>  {
	int id;
	String name;
	double salary;
	
	public Emp2(int id, String name, double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}

	@Override
	public String toString() {
		return  "id:"+id+",name:"+name+",salary:"+salary;
	}
	
	@Override
	public int compareTo(Emp2 o) {		//负数:小于,0:等于,正数:大于
		
		if(this.salary>o.salary){
			return 1;
		}else if(this.salary<o.salary){
			return -1;
		}else{
			if(this.id>o.id){
				return 1;
			}else if(this.id<o.id){
				return -1;
			}else{
				return 0;
			}
		}
	}
}

  TreeMap实现排序:

import java.util.Map;
import java.util.TreeMap;

public class TestTreeMap {
	public static void main(String[] args) {
       Map<Integer,String> treeMap1=new TreeMap();
       treeMap1.put(20, "aa");
       treeMap1.put(3, "bb");
       treeMap1.put(6, "cc");
       
       for(Integer key:treeMap1.keySet()){
    	   System.out.println(key+"----"+treeMap1.get(key));
       }
       
       Map<Employee,String> treeMap2=new TreeMap<Employee,String>();
       treeMap2.put(new Employee(100,"張三",5000), "111");
       treeMap2.put(new Employee(200,"李四",500), "222");
       treeMap2.put(new Employee(150,"王五",6000), "333");
       
       //按照key遞增的方式排序
       for(Employee key:treeMap2.keySet()){
    	   System.out.println(key+"---"+treeMap2.get(key));
       }
	}
}

class Employee implements Comparable<Employee>{
	int id;
	String name;
	double salary;
	public Employee(int id, String name, double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	@Override
	public String toString() {
		return  "id:"+id+",name:"+name+",salary:"+salary;
	}
	@Override
	public int compareTo(Employee o){
		if(this.salary>0)return 1;
		else if(this.salary<o.salary)return -1;
		else{
			if(this.id>o.id)return 1;
			else if(this.id<o.id) return -1;
			else return 0;
		}
	}
}

  

posted @ 2019-11-04 20:10  石shi  阅读(2302)  评论(0编辑  收藏  举报