Javaトレーニング-集合
public static void test1(){
ArrayList aList=new ArrayList();
aList.add(5);
aList.add(2);
aList.add(0);
aList.add(" ");
aList.add('a');
//for
for (int i = 0; i
10. Map :Key Set ,value collection , Set
public static void putElements(){
hashMap.put("huang", "ming");
hashMap.put("chao", "sheng");
hashMap.put("shi", "tian");
int size=hashMap.size();
System.out.println("size="+size);
}
//key set
public static void getKey(){
Set keyset=hashMap.keySet();
Iterator it=keyset.iterator();
while(it.hasNext()){
Object key=it.next();
System.out.println("keys="+key);
}
}
//value collection
public static void getValues(){
Collection coll=hashMap.values();
Iterator it=coll.iterator();
for (Iterator iterator = coll.iterator(); iterator.hasNext();) {
Object value = iterator.next();
System.out.println("values="+value);
}
}
//key-value entrySet
public static void getKV(){
// for
for(Object obj:hashMap.entrySet()){
Entry entry=(Entry) obj;
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println("key="+key+";value="+value);
}
// Key Value
for(Object obj:hashMap.keySet()){
Object key=obj;
Object value=hashMap.get(obj);
System.out.println("key="+key+";value="+value);
}
// Entry key value
Set entrySet=hashMap.entrySet();
Iterator it=entrySet.iterator();
while(it.hasNext()){
Entry entry=(Entry) it.next();
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println("key="+key+";value="+value);
}
}
11.Stackクラス://
public static void test(){
Stack stack=new Stack();
stack.add("apple");
stack.add("banana");
stack.add(123);
stack.add(new Date());
Iterator it=stack.iterator();
while(it.hasNext()){
Object obj=it.next();
System.out.println(obj);
}
//peek ,
Object peekObj=stack.peek();
System.out.println("peekObj:"+peekObj);
System.out.println("peek after:"+stack.size());
//pop ,
Object popObj=stack.pop();
System.out.println("popObj:"+popObj);
System.out.println("pop after:"+stack.size());
}
12. された の を します.public static void countChar(){
Scanner sc=new Scanner(System.in);
System.out.println(" :");
String s=sc.next();
try {
// GBK , UTF-8 。
s = new String(s.getBytes("GBK"), "utf-8");
System.out.println(s);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char[] c=s.toCharArray();
Map map=new TreeMap();
for (int i = 0; i