java-5
Mockito isA(Class clazz) How to resolve type safety?
Mockito/Hamcrest and generic classes Yes, this is a general problem with Mockito/Hamcrest. Generally using isA() with generic classes produces a warning. There are predifined Mockito matchers for the most common generic classes: anyList(), anyMap(), anySet() and anyCollection(). Suggestions: anyIterable() in Mockito 2.1.0 Mockito 2.1.0 added a new anyIterable() method for matching Iterables: when(client.runTask(anyString(), anyString(), anyIterable()).thenReturn(…) … Read more
What is the easiest way to iterate over all the key/value pairs of a java.util.Map in Java 5 and higher?
Assuming K is your key type and V is your value type: for (Map.Entry<K,V> entry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); // do stuff }
Reasons and advantages for upgrading to Java 6 for a non-technical decider (at the client)
Java 5 is now well past its end-of-life date. Sun/Oracle will no longer issue public updates to it. Java SE 5.0 is in its Java Technology End of Life (EOL) transition period. The EOL transition period began April 8th, 2007 and will complete October 8th, 2009, when Java SE 5.0 will have reached its End … Read more
Java Generics: Generic type defined as return type only
The method returns a type of whatever you expect it to be (<X> is defined in the method and is absolutely unbounded). This is very, very dangerous as no provision is made that the return type actually matches the returned value. The only advantage this has is that you don’t have to cast the return … Read more
How to redirect verbose garbage collection output to a file?
From the output of java -X: -Xloggc:<file> log GC status to a file with time stamps Documented here: -Xloggc:filename Sets the file to which verbose GC events information should be redirected for logging. The information written to this file is similar to the output of -verbose:gc with the time elapsed since the first GC event … Read more
Java reading a file into an ArrayList?
This Java code reads in each word and puts it into the ArrayList: Scanner s = new Scanner(new File(“filepath”)); ArrayList<String> list = new ArrayList<String>(); while (s.hasNext()){ list.add(s.next()); } s.close(); Use s.hasNextLine() and s.nextLine() if you want to read in line by line instead of word by word.
How to set a JVM TimeZone Properly
You can pass the JVM this param -Duser.timezone For example -Duser.timezone=Europe/Sofia and this should do the trick. Setting the environment variable TZ also does the trick on Linux.
Why do people still use primitive types in Java?
In Joshua Bloch’s Effective Java, Item 5: “Avoid creating unnecessary objects”, he posts the following code example: public static void main(String[] args) { Long sum = 0L; // uses Long, not long for (long i = 0; i <= Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } and it takes 43 seconds to run. … Read more
How do I join two lists in Java?
In Java 8: List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()) .collect(Collectors.toList()); Java 16+: List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).toList();