Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. package Comparing;
  2.  
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. /**
  8. * Created by Yura on 20.02.2017.
  9. */
  10. public class Names {
  11. private List<String > names;
  12.  
  13. public Names(List<String> names) {
  14. this.names = names;
  15. }
  16.  
  17. public void addName(String name) {
  18. names.add(name);
  19. }
  20.  
  21. @Override
  22. public String toString() {
  23. return "Names: " + names + "";
  24. }
  25.  
  26. public void namesInAlphabeticalOrder (){
  27. Collections.sort(names);
  28. }
  29.  
  30. public void namesReversOrder(){
  31. Collections.sort(names, Collections.reverseOrder());
  32. }
  33.  
  34. public String findMinName() {
  35. return Collections.min(names);
  36. }
  37.  
  38. public void numberOfLetters() {
  39. Collections.sort(names, new LengthComparator());
  40. }
  41.  
  42. public void numberOfLettersReversOrder() {
  43. Collections.sort(names, new numberOfLettersReversOrder());
  44. }
  45.  
  46. private class LengthComparator implements Comparator<String>{
  47. @Override
  48. public int compare(String o1, String o2) {
  49. return Integer.compare(o1.length(), o2.length());
  50. }
  51.  
  52. }
  53.  
  54. private class numberOfLettersReversOrder implements Comparator<String>{
  55. @Override
  56. public int compare(String o1, String o2) {
  57. int lengthResult = Integer.compare(o1.length(), o2.length());
  58.  
  59. if (lengthResult != 0){
  60. return o1.compareTo(o2);
  61. }
  62.  
  63. return lengthResult;
  64. }
  65.  
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement