java复习笔记6

   Map的常见之类

  HashMap, HashTable, TreeMap, WeakHashMap, IdentityHashMap

  最常用的HashMap的使用方法:

运行下面代码

复制代码
import java.util.*;
public class HashMapDemo {
    public static void main(String args[]) {
        HashMap<String,String> hm = new HashMap<String,String>();
        hm.put("0","00");
        hm.put("1","11");
        hm.put("2","22");
        hm.put("3","33");
        System.out.println(hm.keySet());
        System.out.println(hm.values());
        System.out.println(hm.get("1"));
        System.out.println(hm.size());

        //Map.Entry
        Set<Map.Entry<String,String>> es = hm.entrySet();
        Iterator<Map.Entry<String, String>> it = es.iterator();
        while(it.hasNext()) {
            Map.Entry<String,String> me = it.next();
            System.out.println(me.getKey()+"_"+me.getValue());
        }

        //foreach Map.Entry
        for(Map.Entry<String,String> me:hm.entrySet()){
            System.out.println(me.getKey()+"_"+me.getValue());
        }

        Set<String> hs = hm.keySet();
        Iterator it0 = hs.iterator();
        while(it0.hasNext()) {
            String key = (String)it0.next();
            System.out.println( key +"_" +hm.get(key));
        }
    }
}
复制代码

  

  HashMap

  HashMap的键值重复

  HashMap判断key重复是根据key的equals()和hashCode() 判断,如果key的equals是相等和hasCode是相等的话,那就认为元素是重复的, 不往map中存放:

  HashMap类和HashSet类判断元素是否重复方式是一样的

运行下面代码

复制代码
import java.util.*;
class Person{
    public String name = "";
    public Person(String name){
        this.name = name;
    }
    public boolean equals( Object p) {
        if( this == p) {
            return true;
        };
        if( p instanceof Person ) {
            Person pp = (Person)p;
            if(this.name == pp.name){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    public int hashCode(){
        return this.name.hashCode();
    }
    public String toString(){
        return this.name;
    }
}
public class HashMapRepeatDemo {
    public static void main(String args[]) {
        Map<Person,String> hm = new HashMap<Person,String>();
        hm.put(new Person("nono"), "1");
        hm.put(new Person("11"), "2");
        hm.put(new Person("nono"), "1");
        hm.put(new Person("nono"), "1");
        
        System.out.println(hm);
    }
}
复制代码

 

  IdentityHashMap

  IdentityHashMap允许往HashMap中存放key值重复的对象。

运行下面代码

复制代码
import java.util.*;
class Person{
    public String name = "";
    public Person(String name){
        this.name = name;
    }
    public boolean equals( Object p) {
        if( this == p) {
            return true;
        };
        if( p instanceof Person ) {
            Person pp = (Person)p;
            if(this.name == pp.name){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    public int hashCode(){
        return this.name.hashCode();
    }
    public String toString(){
        return this.name;
    }
}
public class IdentityHashMapDemo{
    public static void main(String args[]) {
        Map<Person,String> hm = new IdentityHashMap<Person,String>();
        hm.put(new Person("nono"), "1");
        hm.put(new Person("11"), "2");
        hm.put(new Person("nono"), "1");
        hm.put(new Person("nono"), "1");
        for(Map.Entry<Person,String> me:hm.entrySet()) {
            System.out.print("instanceof Person : ");
            System.out.println(me.getKey() instanceof Person);
            System.out.print("key is : ");
            System.out.println(me.getKey());
            System.out.print("value is : ");
            System.out.println(me.getValue());
        }
        System.out.println(hm);
    }
}
复制代码

 

 

  SortedMap

  SortedMap是TreeMap的抽象实现, 要使用SortedMap,必须要new TreeMap();

运行下面代码

复制代码
import java.util.*;
public class SortedHashMapDemo {
    public static void main(String args[]) {
        SortedMap<String, String> sm = new TreeMap<String, String>();
        sm.put("aa","aa");
        sm.put("bb","bb");
        sm.put("cc","cc");
        sm.put("dd","dd");
        System.out.println(sm);
        System.out.println(sm.firstKey());
        System.out.println(sm.lastKey());
        System.out.println(sm.subMap("aa","dd"));
        System.out.println(sm.headMap("dd"));
        System.out.println(sm.tailMap("aa"));
    }
}
复制代码

 

  TreeMap

运行下面代码

复制代码
import java.util.*;
public class TreeMapDemo {
    public static void main(String args[]) {
        TreeMap<String,String> tm = new TreeMap<String,String>();
        tm.put("a","a");
        System.out.println(tm);
    }
}
复制代码

 

  Collections

  Collections这个工具类也可以作为Set或者List的辅助方法, 只是平常可能用的少, 利用Collection可以实现addAll的功能, 参数是可变的;

运行下面代码

复制代码
import java.util.*;
public class CollectionsDemo {
    public static void main(String args[]) {
        //Collections.emptyList();
        List<String> allList = new ArrayList<String>();
        Set<String> allSet = Collections.emptySet();
        //allList.add("hello"); <<== Wrong
        //allList.add("world"); <<== Wrong
        String[] strArray="Sweet0,Sour0".split(",");
        Collections.addAll(allList, strArray);
        Collections.addAll(allList, ":");
        System.out.println(allList);
    }
}
复制代码

 

 

  Stack类栈的操作

  Stack的search是从栈底一直往上堆彻的, 所以使用search时候输出的index和ArrayList是相反的

运行下面代码

复制代码
import java.util.*;
public class StackDemo {
    public static void main(String args[]) {
        Stack<String> sk = new Stack<String>();
        //push
        sk.push("a");
        sk.push("b");
        sk.push("C");
        sk.push("d");
        System.out.println(sk);
        //peek
        System.out.println(sk.peek());
        //pop
        System.out.println(sk.pop());
        System.out.println(sk);
        //search
        System.out.println(sk.search("a")); //这个居然输出3
        System.out.println(sk.search("b")); //这个输出2
        System.out.println(sk.search("C")); //这个输出1
        //size
        System.out.println(sk.size());
    }
}
复制代码

 

  properties类

  这个要配合xml进行存储, 一般用来保存配置的

运行下面代码

复制代码
import java.util.*;
import java.io.*;
public class PropertyDemo {
    public static void main(String args[]) {
        Properties pro = new Properties();
        pro.setProperty("xx","xx");
        pro.setProperty("yy","yy");
        pro.setProperty("zz","zz");
        System.out.println(pro.getProperty("xx"));
        File file = new File("H:\\work\\java\\demo\\web\\xx.properties");
        try{
            pro.store(new FileOutputStream(file), "Info");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
复制代码

 

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/5230463.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(227)  评论(0编辑  收藏  举报
历史上的今天:
2014-03-03 最近新学的小东西和单词

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示