What is the best way to filter a Java Collection?
2021-6-3 anglehua
I want to filter a java.util.Collection based on a predicate. Java 8 (2014) solves this problem using streams and lambdas in one line of code: List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList()); Here's a tutorial. Use Collection#...Using context in a fragment
2021-6-3 anglehua
How can I get the context in a fragment? I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don't work so what can I do? Database constructor public Database(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(con...How to convert a byte array to a hex string in Java?
2021-6-3 anglehua
I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in the form of: 3a5f771c From the discussion here, and especially this answer, this is the function I currently use: private sta...How to fix the Hibernate “object references an unsaved transient instance - save the transient instance before flushing” error
2021-6-3 anglehua
I receive following error when I save the object using Hibernate object references an unsaved transient instance - save the transient instance before flushing You should include cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping. This happens...How to upload files to server using JSP/Servlet?
2021-6-3 anglehua
How can I upload files to server using JSP/Servlet? I tried this: <form action="upload" method="post"> <input type="text" name="description" /> <input type="file" name="file" /> <input type="submit" /> </form> However, I only get the file name, not the file...No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
2021-6-3 anglehua
I'm compiling a project in Eclipse using m2eclipse. I set the JDK path in Eclipse like this: Windows-->preferences-->installed jres--> jdk1.7.xx path But this is showing an error [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] No co...What is the equivalent of the C++ Pair<L,R> in Java?
2021-6-3 anglehua
Is there a good reason why there is no Pair<L,R> in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own. It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry<K,V>), but this looks quite convoluted. In a thread on comp...How to check internet access on Android? InetAddress never times out
2021-6-3 anglehua
I got a AsyncTask that is supposed to check the network access to a host name. But the doInBackground() is never timed out. Anyone have a clue? public class HostAvailabilityTask extends AsyncTask<String, Void, Boolean> { private Main main; public HostAvailabilityTask(Main main) { ...IntelliJ: Never use wildcard imports
2021-6-3 anglehua
Is there a way to tell IntelliJ never to use wildcard imports? Under 'Settings > Code Style > Imports', I can see that you can specify the 'class count' prior to IntelliJ using wildcard imports. However, if I never want to use wildcard imports can I turn this functionality off? I have tried...What is the difference between == and equals() in Java?
2021-6-3 anglehua
I wanted to clarify if I understand this correctly: == is a reference comparison, i.e. both objects point to the same memory location .equals() evaluates to the comparison of values in the objects In general, the answer to your question is "yes", but... .equals(...) will only compare what it is...What's the difference between JPA and Hibernate?
2021-6-3 anglehua
Closed. This question is opinion-based. It is not currently accepting answers. Closed 3 years ago. Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accep...How to update a value, given a key in a hashmap?
2021-6-3 anglehua
Suppose we have a HashMap<String, Integer> in Java. How do I update (increment) the integer-value of the string-key for each existence of the string I find? One could remove and reenter the pair, but overhead would be a concern. Another way would be to just put the new pair and the old one wou...How to add local .jar file dependency to build.gradle file?
2021-6-3 anglehua
So I have tried to add my local .jar file dependency to my build.gradle file: apply plugin: 'java' sourceSets { main { java { srcDir 'src/model' } } } dependencies { runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar') runtime fileTree(dir: 'libs...Sending Email in Android using JavaMail API without using the default/built-in app
2021-6-3 anglehua
I am trying to create a mail sending application in Android. If I use: Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); This will launch the built-in Android application; I'm trying to send the mail on button click directly without using this application. Send e-mail in Andro...Examples of GoF Design Patterns in Java's core libraries
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. I am learning GoF Java Design Patterns and I want to see some real life examples of them. What a...Set ImageView width and height programmatically?
2021-6-3 anglehua
How can I set an ImageView's width and height programmatically? It may be too late but for the sake of others who have the same problem, to set the height of the ImageView: imageView.getLayoutParams().height = 20; Important. If you're setting the height after the layout has already been 'laid out'...Simple way to repeat a string
2021-6-3 anglehua
I'm looking for a simple commons method or operator that allows me to repeat some string n times. I know I could write this using a for loop, but I wish to avoid for loops whenever necessary and a simple direct method should exist somewhere. String str = "abc"; String repeated = str.repeat(3); repe...Easiest way to convert a List to a Set in Java
2021-6-3 anglehua
What is the easiest way to convert a List to a Set in Java? Set<Foo> foo = new HashSet<Foo>(myList); I agree with sepp2k, but there are some other details that might matter: new HashSet<Foo>(myList); will give you an unsorted set which doesn't have duplicates. In this case, dup...Can an abstract class have a constructor?
2021-6-3 anglehua
Can an abstract class have a constructor? If so, how can it be used and for what purposes? Yes, an abstract class can have a constructor. Consider this: abstract class Product { int multiplyBy; public Product( int multiplyBy ) { this.multiplyBy = multiplyBy; } public int m...How to get the last value of an ArrayList
2021-6-3 anglehua
How can I get the last value of an ArrayList? I don't know the last index of the ArrayList. The following is part of the List interface (which ArrayList implements): E e = list.get(list.size() - 1); E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find ...