【DataStructure】Charming usage of Set in the java

In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  methods in the java api. After investiagting the document of java api, the result is so satisfying that I speak hightly of wisdom of developer of java language.Next
I will introduce charming usage about set in the java.

[java] view
plain
copy

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.TreeSet;
  8. public class SetUtil
  9. {
  10. public static List<String> testStrList = Arrays.asList("Apple", "Orange",
  11. "Pair", "Grape", "Banana", "Apple", "Orange");
  12. /**
  13. * Gets sorted sets which contains no duplicate elements
  14. *
  15. * @time Jul 17, 2014 7:58:16 PM
  16. * @return void
  17. */
  18. public static void sort()
  19. {
  20. Set<String> sortSet = new TreeSet<String>(testStrList);
  21. System.out.println(sortSet);
  22. // output : [Apple, Banana, Grape, Orange, Pair]
  23. }
  24. public static void removeDuplicate()
  25. {
  26. Set<String> uniqueSet = new HashSet<String>(testStrList);
  27. System.out.println(uniqueSet);
  28. <span style="font-family: Arial, Helvetica, sans-serif;">// output : </span><span style="font-family: Arial, Helvetica, sans-serif;">[Pair, Apple, Banana, Orange, Grape]</span>
  29. }
  30. public static void reverse()
  31. {
  32. Set<String> sortSet = new TreeSet<String>(testStrList);
  33. List<String> sortList = new ArrayList<String>(sortSet);
  34. Collections.reverse(sortList);
  35. System.out.println(sortList);
  36. // output : [Pair, Orange, Grape, Banana, Apple]
  37. }
  38. public static void swap()
  39. {
  40. Set<String> sortSet = new TreeSet<String>(testStrList);
  41. List<String> sortList = new ArrayList<String>(sortSet);
  42. Collections.swap(sortList, 0, sortList.size() - 1);
  43. System.out.println(sortList);
  44. output : [Apple, Orange, Grape, Banana, Pair]
  45. }
  46. public static void main(String[] args)
  47. {
  48. SetUtil.sort();
  49. SetUtil.reverse();
  50. SetUtil.swap();
  51. SetUtil.removeDuplicate();
  52. }
  53. }

【DataStructure】Charming usage of Set in the java,布布扣,bubuko.com

时间: 2024-10-11 09:01:50

【DataStructure】Charming usage of Set in the java的相关文章

【DataStructure】Another usage of List: Polynomial

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Apolynomialis a mathematical function of the form: p(x) = a0xn+ a1xn–1+a2xn–2+ ???+an–1x + an The greatest exponent, 

【DataStructure】Description and usage of queue

[Description] A queue is a collection that implements the first-in-first-out protocal. This means that the only accessiable object in the collection in the first one that was inserted. The most common example of a queue is a waiting line. [Interface]

【DataStructure】One of queue usage: Simulation System

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] This simulationillustrates objectoriented programming(OOP). Java objects are instantiated to represent all the interacti

【DataStructure】Descriptioin and usage of List

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Alistis a collection of elements that are accessible sequentially: the first element, followed by the second element, fo

【DataStructure】The description and usage of Stack

A stack is collection that implements the last-in-first-out protocal.This means that the only access object in the collections is the last one thatwas inserted.The fundamental  operations of a stack are: add an element onto the top of stack, access t

【dataStructure】 Arrays and Java Source Review

According to the order of data structure book, Arrays should be introduced in the frist time. When reviewing the some information related to arrays, I feel shocked that many useful classes and methods in java language has been ignored. Now set a simp

【DataStructure】Description and Introduction of Tree

[Description] At ree is a nonlinear data structure that models a hierarchical organization. The characteristic eatures are that each element may have several successors (called its "children") and every element except one (called the "root&

【DataStructure】Implemantation of Binary Tree

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] Here is a class for binary trees that directly implements the recursive definition. By extending the AbstractCollectionclass, it remai

【DataStructure】 Classical Question: Josephus Cycle

[Description] This problem is based upon a report by the historian Joseph ben Matthias (Josephus) on the outcome of a suicide pact that he had made between himself and 40 soldiers as they were besieged by superior Roman forces in 67 A.D. Josephus pro