What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

2021-6-3 anglehua

I have a Map which is to be modified by several threads concurrently. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap From what I understand, Hashtable is an old implementation (extending the obsolete ...

阅读全文>>

评论(0) 浏览(176)

Is it possible to use Java 8 for Android development?

2021-6-3 anglehua

Searching the web, it is not clear if Java 8 is supported for Android development or not. Before I download/setup Java 8, can some one point me at any "official" documentation that says Java 8 is or is not supported for Android development. java 8 Android supports all Java 7 language features and a...

阅读全文>>

评论(0) 浏览(200)

Ways to iterate over a list in Java

2021-6-3 anglehua

Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each. Given a List<E> list object, I know of the follo...

阅读全文>>

评论(0) 浏览(221)

Why can't static methods be abstract in Java?

2021-6-3 anglehua

The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? } The abstract annotation to a method indicates that the method MUST be overriden in a sub...

阅读全文>>

评论(0) 浏览(150)

What is the point of “final class” in Java?

2021-6-3 anglehua

I am reading a book about Java and it says that you can declare the whole class as final. I cannot think of anything where I'd use this. I am just new to programming and I am wondering if programmers actually use this on their programs. If they do, when do they use it so I can understand it better ...

阅读全文>>

评论(0) 浏览(191)

What issues should be considered when overriding equals and hashCode in Java?

2021-6-3 anglehua

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. What issues / pitfalls must be considered when overriding equals and hashCode? The theory (for ...

阅读全文>>

评论(0) 浏览(189)

Update Eclipse with Android development tools v. 23

2021-6-3 anglehua

I updated Eclipse with the new SDK tools (rev. 23), but now when Eclipse starts I receive the error: This Android SDK requires Android Developer Toolkit version 23.0.0 or above. Current version is 22.6.3.v201404151837-1123206. Please update ADT to the latest version. No updates were found with "Ch...

阅读全文>>

评论(0) 浏览(183)

How can I convert byte size into a human-readable format in Java?

2021-6-3 anglehua

How can I convert byte size into a human-readable format in Java? Like 1024 should become "1 Kb" and 1024*1024 should become "1 Mb". I am kind of sick of writing this utility method for each project. Is there a static method in Apache Commons for this? Fun fact: The original snippet posted here wa...

阅读全文>>

评论(0) 浏览(162)

What are the best use cases for Akka framework

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

阅读全文>>

评论(0) 浏览(184)

Difference between FetchType LAZY and EAGER in Java Persistence API?

2021-6-3 anglehua

I am a newbie to Java Persistence API and Hibernate. What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API? Sometimes you have two entities and there's a relationship between them. For example, you might have an entity called University and another entity calle...

阅读全文>>

评论(0) 浏览(189)

How can I increment a date by one day in Java?

2021-6-3 anglehua

I'm working with a date in this format: yyyy-mm-dd. How can I increment this date by one day? Something like this should do the trick: String dt = "2008-01-01"; // Start date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(dt)); c...

阅读全文>>

评论(0) 浏览(172)

Can we instantiate an abstract class?

2021-6-3 anglehua

During one of my interview, I was asked "If we can instantiate an abstract class?" My reply was "No. we can't". But, interviewer told me "Wrong, we can." I argued a bit on this. Then he told me to try this myself at home. abstract class my { public void mymethod() { System.out.print("A...

阅读全文>>

评论(0) 浏览(203)

Why there is no ConcurrentHashSet against ConcurrentHashMap

2021-6-3 anglehua

HashSet is based on HashMap. If we look at HashSet<E> implementation, everything is been managed under HashMap<E,Object>. <E> is used as a key of HashMap. And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in Java. Based on this, I am confused that w...

阅读全文>>

评论(0) 浏览(160)

Create instance of generic type in Java?

2021-6-3 anglehua

Is it possible to create an instance of a generic type in Java? I'm thinking based on what I've seen that the answer is no (due to type erasure), but I'd be interested if anyone can see something I'm missing: class SomeContainer<E> { E createContents() { return what??? } } ...

阅读全文>>

评论(0) 浏览(171)

Get an OutputStream into a String

2021-6-3 anglehua

What's the best way to pipe the output from an java.io.OutputStream to a String in Java? Say I have the method: writeToStream(Object o, OutputStream out) Which writes certain data from the object to the given stream. However, I want to get this output into a String as easily as possible. I'm cons...

阅读全文>>

评论(0) 浏览(169)

How to get the path of a running JAR file?

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. My code runs inside a JAR file, say foo.jar, and I ...

阅读全文>>

评论(0) 浏览(172)

What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

2021-6-3 anglehua

What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java? And when do I use each one? Consider these filenames: C:\temp\file.txt - This is a path, an absolute path, and a canonical path. .\file.txt - This is a path. It's neither an absolute path nor a canonical pat...

阅读全文>>

评论(0) 浏览(178)

Uncatchable ChuckNorrisException

2021-6-3 anglehua

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable? Thoughts that came to mind are using for example interceptors or aspect-oriented programming. I haven't tried this, so I don't know if the JVM would restrict something li...

阅读全文>>

评论(0) 浏览(186)

How to convert Milliseconds to “X mins, x seconds” in Java?

2021-6-3 anglehua

I want to record the time using System.currentTimeMillis() when a user begins something in my program. When he finishes, I will subtract the current System.currentTimeMillis() from the start variable, and I want to show them the time elapsed using a human readable format such as "XX hours, XX mins, ...

阅读全文>>

评论(0) 浏览(179)

Clearing coverage highlighting in Eclipse

2021-6-3 anglehua

After running coverage reports in Eclipse (using cobertura or an EMMA plugin), my source code files get highlighted in green, red and yellow depending on which lines of code were covered by tests. How can I clear this highlighting after I'm done? Click the "Remove all Sessions" button in the toolba...

阅读全文>>

评论(0) 浏览(224)

Powered by emlog 京ICP备15036472号-3 sitemap