Java 实例 - 数组转集合
以下实例演示了使用 Java Util 类的 Arrays.asList(name) 方法将数组转换为集合:
import java.util.*; import java.io.*; public class ArrayToCollection{ public static void main(String args[]) throws IOException{ int n = 5; // 5 个元素 String[] name = new String[n]; for(int i = 0; i < n; i++){ name[i] = String.valueOf(i); } List<String> list = Arrays.asList(name); System.out.println(); for(String li: list){ String str = li; System.out.print(str + " "); } } }
以上代码运行输出结果为:
0 1 2 3 4
Java 实例 - 集合比较
以下实例将字符串转换为集合并使用 Collection 类的 Collection.min() 和 Collection.max() 来比较集合中的元素:
import java.util.Collections; import java.util.Set; import java.util.TreeSet; class Main { public static void main(String[] args) { String[] coins = { "Penny", "nickel", "dime", "Quarter", "dollar" }; Set<String> set = new TreeSet<String>(); for (int i = 0; i < coins.length; i++) { set.add(coins[i]); } System.out.println(Collections.min(set)); System.out.println(Collections.min(set, String.CASE_INSENSITIVE_ORDER)); for (int i = 0; i <= 10; i++) { System.out.print("-"); } System.out.println(""); System.out.println(Collections.max(set)); System.out.println(Collections.max(set, String.CASE_INSENSITIVE_ORDER)); } }
以上代码运行输出结果为:
Penny dime ----------- nickel Quarter
Java 实例 - HashMap遍历
以下实例演示了如何使用 Collection 类的 iterator() 方法来遍历集合:
import java.util.*; public class Main { public static void main(String[] args) { HashMap< String, String> hMap = new HashMap< String, String>(); hMap.put("1", "1st"); hMap.put("2", "2nd"); hMap.put("3", "3rd"); Collection cl = hMap.values(); Iterator itr = cl.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
1st 2nd 3rd
Java 实例 - 集合长度
以下实例演示了如何使用 Collections 类 的collection.add() 来添加数据并使用 collection.size()来计算集合的长度:
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(); size = collection.size(); if (collection.isEmpty()){ System.out.println("集合是空的"); } else{ System.out.println( "集合长度: " + size); } System.out.println(); } }
以上代码运行输出结果为:
集合实例! 集合数据: White Yellow Blue Green 集合长度: 4
Java 实例 - 集合打乱顺序
以下实例演示了如何使用 Collections 类 Collections.shuffle() 方法来打乱集合元素的顺序:
import java.util.*; public class Main { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) list.add(new Integer(i)); System.out.println("打乱前:"); System.out.println(list); for (int i = 1; i < 6; i++) { System.out.println("第" + i + "次打乱:"); Collections.shuffle(list); System.out.println(list); } } }
打乱前: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 第1次打乱: [2, 0, 5, 1, 4, 9, 7, 6, 3, 8] 第2次打乱: [2, 6, 4, 8, 5, 7, 9, 1, 0, 3] 第3次打乱: [6, 5, 1, 0, 3, 7, 2, 4, 9, 8] 第4次打乱: [1, 3, 8, 4, 7, 2, 0, 6, 5, 9] 第5次打乱: [3, 0, 7, 9, 5, 8, 4, 2, 1, 6]