.war vs .ear file
2021-6-3 anglehua
What is the difference between a .war and .ear file? From GeekInterview: In J2EE application, modules are packaged as EAR, JAR, and WAR based on their functionality JAR: EJB modules which contain enterprise java beans (class files) and EJB deployment descriptor are packed as JAR files with .j...Should I always use a parallel stream when possible?
2021-6-3 anglehua
With Java 8 and lambdas it's easy to iterate over collections as streams, and just as easy to use a parallel stream. Two examples from the docs, the second one using parallelStream: myShapesCollection.stream() .filter(e -> e.getColor() == Color.RED) .forEach(e -> System.out.println(e.g...Check if at least two out of three booleans are true
2021-6-3 anglehua
An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true. My solution follows: boolean atLeastTwo(boolean a, boolean b, boolean c) { if ((a && b) || (b && c) || (a && c)) { re...Android changing Floating Action Button color
2021-6-3 anglehua
I have been trying to change Material's Floating Action Button color, but without success. <android.support.design.widget.FloatingActionButton android:id="@+id/profile_edit_fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bo...How to build jars from IntelliJ properly?
2021-6-3 anglehua
I have a project that contains a single module, and some dependencies. I'd like to create a jar, in a separate directory, that contains the compiled module. In addition, I'd like to have the dependencies present beside my module. No matter how I twist IntelliJ's "build jar" process, the output of my...When to use: Java 8+ interface default method, vs. abstract method
2021-6-3 anglehua
Java 8 allows for default implementation of methods in interfaces called Default Methods. I am confused between when would I use that sort of interface default method, instead of an abstract class (with abstract method(s)). So when should interface with default methods be used and when should an abs...How do I count the number of occurrences of a char in a String?
2021-6-3 anglehua
I have the string a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop). My 'idiomatic one-liner' for this is: int ...How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?
2021-6-3 anglehua
How can I tell if the JVM in which my application runs is 32 bit or 64-bit? Specifically, what functions or properties I can used to detect this within the program? You retrieve the system property that marks the bitness of this JVM with: System.getProperty("sun.arch.data.model"); Possible result...How to convert comma-separated String to List?
2021-6-3 anglehua
Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? String commaSeparated = "item1 , item2 , item3"; List<String> items = //method that converts above string into list?...Why am I getting a NoClassDefFoundError in Java?
2021-6-3 anglehua
I am getting a NoClassDefFoundError when I run my Java application. What is typically the cause of this? This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths. ...Why does Math.round(0.49999999999999994) return 1?
2021-6-3 anglehua
In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5. for (int i = 10; i >= 0; i--) { long l = Double.doubleToLongBits(i + 0.5); double x; do { x = Double.longBitsToDouble(l); System.out.println(x + " rounded is " +...Run a single test method with maven
2021-6-3 anglehua
I know you can run all the tests in a certain class using: mvn test -Dtest=classname But I want to run an individual method and -Dtest=classname.methodname doesn't seem to work. To run a single test method in Maven, you need to provide the command as: mvn test -Dtest=TestCircle#xyz test where Te...When should I use File.separator and when File.pathSeparator?
2021-6-3 anglehua
In the File class there are two strings, separator and pathSeparator. What's the difference? When should I use one over the other? If you mean File.separator and File.pathSeparator then: File.pathSeparator is used to separate individual file paths in a list of file paths. Consider on windows, the ...Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?
2021-6-3 anglehua
Java is an optional package on the latest versions of macOS. Yet once installed it appears like the JAVA_HOME environment variable is not set properly. With the Java optional package or Oracle JDK installed, adding one of the following lines to your ~/.bash_profile file will set the environment v...How do I programmatically determine operating system in Java?
2021-6-3 anglehua
I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different properties based on whether I am on a Windows or Unix platform). What is the safest way to do this with 100% reliability? You can use: ...Is there anything like .NET's NotImplementedException in Java?
2021-6-3 anglehua
Is there anything like .NET's NotImplementedException in Java? Commons Lang has it. Or you could throw an UnsupportedOperationException. I think the java.lang.UnsupportedOperationException is what you are looking for. 采集自互联网,如有侵权请联系本人How to split a string with any whitespace chars as delimiters
2021-6-3 anglehua
What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters? Something in the lines of myString.split("\\s+"); This groups all white spaces as a delimiter. So if I have the...Can you find all classes in a package using reflection?
2021-6-3 anglehua
Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.) Due to the dynamic nature of class loaders, this is not possible. Class loaders are not required to tell the VM which classes it can provide, instead they are just handed r...How to install the JDK on Ubuntu Linux
2021-6-3 anglehua
Note: This is an old question and the answers reflect the world as it was then. Modern Ubuntu distributions have OpenJDK available which can be installed with sudo apt install default-jdk I am trying to install the Java Development Kit (JDK) on Ubuntu Linux distribution, but I am unable to insta...Can enums be subclassed to add new elements?
2021-6-3 anglehua
I want to take an existing enum and add more elements to it as follows: enum A {a,b,c} enum B extends A {d} /*B is {a,b,c,d}*/ Is this possible in Java? No, you can't do this in Java. Aside from anything else, d would then presumably be an instance of A (given the normal idea of "extends"), but ...