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...

阅读全文>>

评论(0) 浏览(140)

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. ...

阅读全文>>

评论(0) 浏览(107)

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<>(...

阅读全文>>

评论(0) 浏览(113)

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...

阅读全文>>

评论(0) 浏览(112)

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...

阅读全文>>

评论(0) 浏览(117)

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...

阅读全文>>

评论(0) 浏览(91)

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...

阅读全文>>

评论(0) 浏览(111)

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...

阅读全文>>

评论(0) 浏览(158)

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 ...

阅读全文>>

评论(0) 浏览(107)

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...

阅读全文>>

评论(0) 浏览(90)

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, ...

阅读全文>>

评论(0) 浏览(131)

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...

阅读全文>>

评论(0) 浏览(122)

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 ...

阅读全文>>

评论(0) 浏览(115)

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...

阅读全文>>

评论(0) 浏览(213)

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? ...

阅读全文>>

评论(0) 浏览(140)

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 ...

阅读全文>>

评论(0) 浏览(116)

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...

阅读全文>>

评论(0) 浏览(116)

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...

阅读全文>>

评论(0) 浏览(127)

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...

阅读全文>>

评论(0) 浏览(109)

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...

阅读全文>>

评论(0) 浏览(108)

Powered by emlog 京ICP备15036472号-3 sitemap