How to check if a String is numeric in Java

2021-6-3 anglehua

How would you check if a String was a number before parsing it? With Apache Commons Lang 3.5 and above: NumberUtils.isCreatable or StringUtils.isNumeric. With Apache Commons Lang 3.4 and below: NumberUtils.isNumber or StringUtils.isNumeric. You can also use StringUtils.isNumericSpace which returns ...

阅读全文>>

评论(0) 浏览(234)

Failed to load the JNI shared Library (JDK)

2021-6-3 anglehua

When I try opening Eclipse, a pop-up dialog states: Failed to load the JNI shared library "C:/JDK/bin/client/jvm.dll"`. Following this, Eclipse force closes. Here's a few points I'd like to make: I checked to see if anything exists at that path. It does exist. My Eclipse and Java SE Developme...

阅读全文>>

评论(0) 浏览(216)

How to create RecyclerView with multiple view type?

2021-6-3 anglehua

From https://developer.android.com/preview/material/ui-widgets.html When we create RecyclerView.Adapter we have to specify ViewHolder that will bind with the adapter. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; public MyAdapt...

阅读全文>>

评论(0) 浏览(289)

How to reference a method in javadoc?

2021-6-3 anglehua

How can I use the @link tag to link to a method? I want to change: /** * Returns the Baz object owned by the Bar object owned by Foo owned by this. * A convenience method, equivalent to getFoo().getBar().getBaz() * @return baz */ public Baz fooBarBaz() to: /** * Returns the Baz object owned by...

阅读全文>>

评论(0) 浏览(236)

What exactly is a Maven Snapshot and why do we need it?

2021-6-3 anglehua

I am a bit confused about the meaning of a Maven Snapshot and why we build one? A snapshot version in Maven is one that has not been released. The idea is that before a 1.0 release (or any other release) is done, there exists a 1.0-SNAPSHOT. That version is what might become 1.0. It's basically "1....

阅读全文>>

评论(0) 浏览(230)

Java string to date conversion

2021-6-3 anglehua

What is the best way to convert a String in the format 'January 2, 2010' to a Date in Java? Ultimately, I want to break out the month, the day, and the year as integers so that I can use Date date = new Date(); date.setMonth().. date.setYear().. date.setDay().. date.setlong currentTime = date.getTim...

阅读全文>>

评论(0) 浏览(226)

Error:java: javacTask: source release 8 requires target release 1.8

2021-6-3 anglehua

Using IntelliJ IDE can't compile any projects. Screenshots of settings below: Used JDK: Project SDK and Language level: Language Level: Anybody have any ideas? File > Settings > Build, Execution, Deployment > Compiler > Java Compiler Change Target bytecode version to 1.8 of the modu...

阅读全文>>

评论(0) 浏览(288)

When and how should I use a ThreadLocal variable?

2021-6-3 anglehua

When should I use a ThreadLocal variable? How is it used? One possible (and common) use is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object (I'm looking at you, SimpleDateFormat). Instead, give each thread its own instance of the object. ...

阅读全文>>

评论(0) 浏览(220)

How do I time a method's execution in Java?

2021-6-3 anglehua

How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc? Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want. There is always the old-fashioned way: long startTime = ...

阅读全文>>

评论(0) 浏览(201)

How do I convert a String to an InputStream in Java?

2021-6-3 anglehua

Given a string: String exampleString = "example"; How do I convert it to an InputStream? Like this: InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8)); Note that this assumes that you want an InputStream that is a stream of bytes that represent your orig...

阅读全文>>

评论(0) 浏览(230)

How do I write a correct micro-benchmark in Java?

2021-6-3 anglehua

How do you write (and run) a correct micro-benchmark in Java? I'm looking for some code samples and comments illustrating various things to think about. Example: Should the benchmark measure time/iteration or iterations/time, and why? Related: Is stopwatch benchmarking acceptable? Tips about writin...

阅读全文>>

评论(0) 浏览(202)

What do 3 dots next to a parameter type mean in Java?

2021-6-3 anglehua

What do the 3 dots following String in the following method mean? public void myMethod(String... strings){ // method body } It means that zero or more String objects (or a single array of them) may be passed as the argument(s) for that method. See the "Arbitrary Number of Arguments" section he...

阅读全文>>

评论(0) 浏览(267)

Convert InputStream to byte array in Java

2021-6-3 anglehua

How do I read an entire InputStream into a byte array? You can use Apache Commons IO to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte[]. InputStream is; byte[] bytes = IOUtils.toByteArray(is); Internally this creates a ByteArrayOutputS...

阅读全文>>

评论(0) 浏览(269)

How can I read a large text file line by line using Java?

2021-6-3 anglehua

I need to read a large text file of around 5-6 GB line by line using Java. How can I do this quickly? A common pattern is to use try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { // process the line. } } Yo...

阅读全文>>

评论(0) 浏览(243)

How to configure port for a Spring Boot application

2021-6-3 anglehua

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080. As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with server.p...

阅读全文>>

评论(0) 浏览(211)

How do I check if a file exists in Java?

2021-6-3 anglehua

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl's -e $filename)? The only similar question on SO deals with writing the file and was thus answered using FileWriter which is obviously not applicable here. If possible I'd prefer a real API call r...

阅读全文>>

评论(0) 浏览(208)

A 'for' loop to iterate over an enum in Java

2021-6-3 anglehua

I have an enum in Java for the cardinal & intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of these enum values? .values() You can call the val...

阅读全文>>

评论(0) 浏览(188)

How to use Jackson to deserialise an array of objects

2021-6-3 anglehua

The Jackson data binding documentation indicates that Jackson supports deserialising "Arrays of all supported types" but I can't figure out the exact syntax for this. For a single object I would do this: //json input { "id" : "junk", "stuff" : "things" } //Java MyClass instance = objectMap...

阅读全文>>

评论(0) 浏览(218)

How to convert a Java 8 Stream to an Array?

2021-6-3 anglehua

What is the easiest/shortest way to convert a Java 8 Stream into an array? The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method. String[] stringArray = stringStream.toArray(St...

阅读全文>>

评论(0) 浏览(193)

Why is 2 * (i * i) faster than 2 * i * i in Java?

2021-6-3 anglehua

The following Java program takes on average between 0.50 secs and 0.55 secs to run: public static void main(String[] args) { long startTime = System.nanoTime(); int n = 0; for (int i = 0; i < 1000000000; i++) { n += 2 * (i * i); } System.out.println((double) (System.na...

阅读全文>>

评论(0) 浏览(220)

Powered by emlog 京ICP备15036472号-3 sitemap