How to export a Swagger JSON/YAML file from Swagger UI?

2021-6-7 anglehua

How can I export a Swagger definition file (it should be a JSON or YAML file)? Let's say I have an endpoint looking like http://example.com//swagger/ui/index#!: The version is api version: v1. There is no "Export" button that I can see. So how do I export it? The URL of the API definiton is displa...

阅读全文>>

评论(0) 浏览(3505)

Why is processing a sorted array faster than processing an unsorted array?

2021-6-3 anglehua

Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data miraculously makes the code almost six times faster: #include <algorithm> #include <ctime> #include <iostream> int main() { // Generate data const unsigned arraySi...

阅读全文>>

评论(0) 浏览(968)

Why is subtracting these two times (in 1927) giving a strange result?

2021-6-3 anglehua

If I run the following program, which parses two date strings referencing times 1 second apart and compares them: public static void main(String[] args) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str3 = "1927-12-31 23:54:07"; Str...

阅读全文>>

评论(0) 浏览(914)

Is Java “pass-by-reference” or “pass-by-value”?

2021-6-3 anglehua

I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation? Java is always pass-by-val...

阅读全文>>

评论(0) 浏览(733)

How do I read / convert an InputStream into a String in Java?

2021-6-3 anglehua

If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is the easiest way to take the InputStream and convert ...

阅读全文>>

评论(0) 浏览(863)

Avoiding NullPointerException in Java

2021-6-3 anglehua

I use object != null a lot to avoid NullPointerException. What is an alternative to: if (someobject != null) { someobject.doCalc(); } This to me sounds like a reasonably common problem that junior to intermediate developers tend to face at some point: they either don't know or don't trust the ...

阅读全文>>

评论(0) 浏览(718)

What are the differences between a HashMap and a Hashtable in Java?

2021-6-3 anglehua

What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded applications? There are several differences between HashMap and Hashtable in Java: Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications...

阅读全文>>

评论(0) 浏览(706)

Create ArrayList from array

2021-6-3 anglehua

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; new ArrayList<>(Arrays.asList(array)); Given: Element[] array =...

阅读全文>>

评论(0) 浏览(696)

Proper use cases for Android UserManager.isUserAGoat()?

2021-6-3 anglehua

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. I was looking at the new APIs introduced in Android...

阅读全文>>

评论(0) 浏览(641)

How do I generate random integers within a specific range in Java?

2021-6-3 anglehua

How do I generate a random int value in a specific range? I have tried the following, but those do not work: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum); Bug: randomNum can be bigger than maximum. Attempt 2: Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nex...

阅读全文>>

评论(0) 浏览(853)

Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

2021-6-3 anglehua

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j)? As alwa...

阅读全文>>

评论(0) 浏览(712)

Why is char[] preferred over String for passwords?

2021-6-3 anglehua

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use String to handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenie...

阅读全文>>

评论(0) 浏览(772)

How do I efficiently iterate over each entry in a Java Map?

2021-6-3 anglehua

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation that I have for the interface? Map<String, Strin...

阅读全文>>

评论(0) 浏览(662)

How can I create a memory leak in Java?

2021-6-3 anglehua

I just had an interview, and I was asked to create a memory leak with Java. Needless to say, I felt pretty dumb having no clue on how to even start creating one. What would an example be? Here's a good way to create a true memory leak (objects inaccessible by running code but still stored in memory...

阅读全文>>

评论(0) 浏览(690)

What is the difference between public, protected, package-private and private in Java?

2021-6-3 anglehua

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance? The official tutorial may be of some use to you. Class Package Subclass(same pkg) Subclas...

阅读全文>>

评论(0) 浏览(673)

When to use LinkedList over ArrayList in Java?

2021-6-3 anglehua

I've always been one to simply use: List<String> names = new ArrayList<>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Summary ArrayList with Arra...

阅读全文>>

评论(0) 浏览(674)

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

2021-6-3 anglehua

How can I convert a String to an int in Java? My String contains only numbers, and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234. String myString = "1234"; int foo = Integer.parseInt(myString); If you look at the Java documenta...

阅读全文>>

评论(0) 浏览(655)

What is a serialVersionUID and why should I use it?

2021-6-3 anglehua

Eclipse issues warnings when a serialVersionUID is missing. The serializable class Foo does not declare a static final serialVersionUID field of type long What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem. The docs fo...

阅读全文>>

评论(0) 浏览(672)

Initialization of an ArrayList in one line

2021-6-3 anglehua

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as follows: ArrayList<String> places = new Ar...

阅读全文>>

评论(0) 浏览(714)

How do I test a private function or a class that has private methods, fields or inner classes?

2021-6-3 anglehua

How do I unit test (using xUnit) a class that has internal private methods, fields or nested classes? Or a function that is made private by having internal linkage (static in C/C++) or is in a private (anonymous) namespace? It seems bad to change the access modifier for a method or function just to ...

阅读全文>>

评论(0) 浏览(705)

Powered by emlog 京ICP备15036472号-3 sitemap