Map的遍历输出和Collections以及Properties

来源:互联网 发布:python公约数 编辑:程序博客网 时间:2024/04/27 00:09
package com.oracle.collection;


import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;


import org.junit.Test;


public class Test_Map {


@Test
public void test() {
Map map = new HashMap();


map.put("AA", "11");
map.put("BB", "11");
map.put("CC", "11");
map.put("DD", "11");
map.put("AA", "11");
System.out.println(map.get(2));


map.put(new Person("tom", 18), "AA");
map.put(new Person("tom", 18), "AA");


System.out.println(map);


}


@Test
public void test1() {
Map map = new HashMap();
map.put("AA1", "AA");
map.put("BB1", "BB");
map.put("CC1", "CC");
map.put("DD1", "DD");

Set set=map.keySet();       //取出map中的键值
Collection coll =map.values();         //取出map中所有的value值
Set set1=map.entrySet();             //取出map中的映射

Iterator it=set1.iterator();

while(it.hasNext()){
//Map.Entry m=(Map.Entry)it.next();
System.out.println(it.next());
// System.out.print(m.getKey());
// System.out.println(m.getValue());
}

Set set2=map.keySet();
Iterator its=set2.iterator();
while(its.hasNext()){

String string=(String) its.next();
System.out.println(string);
System.out.println(map.get(string));
}

// System.out.println(coll);
// System.out.println(set);
// System.out.println(set1);

}

@Test
public void test2() {
Map map = new LinkedHashMap<>();
map.put("11", 11);
map.put("22", 22);
map.put("33", 33);
map.put("44", 44);

System.out.println(map);
}



@Test 
public void test3(){
TreeMap treemap=new TreeMap();
treemap.put(new Person("tom", 18), "AA");
treemap.put(new Person("jane", 18), "AA");
treemap.put(new Person("mike", 18), "AA");
System.out.println(treemap);
}


@Test 
public void test4() throws Exception{

Properties proper=new Properties();        //处理属性文件
proper.load(new FileInputStream(new File("jdbc.property")));
//proper.getProperty(key)

String usname=proper.getProperty("username");
String pass =proper.getProperty("password");
System.out.println(usname);
System.out.println(pass);
}



@Test 
public void test5(){
List list=new ArrayList();
list.add("AA");
list.add("BB");
list.add("CC");
list.add("DD");
System.out.println(list);

Collections.reverse(list);  //反转
System.out.println(list);

Collections.shuffle(list);   //随机排序
System.out.println(list);

Collections.sort(list);

List lists=Collections.synchronizedList(list);
}
}
0 0
原创粉丝点击