java-8

Java, in which thread are sequential streams executed?

ε祈祈猫儿з 提交于 2020-01-23 05:22:40
问题 While reading the documentation about streams, I came across the following sentences: ... attempting to access mutable state from behavioral parameters presents you with a bad choice ... if you do not synchronize access to that state, you have a data race and therefore your code is broken ... [1] If the behavioral parameters do have side-effects ... [there are no] guarantees that different operations on the "same" element within the same stream pipeline are executed in the same thread. [2]

SONAR: Replace this lambda with a method reference

自作多情 提交于 2020-01-23 04:32:20
问题 Sonar tells me "Replace this lambda with a method reference" public class MyClass { private List<SomeValue> createSomeValues(List<Anything> anyList) { return anyList // .stream() // .map(anything -> createSomeValue(anything)) // .collect(Collectors.toList()); } private SomeValue createSomeValue(Anything anything) { StatusId statusId = statusId.fromId(anything.getStatus().getStatusId()); return new SomeValue(anything.getExternId(), statusId); } } Is this possible here? I tried several things,

Number of String Objects on heap in JAVA-8

你。 提交于 2020-01-23 03:34:05
问题 From this Number of String Objects on stack overflow,I came to know that if we do some thing like : String s = new String("ABC"); Then we have two objects one on heap that is String and one on constant pool that is "ABC" , But today I took the heap dump and found there are two objects on heap it self. I used MAT tool for the same please find the screen shot below. So my query is if there are two Objects on heap one of Char[] and other for String class and one on constant pool then thus this

Number of String Objects on heap in JAVA-8

亡梦爱人 提交于 2020-01-23 03:33:45
问题 From this Number of String Objects on stack overflow,I came to know that if we do some thing like : String s = new String("ABC"); Then we have two objects one on heap that is String and one on constant pool that is "ABC" , But today I took the heap dump and found there are two objects on heap it self. I used MAT tool for the same please find the screen shot below. So my query is if there are two Objects on heap one of Char[] and other for String class and one on constant pool then thus this

Why does stream parallel() not use all available threads?

早过忘川 提交于 2020-01-23 02:52:27
问题 I tried to run 100 Sleep tasks in parallel using Java8(1.8.0_172) stream.parallel() submitted inside a custom ForkJoinPool with 100+ threads available. Each task would sleep for 1s. I expected the whole work would finish after ~1s, given the 100 sleeps could be done in parallel. However I observe a runtime of 7s. @Test public void testParallelStream() throws Exception { final int REQUESTS = 100; ForkJoinPool forkJoinPool = null; try { // new ForkJoinPool(256): same results for all tried

Java 8 localdatetime vs Date

百般思念 提交于 2020-01-23 02:37:05
问题 I have a string 2017-07-31T01:01:00-07:00 and I am trying to parse it to date and in CST Timezone. I am getting different results when i parse this string using Date and Java 8 ZonedDateTime. I am not getting why this is happening and what I am doing wrong. String dateStr = "2017-07-31T01:01:00-07:00"; LocalDateTime time = null; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-hh"); String[] dateArray = dateStr.split("-"); String[] timeZones = TimeZone .getAvailableIDs

Java 8 Split String and Create Map inside Map

耗尽温柔 提交于 2020-01-22 19:51:08
问题 I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4 . I want to add this in to a Map<String, Map<String, String>> . Like: {101={1=5, 2=4}, 102={1=5, 2=5, 3=5}, 103={1=4}} How to do this using Java 8 stream? I tried using normal Java. And it works fine. public class Util { public static void main(String[] args) { String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4"; Map<String, Map<String, String>> m1 = new HashMap<>(); Map<String, String> m2 = null; String[] items

Find direct and indirect subclasses by scanning filesystem

微笑、不失礼 提交于 2020-01-22 17:42:10
问题 I'm having a problem in writing an algorithm to help me scan a file system and find all subclasses of a certain class. Details: I've an app that scans an external application using nio Files.walk() while retrieving I check for "extends SuperClass" while reading the file if the word exits, I add the class name in my list as follows: List<String> subclasses = new ArrayList<>(); Files.walk(appPath) .filter(p->Files.isRegularFile(p) && p.toString() .endsWith(".java")).forEach(path -> { try { List

hibernate 5 + ZonedDateTime + postgresql include time zone and the offset

笑着哭i 提交于 2020-01-22 17:28:27
问题 I have a running app spring boot 1.3 + hibernate 5 + java 8 + ZonedDateTime + postgresql and in one of the tables I have the following fields. @Column(name = "DATE_ENABLED") @Type(type="java.time.ZonedDateTime") private ZonedDateTime dateEnabled; @Column(name = "DATE_DISABLED") @Type(type="java.time.ZonedDateTime") private ZonedDateTime dateDisabled; If I run the app then I see that this by default produces "timestamp without time zone" testDB=# \d type Table "public.type" Column | Type |

Stream ordered/unordered problems

夙愿已清 提交于 2020-01-22 14:15:30
问题 I have the following code: Set<Integer> l = new TreeSet<>(); l.add(1); l.add(10); l.add(3); l.add(-3); l.add(-4); and I want to unorder the collection with: l.stream().unordered().forEach(System.out::println); but the forEach returns always the collection ordered! Then I have also another doubt about this sentence: For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream