HashSet

这个程序将从System.in读取单词,将他们逐个添加到HashSet对象中,最后打印20个单词。

  1. package set;
  2. import java.util.*;
  3. /**
  4. * 这个程序打印System.in中的不同单词,用来测试HashSet类
  5. */
  6. public class SetTest
  7. {
  8. public static void main(String[] args)
  9. {
  10. var words = new HashSet<String>();
  11. long totalTime = 0;
  12. try (var in = new Scanner(System.in))
  13. {
  14. while (in.hasNext())
  15. {
  16. String word = in.next();
  17. long callTime = System.currentTimeMillis();
  18. words.add(word);
  19. callTime = System.currentTimeMillis() - callTime;
  20. totalTime += callTime;
  21. }
  22. }
  23. Iterator<String> iter = words.iterator();
  24. for (int i = 1; i <= 20 && iter.hasNext(); i++)
  25. System.out.println(iter.next());
  26. System.out.println(". . .");
  27. System.out.println(words.size() + " 个不同的单词. " + totalTime + " 毫秒.");
  28. }
  29. }

可以在命令行中重定向系统输入:java set.SetTest < alice30.txt
下面这个名为 alice30.txt 的文本文件有5909个不同的单词
alice30.txt

TreeSet

下面的程序用两个TreeSet对象来组织Item对象,第一个按照编号排序,这是Item对象的默认排序顺序。第二个按照自定义的比较器来排序。

  1. package set;
  2. import java.util.*;
  3. /**
  4. * 用两种方法来对Item对象进行排序
  5. */
  6. public class TreeSetTest
  7. {
  8. public static void main(String[] args) {
  9. var tree1 = new TreeSet<Item>();
  10. tree1.add(new Item("Toaster", 1234));
  11. tree1.add(new Item("Widget", 4562));
  12. tree1.add(new Item("Modem", 9912));
  13. System.out.println(tree1);
  14. var tree2 = new TreeSet<Item>(new AComparator());
  15. tree2.addAll(tree1);
  16. System.out.println(tree2);
  17. }
  18. }
  19. class AComparator implements Comparator<Item> {
  20. @Override
  21. public int compare(Item o1, Item o2) {
  22. return o1.getDescription().compareTo(o2.getDescription());
  23. }
  24. }
  1. package set;
  2. import java.util.*;
  3. /**
  4. * 定义一个物品类,包含两个实例字段description和partNumber
  5. */
  6. class Item implements Comparable<Item>
  7. {
  8. private String description;
  9. private int partNumber;
  10. /**
  11. * 构造一个物品类
  12. * @param aDescription 物品的描述
  13. * @param aPartNumber 物品的编号
  14. */
  15. public Item(String aDescription, int aPartNumber)
  16. {
  17. description = aDescription;
  18. partNumber = aPartNumber;
  19. }
  20. public String toString()
  21. {
  22. return "[description=" + description + ", partNumber=" + partNumber + "]";
  23. }
  24. public boolean equals(Object otherObject)
  25. {
  26. if (this == otherObject) return true;
  27. if (otherObject == null) return false;
  28. if (getClass() != otherObject.getClass()) return false;
  29. var other = (Item) otherObject;
  30. return Objects.equals(description, other.description)
  31. && partNumber == other.partNumber;
  32. }
  33. public int hashCode()
  34. {
  35. return Objects.hash(description, partNumber);
  36. }
  37. public int compareTo(Item other)
  38. {
  39. int diff = Integer.compare(partNumber, other.partNumber);
  40. // 先按编号排序,编号相同按描述排序
  41. return diff != 0 ? diff : description.compareTo(other.description);
  42. }
  43. public String getDescription()
  44. {
  45. return description;
  46. }
  47. }