Map遍历
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
//增强For循环
public class Main {
public static void main(String[] args) {
// 创建一个HashMap对象,并加入了一些键值对。
Map<String, String> maps = new HashMap<String, String>();
maps.put("1", "PHP");
maps.put("2", "Java");
maps.put("3", "C");
maps.put("4", "C++");
maps.put("5", "HTML");
// 传统的遍历map集合的方法1; keySet()
//traditionalMethod1(maps);
// 传统的遍历map集合的方法2; entrySet()
//traditionalMethod2(maps);
// 使用增强For循环来遍历map集合方法1; keySet()
//strongForMethod1(maps);
// 使用增强For循环来遍历map集合方法2; entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map<String, String> maps) {
Set<Entry<String, String>> set = maps.entrySet();
for (Entry<String, String> entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map<String, String> maps) {
Set<String> set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// 使用entrySet()方法,获取maps集合中的每一个键值对,
private static void traditionalMethod2(Map<String, String> maps) {
Set<Map.Entry<String, String>> sets = maps.entrySet();
// 取得迭代器遍历出对应的值。
Iterator<Entry<String, String>> it = sets.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = (Entry<String, String>) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// 使用keySet()方法,获取maps集合中的所有键,遍历键取得所对应的值。
private static void traditionalMethod1(Map<String, String> maps) {
Set<String> sets = maps.keySet();
// 取得迭代器遍历出对应的值
Iterator<String> it = sets.iterator();
while (it.hasNext()) {
String key = it.next();
String value = maps.get(key);
System.out.println(key + " : " + value);
}
}
}
以上代码运行输出结果为:
1 : PHP
2 : Java
3 : C
4 : C++
5 : HTML
根据 key从map 里取出元素,并转成 Long、Integer
Long value1 = MapUtils.getLong(map, key);
Integer value2 = MapUtils.getInteger(map, key);
这样取出数据能够实现先判空,再判断类型,之后转换,防止报错。
内部实现:
public static Long getLong(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Long) {
return (Long) answer;
}
return new Long(answer.longValue());
}
集合反转(List反向输出)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
class Main {
public static void main(String[] args) {
String[] coins = { "A", "B", "C", "D", "E" };
List l = new ArrayList();
for (int i = 0; i < coins.length; i++)
l.add(coins[i]);
ListIterator liter = l.listIterator();
System.out.println("反转前");
while (liter.hasNext())
System.out.println(liter.next());
Collections.reverse(l);
liter = l.listIterator();
System.out.println("反转后");
while (liter.hasNext())
System.out.println(liter.next());
}
}
以上代码运行输出结果为:
反转前
A
B
C
D
E
反转后
E
D
C
B
A
删除集合中指定元素(Set删除指定元素)
import java.util.*;
public class Main {
public static void main(String [] args) {
System.out.println( "集合实例!\n" );
int size;
HashSet collection = new HashSet ();
String str1 = "Yellow", str2 = "White", str3 =
"Green", str4 = "Blue";
Iterator iterator;
collection.add(str1);
collection.add(str2);
collection.add(str3);
collection.add(str4);
System.out.print("集合数据: ");
iterator = collection.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
collection.remove(str2);
System.out.println("删除之后 [" + str2 + "]\n");
System.out.print("现在集合的数据是: ");
iterator = collection.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
size = collection.size();
System.out.println("集合大小: " + size + "\n");
}
}
以上代码运行输出结果为:
集合实例!
集合数据: White Yellow Blue Green
删除之后 [White]
现在集合的数据是: Yellow Blue Green
集合大小: 3
只读集合
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] argv)
throws Exception {
List stuff = Arrays.asList(new String[] { "a", "b" });
List list = new ArrayList(stuff);
list = Collections.unmodifiableList(list);
try {
list.set(0, "new value");
}
catch (UnsupportedOperationException e) {
}
Set set = new HashSet(stuff);
set = Collections.unmodifiableSet(set);
Map map = new HashMap();
map = Collections.unmodifiableMap(map);
System.out.println("集合现在是只读");
}
}
以上代码运行输出结果为:
集合现在是只读
集合输出(TreeMap操作)
import java.util.*;
public class Main{
public static void main(String[] args) {
System.out.println("TreeMap 实例!\n");
TreeMap tMap = new TreeMap();
tMap.put(1, "Sunday");
tMap.put(2, "Monday");
tMap.put(3, "Tuesday");
tMap.put(4, "Wednesday");
tMap.put(5, "Thursday");
tMap.put(6, "Friday");
tMap.put(7, "Saturday");
System.out.println("TreeMap 键:"
+ tMap.keySet());
System.out.println("TreeMap 值:"
+ tMap.values());
System.out.println("键为 5 的值为: " + tMap.get(5)+ "\n");
System.out.println("第一个键: " + tMap.firstKey()
+ " Value: "
+ tMap.get(tMap.firstKey()) + "\n");
System.out.println("最后一个键: " + tMap.lastKey()
+ " Value: "+ tMap.get(tMap.lastKey()) + "\n");
System.out.println("移除第一个数据: "
+ tMap.remove(tMap.firstKey()));
System.out.println("现在 TreeMap 键为: "
+ tMap.keySet());
System.out.println("现在 TreeMap 包含: "
+ tMap.values() + "\n");
System.out.println("移除最后一个数据: "
+ tMap.remove(tMap.lastKey()));
System.out.println("现在 TreeMap 键为: "
+ tMap.keySet());
System.out.println("现在 TreeMap 包含: "
+ tMap.values());
}
}
以上代码运行输出结果为:
TreeMap 实例!
TreeMap 键:[1, 2, 3, 4, 5, 6, 7]
TreeMap 值:[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
键为 5 的值为: Thursday
第一个键: 1 Value: Sunday
最后一个键: 7 Value: Saturday
移除第一个数据: Sunday
现在 TreeMap 键为: [2, 3, 4, 5, 6, 7]
现在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
移除最后一个数据: Saturday
现在 TreeMap 键为: [2, 3, 4, 5, 6]
现在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday]
集合转数组(List转Array)
import java.util.*;
public class Main{
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("我");
list.add("的");
list.add("天");
list.add("堂");
list.add("www.sanguo.com");
String[] s1 = list.toArray(new String[0]);
for(int i = 0; i < s1.length; ++i){
String contents = s1[i];
System.out.print(contents);
}
}
}
以上代码运行输出结果为:
我的天堂www.sanguo.com
List 循环移动元素
以下实例演示了如何使用 Collections 类的 rotate() 来循环移动元素,方法第二个参数指定了移动的起始位置:
import java.util.*;
public class Main {
public static void main(String[] args) {
List list = Arrays.asList("one Two three Four five six".split(" "));
System.out.println("List :"+list);
Collections.rotate(list, 3);
System.out.println("rotate: " + list);
}
}
以上代码运行输出结果为:
List :[one, Two, three, Four, five, six]
rotate: [Four, five, six, one, Two, three]