1. Map

1.1. map中的方法

Java学习---Map的学习-LMLPHP

1.2. Map.Entry

对于集合来讲,就是把kye-value的数据保存在了Map.Entry的实例之后,再在Map集合中插入了一个Map.Entry的实例化对象

Map.Entry一般用于输出集合

Java学习---Map的学习-LMLPHP

1.3. Map接口的常用子类

Java学习---Map的学习-LMLPHP

1.4. HashTable和HashMap区别

Java学习---Map的学习-LMLPHP

1.5. Map的标准输出(2个)

Java学习---Map的学习-LMLPHP

Map的方法使用集合

 package com.ftl;

 import java.util.*;

 class Person implements Comparable<Person>
{
private String name;
private int age;
public Person(String name, int age)
{
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "姓名:" + this.name + ", 年龄:" + this.age ;
}
public boolean equals(Object obj)
{
boolean flag = true;
if ( this == obj)
{
return flag ;
}
if ( !(obj instanceof Person))
{
flag = false;
}
Person p = (Person)obj;
if (this.name.equals(p.name)
&& this.age == p.age)
{
return flag;
}
else
{
flag = false;
} return flag;
} public int hashCode()
{
return this.name.hashCode() * this.age;
} @Override
public int compareTo(Person per) {
if ( this.age > per.age)
{
return 1;
}
else if (this.age < per.age)
{
return -1;
}
else
{
return this.name.compareTo(per.name);
}
} } public class HelloFtl {
public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
System.out.println("--------------------------------------");
List<String> allList = null;
Collection<String> col = null;
allList = new ArrayList<String>();
col = new ArrayList<String>();
allList.add("Hello");
allList.add("world");
System.out.println(allList);
System.out.println("--------------------------------------");
col.add("HHH");
col.add("FTL");
col.add("WM");
allList.addAll(allList.size(), col);
System.out.println(allList);
// System.out.println("--------------------------------------");
// allList.remove(0);
// System.out.println(allList);
// System.out.println("--------------------------------------");
allList.removeAll(col);
// System.out.println(allList);
System.out.println("--------------------------------------");
for(int i = 0; i < allList.size(); i++)
{
System.out.print(allList.get(i) + "、");
}
System.out.println();
System.out.println("--------------------------------------");
for(int i = allList.size()-1; i >=0 ; i--)
{
System.out.print(allList.get(i) + "、");
}
System.out.println();
System.out.println("--------------------------------------");
Object[] obj = allList.toArray();
for(int i = 0; i <obj.length; i++)
{
String temp = (String)obj[i];
System.out.print(temp + "、");
}
System.out.println();
System.out.println("--------------------------------------");
System.out.println(allList.contains("HHH") ? "lv存在": "lv不存在");
System.out.println(allList.contains("lv的位置是:" + allList.indexOf("HHH")));
System.out.println(allList.contains("allList否为空" + allList.isEmpty() ));
System.out.println("--------------------------------------");
LinkedList<String> link = new LinkedList<String>();
link.add("A");
link.add("B");
link.add("C");
link.addFirst("Root");
link.addLast("End");
System.out.print(link);
System.out.println();
System.out.println("Element: " + link.element());
System.out.println("Pool: " + link.poll());
System.out.print(link);
System.out.println();
System.out.println("Peek: " + link.peek());
System.out.print(link);
System.out.println();
System.out.println("--------------------------------------");
for ( int i = 0; i < link.size(); i++)
{
System.out.print("Pool: "
+ "" + link.poll() + "、");
}
System.out.println();
System.out.println("--------------------------------------");
Set<Person > set = new HashSet<Person>();
// Set<Person> set = new TreeSet<Person>();
set.add(new Person("Root2", 12));
set.add(new Person("AA", 21));
set.add(new Person("BB", 22));
set.add(new Person("CC", 22));
set.add(new Person("CC", 22));
set.add(new Person("CC", 12));
System.out.println( set);
System.out.println("--------------------------------------");
System.out.println("Iterator: ");
Iterator<Person> iter = set.iterator();
while(iter.hasNext())
{
System.out.print(iter.next() + "、");
}
System.out.println();
System.out.println("--------------------------------------");
while(iter.hasNext())
{
Person str = iter.next();
if ("CC".equals(str))
{
iter.remove();
}
else
{
System.out.print(str + "、");
}
}
System.out.println("--------------------------------------");
Map<String ,String> map = null;
map = new HashMap<String, String>();
map.put("04122", "FTL1012");
map.put("04122076", "FTL");
map.put("04122078", "1012");
System.out.println(map.containsKey("04122"));
System.out.println(map.containsValue("04122"));
System.out.println("04122 --> " + map.get("04122"));
System.out.println("--------------------------------------");
System.out.println("HashMap 中所有的Key:");
Set<String> sset = map.keySet();
Iterator<String> it = sset.iterator();
while (it.hasNext())
{
String t = (String) it.next();
System.out.print( t + "\t");
}
System.out.println("--------------------------------------");
System.out.println("HashMap 中所有的Value:");
Collection<String> co = map.values();
Iterator<String> itt = co.iterator();
while (itt.hasNext())
{
String tt = (String) itt.next();
System.out.print( tt + "\t");
}
System.out.println("--------------------------------------");
map = new TreeMap<String, String>();
map.put("04122", "FTL1012");
map.put("04122076", "FTL");
map.put("04122078", "1012");
Set<String> keys = map.keySet();
Iterator<String> key = keys.iterator();
System.out.println("TreeMap 中所有的Key:");
while (key.hasNext())
{
String tt = (String)key.next();
System.out.print( key + "\t");
}
System.out.println("--------------------------------------");
Collection<String> ccc = map.values();
Iterator<String> ttt = ccc.iterator();
System.out.println("TreeMap 中所有的Value:");
while (ttt.hasNext())
{
String tt = (String) ttt.next();
System.out.print( ttt + "\t");
} System.out.println("--------------------------------------");
}
};

Map的标准输出示例:

 package com.ftl;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class HelloFtl {
public static void main(String[] args){
Map<String,String>map = new TreeMap<>();
map.put("Hello", "world");
map.put("Hello1", "world1");
map.put("Hello2", "world2");
map.put("Hello3", "world3");
String str = map.get("Hello");
Set<String> set = map.keySet();
Collection<String> values = map.values();
Iterator<String> iterator = set.iterator();
Iterator<String> iter = values.iterator();
System.out.println("------------------------------------");
System.out.println("\nforeach方法:");
for (String s : values) {
System.out.print(s+"、");
}
System.out.println("------------------------------------");
System.out.println("\nIterator方法:");
while(iterator.hasNext()){
System.out.print(iterator.next()+"、");
}
System.out.println("------------------------------------");
System.out.println("\nMap的标准输出_1(Map.entrySet):");
Set<Entry<String,String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, String> next = it.next();
System.out.print(next.getKey()+"-->: "+next.getValue()+"\n");
}
System.out.println("------------------------------------");
System.out.println("\nMap的标准输出_2(Foreach):");
for (Map.Entry<String, String> entry : entrySet) {
System.out.print(entry.getKey()+"-->: "+entry.getValue()+"\n");
}
System.out.println("------------------------------------");
System.out.println("Map.entrySet()大小:"+map.entrySet().size());
System.out.println("------------------------------------");
}}

Map的迭代输出

 package test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test
{
public static void main(String[] args) throws Exception
{
Map<String,String> map = new HashMap();
map.put("hello", "world");
map.put("hello1", "world1");
map.put("hello2", "world2");
map.put("hello3", "world3");
map.put("hello4", "world4");
String str = map.get("hello");
boolean containsKey = map.containsKey("hello");
boolean containsValue = map.containsValue("world");
Set<String> keySet = map.keySet();
Collection<String> values = map.values(); Iterator<String> iter = values.iterator();
while(iter.hasNext()){
String s = iter.next();
System.out.println(s+"E");
} }
}
05-15 02:17