Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public void go()
  2. {
  3. ArrayList<String> fruits = new ArrayList<>();
  4.  
  5. fruits.add("pear");
  6. fruits.add("pineapple");
  7. fruits.add("oranges");
  8. fruits.add("banana");
  9. fruits.add("apple");
  10.  
  11. for(String str: fruits)
  12. {
  13. if(str.contains("apple"))
  14. { /* I would like anything containing 'apple'
  15. * i.e. apple & pineapple to be placed on top of the array*/ }
  16. }
  17. for(String str: fruits)
  18. {
  19. System.out.println(str);
  20. }
  21. }
  22.  
  23. List<String> apples = new ArrayList<>();
  24. List<String> nonApples = new ArrayList<>();
  25. for (String fruit : fruits) {
  26. if (fruit.contains("apple")) {
  27. apples.add(fruit);
  28. } else {
  29. nonApples.add(fruit);
  30. }
  31. }
  32. Collections.sort(apples);
  33. apples.addAll(nonApples);
  34. // apples now contains what you want. Of course, it's now misnamed :)
  35.  
  36. List<String> result = new ArrayList<>(apples);
  37. Collections.sort(result);
  38. result.addAll(nonApples);
  39.  
  40. for (int i = 0; i < result.size(); i++) {
  41. fruits.set(i, result.get(i));
  42. }
  43.  
  44. Collections.sort(fruits, new Comparator<String>(){
  45.  
  46. @Override
  47. public int compare(String arg0, String arg1) {
  48. if (arg0.contains("apple") && arg1.contains("apple")) {
  49. return arg0.compareTo(arg1);
  50. }
  51. else if (arg0.contains("apple") && !arg1.contains("apple")){
  52. return -1;
  53. }
  54. else if (!arg0.contains("apple") && arg1.contains("apple")){
  55. return 1;
  56. } else return 0;
  57. } });
  58.  
  59. public class AppleStringComparator implements Comparator<String> {
  60.  
  61. @Override
  62. public int compare(String o1, String o2) {
  63. if (o1.contains("apple")) {
  64. if (o2.contains("apple")) {
  65. return o1.compareTo(o2);
  66. } else {
  67. return -1;
  68. }
  69. } else {
  70. if (o2.contains("apple")) {
  71. return 1;
  72. } else {
  73. return 0;
  74. }
  75. }
  76. }
  77. }
  78.  
  79. public class MainClass {
  80. public void go() {
  81. // The example code from the OP
  82. ArrayList<String> fruits = new ArrayList<>();
  83.  
  84. fruits.add("pear");
  85. fruits.add("pineapple");
  86. fruits.add("oranges");
  87. fruits.add("banana");
  88. fruits.add("apple");
  89.  
  90. Collections.sort(fruits, new AppleStringComparator());
  91.  
  92. // Print out the result, for completeness' sake:
  93. System.out.println(fruits);
  94. }
  95.  
  96. List<String> containingApple = new ArrayList<>();
  97. for (String s : list) {
  98. if (s.contains("apple")) {
  99. containingApple.add(s);
  100. }
  101. }
  102. Collections.sort(containingApple);
  103. List<String> result = new ArrayList<>(list.size());
  104. result.addAll(containingApple);
  105. for (String s : list) {
  106. if (!s.contains("apple")) {
  107. result.add(s);
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement