public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value); //说明key和value值都是可以为null
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) { //当value==null的时候,会抛出异常
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
hash = hash(key);
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry<K,V> e = tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
return null;
}HashMap<String, Integer> map = new HashMap<String, Integer>(20);
Map<String, Integer> map = new HashMap<String, Integer>(20);
for(Map.Entry<String, Integer> entry : map.entrySet()){ //直接遍历出Entry
System.out.println("key-->"+entry.getKey()+",value-->"+m.get(entry.getValue()));
}Map<String, Integer> map = new HashMap<String, Integer>(20);
Iterator<String> keySet = map.keySet().iterator(); //遍历Hash表中的key值集合,通过key获取value
while(keySet .hasNext()){
Object key = keySet .next();
System.out.println("key-->"+key+",value-->"+m.get(key));
}异常信息:java.lang.OutOfMemoryError:PermGen space
方法区溢出也是一种常见的内存溢出异常,一个类如果要被垃圾收集器回收,判定条件是很苛刻的。在经常动态生成大量Class的应用中,要特别注意这点。
if(bitmapObject.isRecycled()==false) //如果没有回收
bitmapObject.recycle(); //在程序onCreate时就可以调用 即可 privatefinalstaticfloat
TARGET_HEAP_UTILIZATION = 0.75f; privatefinalstaticintCWJ_HEAP_SIZE
= 6* 1024* 1024;
//设置最小heap内存为6MB大小 VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);public class OutClass {
private String mName = "lly";
static int mAge = 12;
class InnerClass{
String name;
int age;
private void getName(){
name = mName;
age = mAge;
System.out.println("name="+name+",age="+age);
}
}
public static void main(String[] args) {
//第一种初始化内部类方法
OutClass.InnerClass innerClass = new OutClass().new InnerClass();
innerClass.getName();
//第二种初始化内部类方法
OutClass out = new OutClass();
InnerClass in = out.new InnerClass();
in.getName();
}
}public class OutClass {
private String mName = "lly";
static int mAge = 12;
static class StaticClass{
String name = "lly2";
int age;
private void getName(){
// name = mName; //不能引用外部类的非静态成员变量
age = mAge;
System.out.println("name="+name+",age="+age);
}
}
public static void main(String[] args) {
//第一种初始化静态内部类方法
OutClass.StaticClass staticClass = new OutClass.StaticClass();
staticClass.getName();
//或者直接使用静态内部类初始化
StaticClass staticClass2 = new StaticClass();
staticClass2.getName();
}
}textView.setOnClickListener(new View.OnClickListener(){ //OnClickListener为一个接口interface
public void onClick(View v){
...
}
});public abstract class Animal {
public abstract void getColor();
}
public class Dog{
public static void main(String[] args) {
Animal dog = new Animal() {
@Override
public void getColor() {
System.out.println("黑色");
}
};
dog.getColor();
}
}原文:http://blog.csdn.net/shakespeare001/article/details/51274685