fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.*;
  5. import java.util.stream.*;
  6. import java.lang.*;
  7. import java.io.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. ThreadPoolExecutor pool = new ThreadPoolExecutor(8, 8, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
  15. //ExecutorService pool = Executors.newFixedThreadPool(2);
  16. //ForkJoinPool pool = new ForkJoinPool(8);
  17.  
  18. List<Record> list = new ArrayList<>();
  19. for (int i = 0; i < 115000; i++)
  20. {
  21. list.add(new Record("A", i / 10000, "B", "C", i, (double) i / 100 + 1));
  22. }
  23. System.out.println("ArrayList's spliterator is ORDERED: " +
  24. ((list.spliterator().characteristics() & Spliterator.ORDERED) != 0));
  25.  
  26. Stream<Record> stream = list.parallelStream()
  27. //.parallel()
  28. .sorted(
  29. (r1, r2) -> Integer.compare(r1.getCategory2(), r2.getCategory2())
  30. )
  31. //.parallel()
  32. ;
  33.  
  34. List<Record> output = stream.collect(Collectors.toList());
  35.  
  36. System.out.println(output.size());
  37. int prev = -1;
  38. boolean verified = true;
  39. for (Record record : output)
  40. {
  41. int curr = record.getValue1();
  42. if (prev != -1)
  43. {
  44. if (prev + 1 != curr)
  45. {
  46. System.out.println("Warning: " + prev + " followed by " + curr + "!");
  47. verified = false;
  48. }
  49. }
  50. prev = curr;
  51. }
  52. System.out.println("Verified: " + verified);
  53. }
  54. }
  55.  
  56. class Record implements Comparable<Record>
  57. {
  58. private String myCategory1;
  59. private int myCategory2;
  60. private String myCategory3;
  61. private String myCategory4;
  62. private int myValue1;
  63. private double myValue2;
  64.  
  65. public Record(String category1, int category2, String category3, String category4,
  66. int value1, double value2)
  67. {
  68. myCategory1 = category1;
  69. myCategory2 = category2;
  70. myCategory3 = category3;
  71. myCategory4 = category4;
  72. myValue1 = value1;
  73. myValue2 = value2;
  74. }
  75.  
  76. public String getCategory1()
  77. {
  78. return myCategory1;
  79. }
  80.  
  81. public int getCategory2()
  82. {
  83. return myCategory2;
  84. }
  85.  
  86. public String getCategory3()
  87. {
  88. return myCategory3;
  89. }
  90.  
  91. public String getCategory4()
  92. {
  93. return myCategory4;
  94. }
  95.  
  96. public int getValue1()
  97. {
  98. return myValue1;
  99. }
  100.  
  101. public double getValue2()
  102. {
  103. return myValue2;
  104. }
  105.  
  106. public int compareTo(Record other)
  107. {
  108. int comp = myCategory1.compareTo(other.myCategory1);
  109. if (comp != 0) return comp;
  110. comp = myCategory2 - other.myCategory2;
  111. if (comp != 0) return comp;
  112. comp = myCategory3.compareTo(other.myCategory3);
  113. if (comp != 0) return comp;
  114. comp = myCategory4.compareTo(other.myCategory4);
  115. return comp;
  116. }
  117.  
  118. /**
  119.   * Returns the string representation.
  120.   * @return The string representation.
  121.   */
  122. public String toString()
  123. {
  124. StringBuilder buf = new StringBuilder();
  125. buf.append("Record(");
  126. buf.append(myCategory1);
  127. buf.append(",");
  128. buf.append(myCategory2);
  129. buf.append(",");
  130. buf.append(myCategory3);
  131. buf.append(",");
  132. buf.append(myCategory4);
  133. buf.append(",");
  134. buf.append(myValue1);
  135. buf.append(",");
  136. buf.append(myValue2);
  137. buf.append(")");
  138. return buf.toString();
  139. }
  140. }
Success #stdin #stdout 0.29s 321728KB
stdin
Standard input is empty
stdout
ArrayList's spliterator is ORDERED: true
115000
Warning: 69999 followed by 71875!
Warning: 79061 followed by 70000!
Warning: 71874 followed by 79062!
Warning: 99999 followed by 100625!
Warning: 107811 followed by 100000!
Warning: 100624 followed by 107812!
Verified: false