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 list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
for (Iterator item = list.iterator(); item.hasNext();) {
String temp = item.next();
System.out.println(temp);
}
}
public static void testIteratorSet(){
Set set=new HashSet<>();
set.add("11");
set.add("22");
set.add("33");
for(Iterator iter=set.iterator();iter.hasNext();){
String temp=iter.next();
System.out.println(temp);
}
}
//第一种
public static void testIteratorMap(){
Map map=new HashMap<>();
map.put(100, "aa");
map.put(400, "bb");
map.put(500, "cc");
Set> ss=map.entrySet();
for(Iterator> iter= ss.iterator();iter.hasNext();){
Entry temp=iter.next();
System.out.println(temp.getKey()+"--"+temp.getValue());
}
//第二種
Set keySet=map.keySet();
for(Iterator 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 set=new TreeSet<>();
set.add(300);
set.add(200);
set.add(600);
for(Integer m:set)
System.out.println(m);
Set 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 {
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
return -1;
}else{
if(this.id>o.id){
return 1;
}else if(this.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 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 treeMap2=new TreeMap();
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{
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
else{
if(this.id>o.id)return 1;
else if(this.id
else return 0;
}
}
}