What's the difference between map() and flatMap() methods in Java 8?

2021-6-3 anglehua

In Java 8, what's the difference between Stream.map() and Stream.flatMap() methods? Both map and flatMap can be applied to a Stream<T> and they both return a Stream<R>. The difference is that the map operation produces one output value for each input value, whereas the flatMap operation...

阅读全文>>

评论(0) 浏览(195)

Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?

2021-6-3 anglehua

I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy - Animal (Parent) Dog - Cat (Children) So suppose I have a method doSomething(List<Animal> animals). By all the rules of inheritance and polymorphism, I would assume that a List<Dog&...

阅读全文>>

评论(0) 浏览(203)

How do I discover memory usage of my application in Android?

2021-6-3 anglehua

How can I find the memory used on my Android application, programmatically? I hope there is a way to do it. Plus, how do I get the free memory of the phone too? Note that memory usage on modern operating systems like Linux is an extremely complicated and difficult to understand area. In fact the c...

阅读全文>>

评论(0) 浏览(191)

How to initialize HashSet values by construction?

2021-6-3 anglehua

I need to create a Set with initial values. Set<String> h = new HashSet<String>(); h.add("a"); h.add("b"); Is there a way to do this in one line of code? For instance, it's useful for a final static field. There is a shorthand that I use that is not very time efficient, but fits on a s...

阅读全文>>

评论(0) 浏览(190)

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

2021-6-3 anglehua

When creating a new Java project in IntelliJ IDEA, the following directories and files are created: ./projectname.iml ./projectname.ipr ./projectname.iws ./src/ I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in ...

阅读全文>>

评论(0) 浏览(190)

What is PECS (Producer Extends Consumer Super)?

2021-6-3 anglehua

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super? tl;dr: "PECS" is from the collection's point of view. If you are only pulling items from a generic collection, ...

阅读全文>>

评论(0) 浏览(179)

Scanner is skipping nextLine() after using next() or nextFoo()?

2021-6-3 anglehua

I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println("Enter numerical value"); int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st string"); String string1 = input.nextLine(); // Rea...

阅读全文>>

评论(0) 浏览(217)

Jackson with JSON: Unrecognized field, not marked as ignorable

2021-6-3 anglehua

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON: {"wrapper":[{"id":"13","name":"Fred"}]} Here is a simplified use case: private void tryReading() { String jso...

阅读全文>>

评论(0) 浏览(209)

Where is Java Installed on Mac OS X?

2021-6-3 anglehua

I just downloaded Java 7u17 on Mac OS 10.7.5 from here and then successfully installed it. In order to do some JNI programming, I need to know where Java installed on my Mac. I thought that inside the /Library/Java/JavaVirtualMachines/ folder, there would be a folder called 1.7.0.jdk or something, ...

阅读全文>>

评论(0) 浏览(188)

How to convert an Array to a Set in Java

2021-6-3 anglehua

I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like: java.util.Arrays.asList(Object[] a); Any ideas? Like this: Set<T> mySet = new HashSet<>(Arrays.asList(someArray)); I...

阅读全文>>

评论(0) 浏览(205)

Java URL encoding of query string parameters

2021-6-3 anglehua

Say I have a URL http://example.com/query?q= and I have a query entered by the user such as: random word £500 bank $ I want the result to be a properly encoded URL: http://example.com/query?q=random%20word%20%A3500%20bank%20%24 What's the best way to achieve this? I tried URLEncoder and creatin...

阅读全文>>

评论(0) 浏览(245)

What is the equivalent of Java static methods in Kotlin?

2021-6-3 anglehua

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin? You place the function in the "companion object". So the java code like this: class Foo { public static int a() { return 1; } } will become class Foo { companion object { fun a() : Int ...

阅读全文>>

评论(0) 浏览(200)

How do I get a class instance of generic type T?

2021-6-3 anglehua

I have a generics class, Foo<T>. In a method of Foo, I want to get the class instance of type T, but I just can't call T.class. What is the preferred way to get around it using T.class? The short answer is, that there is no way to find out the runtime type of generic type parameters in Java. ...

阅读全文>>

评论(0) 浏览(269)

Dealing with “Xerces hell” in Java/Maven?

2021-6-3 anglehua

In my office, the mere mention of the word Xerces is enough to incite murderous rage from developers. A cursory glance at the other Xerces questions on SO seem to indicate that almost all Maven users are "touched" by this problem at some point. Unfortunately, understanding the problem requires a bi...

阅读全文>>

评论(0) 浏览(285)

Making a mocked method return an argument that was passed to it

2021-6-3 anglehua

Consider a method signature like: public String myFunction(String abc); Can Mockito help return the same string that the method received? You can create an Answer in Mockito. Let's assume, we have an interface named Application with a method myFunction. public interface Application { public Stri...

阅读全文>>

评论(0) 浏览(199)

How do I convert from int to String?

2021-6-3 anglehua

I'm working on a project where all conversions from int to String are done like this: int i = 5; String strI = "" + i; I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose? Normal ways would be Integer.toString(i) or String.valueOf(i). The concatenation will work...

阅读全文>>

评论(0) 浏览(174)

Implements vs extends: When to use? What's the difference?

2021-6-3 anglehua

Please explain in an easy to understand language or a link to some article. extends is for extending a class. implements is for implementing an interface The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the clas...

阅读全文>>

评论(0) 浏览(184)

Differences between Oracle JDK and OpenJDK

2021-6-3 anglehua

NOTE: This question is from 2014. As of Java 11 OpenJDK and Oracle JDK are converging. Are there any crucial differences between Oracle and OpenJDK? For example, are the garbage collection and other JVM parameters the same? Does GC work differently between the two? Both OpenJDK and Oracle JDK ...

阅读全文>>

评论(0) 浏览(220)

Can I catch multiple Java exceptions in the same catch clause?

2021-6-3 anglehua

In Java, I want to do something like this: try { ... } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at the same time */) { someCode(); } ...instead of: try { ... } catch (IllegalArgumentExcept...

阅读全文>>

评论(0) 浏览(207)

How to tell Jackson to ignore a field during serialization if its value is null?

2021-6-3 anglehua

How can Jackson be configured to ignore a field value during serialization if that field's value is null. For example: public class SomeClass { // what jackson annotation causes jackson to skip over this value if it is null but will // serialize it otherwise private String someValue; } ...

阅读全文>>

评论(0) 浏览(205)

Powered by emlog 京ICP备15036472号-3 sitemap