Java multiline string
2021-6-3 anglehua
Coming from Perl, I sure am missing the "here-document" means of creating a multi-line string in source code: $string = <<"EOF" # create a three-line string text text text EOF In Java, I have to have cumbersome quotes and plus signs on every line as I concatenate my multiline string from scr...The Use of Multiple JFrames: Good or Bad Practice?
2021-6-3 anglehua
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago. ...Convert Set to List without creating new List
2021-6-3 anglehua
I am using this code to convert a Set to a List: Map<String, List<String>> mainMap = new HashMap<>(); for (int i=0; i < something.size(); i++) { Set<String> set = getSet(...); //returns different result each time List<String> listOfNames = new ArrayList<>(...How to nicely format floating numbers to string without unnecessary decimal 0's
2021-6-3 anglehua
A 64-bit double can represent integer +/- 253 exactly. Given this fact, I choose to use a double type as a single type for all my types, since my largest integer is an unsigned 32-bit number. But now I have to print these pseudo integers, but the problem is they are also mixed in with actual doubles...Difference between @Mock and @InjectMocks
2021-6-3 anglehua
What is the difference between @Mock and @InjectMocks in Mockito framework? @Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock (or @Spy) annotations into this instance. Note you must use @RunWith(MockitoJUnitRunner.class) or Moc...Spring: @Component versus @Bean
2021-6-3 anglehua
I understand that @Component annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning. @Bean was introduced in spring 3.0 and can be used with @Configuration in order to fully get rid of xml file and use java config instead. Would it have been po...How to make an Android device vibrate? with different frequency?
2021-6-3 anglehua
I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this? Try: import android.os.Vibrator; ... Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds if (Build.VERSION.SDK_INT >= Build.VERS...Getting a File's MD5 Checksum in Java
2021-6-3 anglehua
I am looking to use Java to get the MD5 checksum of a file. I was really surprised but I haven't been able to find anything that shows how to get the MD5 checksum of a file. How is it done? There's an input stream decorator, java.security.DigestInputStream, so that you can compute the digest while...How to remove the last character from a string?
2021-6-3 anglehua
I want to remove the last character from a string. I've tried doing this: public String method(String str) { if (str.charAt(str.length()-1)=='x'){ str = str.replace(str.substring(str.length()-1), ""); return str; } else{ return str; } } Getting the length of the ...How do I remove repeated elements from ArrayList?
2021-6-3 anglehua
I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this? If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (w...Convert java.util.Date to java.time.LocalDate
2021-6-3 anglehua
What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Date input = new Date(); LocalDate date = ??? Short answer Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); Explanation Despite its name, ...Converting between java.time.LocalDateTime and java.util.Date
2021-6-3 anglehua
Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime, for holding a timezone-independent date-with-time value. There are probably millions of lines of code using the legacy class java.util.Date for this purpose. As such, when interfacing old...What are enums and why are they useful?
2021-6-3 anglehua
Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread safety benefits to such solution. I have never used enums and I have been programing in Java for more than couple a years now. And apparently they changed ...How can I convert my Java program to an .exe file?
2021-6-3 anglehua
If I have a Java source file (*.java) or a class file (*.class), how can I convert it to a .exe file? I also need an installer for my program. javapackager The Java Packager tool compiles, packages, and prepares Java and JavaFX applications for distribution. The javapackager command is the command...Java 8 Lambda function that throws exception?
2021-6-3 anglehua
I know how to create a reference to a method that has a String parameter and returns an int, it's: Function<String, Integer> However, this doesn't work if the function throws an exception, say it's defined as: Integer myMethod(String s) throws IOException How would I define this reference? ...String concatenation: concat() vs “+” operator
2021-6-3 anglehua
Assuming String a and b: a += b a = a.concat(b) Under the hood, are they the same thing? Here is concat decompiled as reference. I'd like to be able to decompile the + operator as well to see what that does. public String concat(String s) { int i = s.length(); if (i == 0) { return ...Why can't I define a static method in a Java interface?
2021-6-3 anglehua
EDIT: As of Java 8, static methods are now allowed in interfaces. Here's the example: public interface IXMLizable<T> { static T newInstanceFromXML(Element e); Element toXMLElement(); } Of course this won't work. But why not? One of the possible issues would be, what happens when you call...Where to get “UTF-8” string literal in Java?
2021-6-3 anglehua
I'm trying to use a constant instead of a string literal in this piece of code: new InputStreamReader(new FileInputStream(file), "UTF-8") "UTF-8" appears in the code rather often, and would be much better to refer to some static final variable instead. Do you know where I can find such a variable i...Java 8 Distinct by property
2021-6-3 anglehua
In Java 8 how can I filter a collection using the Stream API by checking the distinctness of a property of each object? For example I have a list of Person object and I want to remove people with the same name, persons.stream().distinct(); Will use the default equality check for a Person object, so...java.util.Date vs java.sql.Date
2021-6-3 anglehua
java.util.Date vs java.sql.Date: when to use which and why? Congratulations, you've hit my favorite pet peeve with JDBC: Date class handling. Basically databases usually support at least three forms of datetime fields which are date, time and timestamp. Each of these have a corresponding class in J...