首页javasetJava Collection - 如何查找Set的最大/最小元素

Java Collection - 如何查找Set的最大/最小元素

我们想知道如何查找Set的最大/最小元素。
 


import java.util.Collections;
import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
    HashSet<Long> hashSet = new HashSet<Long>();
    hashSet.add(new Long("1111111111"));
    hashSet.add(new Long("2222222222"));
    hashSet.add(new Long("3333333333"));
    hashSet.add(new Long("4444444444"));
    hashSet.add(new Long("5555555555"));

    Object obj = Collections.max(hashSet);
    //Object obj = Collections.min(hashSet);
    System.out.println(obj);
  }
}