I have the following modeling problem: as an exercise, I need to model a system that allocates units (police cars/fire trucks) to a specific emergency (fire/robbery/…). Therefore an emergency needs …
Custom HashMap implementation
This question was asked to me in an interview. I think the only way to get best solution is SOF. So the question was “How would you implement a custom HashMap in java(Assume there is no such data structure called HashMap)”. The only answer I could think of was by implementing Associative Arrays (b…
How can I find whitespace in a String?
How can I check to see if a String contains a whitespace character, an empty space or ” “. If possible, please provide a Java example. For example: String = “test word”; Answer For checking if a string contains whitespace use a Matcher and call its find method. If you want to check if …
String Comparison in Java
What does “compare two strings lexicographically” mean? Answer Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary. The Java String class provides the .compareTo () method in order to lexicographically compare Stri…
How to calculate the number of lines in a “Java Project”
How can I calculate the number of lines in a “Java Project”? I’m using Netbeans 6.9. Answer Have a look at the NetBeans Metrics Module. NetBeans Metric Module is a module for NetBeans that can measure your java source code and display the results in NetBeans. Or the wordcount plugin. Counts …
The purpose of interfaces continued
OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract. And I semi see the point of them. But if all you have in the interface is: and it has no implementation as such, then whoever uses your i…
Count words, java
I want to count words. I use the methods hasNextChar and getChar. The sentence may contain all kind of chars. Here’s my code: It works so far but e.g. when I have a ” . ” at the end it gives me 8 instead of 7 words. Here are some examples of sentences: *„Schreiben Sie ein Praktikanten-Vermit…
How to upload sources to local Maven repository
Suppose I have a Maven 2 Java project on my local machine. I build the project .jar file and push it to my local Maven repo, using mvn install. Question: How can I force Maven to also push the project sources jar to the local repo? This is useful if I’ll use the above mentioned project as dependency whi…
“Java DateFormat is not threadsafe” what does this leads to?
Everybody cautions regarding Java DateFormat not being thread safe and I understand the concept theoretically. But I’m not able to visualize what actual issues we can face due to this. Say, I’ve a DateFormat field in a class and the same is used in different methods in the class (formatting dates)…
How do you create a proper Epoch calendar object in Java?
I want to create a calendar object that is the epoch date. What’s the ‘correct’ (if any) way to do it? Answer The ‘setTimeInMillis()’ method would work fine and be easily understood by others. It might also be clearer if you passed it 0 instead of 1. The first method has more lin…