Interface as a type in Java?

Let’s declare two interfaces and a class that implements them both: interface I1 { } interface I2 { } class C implements I1, I2 { } objects can have multiple types In the following code, it can be seen that a C instance has the type of C as well as I1 and I2: C … Read more

java.nio.file.WatchEvent gives me only relative path. How can I get the absolute path of the modified file?

You need to get the parent directory from the WatchKey to resolve the full path WatchKey key; WatchEvent<Path> event; Path dir = (Path)key.watchable(); Path fullPath = dir.resolve(event.context()); This piece of code reads like it needs accompanying documentation to be grasped, it makes little sense on its own. What were their intentions with this particular API … Read more

What is an efficient algorithm to find whether a singly linked list is circular/cyclic or not? [duplicate]

The standard answer is to take two iterators at the beginning, increment the first one once, and the second one twice. Check to see if they point to the same object. Then repeat until the one that is incrementing twice either hits the first one or reaches the end. This algorithm finds any circular link … Read more

IllegalAnnotationException: Two classes have the same XML type name

I found the cause of my problem. This problem occurs because JAX-WS generates a class for each method and the class name is constructed by concatenating methodName + “Response”. In my case, the newly generated class by JAX-WS will have the same name as my response object. Example: @Stateless @WebService() public class AccountWS { @WebMethod() … Read more

Why Java does not see that Integers are equal?

Check out this article: Boxed values and equality When comparing wrapper types such as Integers, Longs or Booleans using == or !=, you’re comparing them as references, not as values. If two variables point at different objects, they will not == each other, even if the objects represent the same value. Example: Comparing different Integer … Read more

Java getting my IP address

String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + ” ” + ip); } } } catch (SocketException e) { throw new … Read more

Convert two dimensional array to List in java?

This is a nice way of doing it for any two-dimensional array, assuming you want them in the following order: [[array[0]-elems], [array[1]elems]…] public <T> List<T> twoDArrayToList(T[][] twoDArray) { List<T> list = new ArrayList<T>(); for (T[] array : twoDArray) { list.addAll(Arrays.asList(array)); } return list; }

Java String Split by “|”

You must use: String [] temp = s.split(“\\|”); This is because the split method takes a regular expression, and | is one of the special characters. It means ‘or’. That means you are splitting by ” or ”, which is just ”. Therefore it will split between every character. You need two slashes because the … Read more