java-7

Efficiently converting java string to its equivalent enum

元气小坏坏 提交于 2019-12-02 09:48:18
Given a string i want to get the enum equivalent of it in constant time. I have a enum defined like the one shown in the question. Best way to create enum of strings? public enum Strings { STRING_ONE("ONE"), STRING_TWO("TWO") ; private final String text; /** * @param text */ private Strings(final String text) { this.text = text; } /* (non-Javadoc) * @see java.lang.Enum#toString() */ @Override public String toString() { return text; } } If i now get a string (say "TWO"), is there a way to see if it exists and if it exists then is there a way i can define a method that will return the enum value

SocketChannel read() behaviour - short reads

橙三吉。 提交于 2019-12-02 09:27:18
The ServerSocketChannel is used this way: ServerSocketChannel srv = ServerSocketChannel.open(); srv.socket().bind(new java.net.InetSocketAddress(8112)); SocketChannel client = srv.accept(); When a connection is received, data is read this way: ByteBuffer data = ByteBuffer.allocate(2000); data.order(ByteOrder.LITTLE_ENDIAN); client.read(data); logger.debug("Position: {} bytes read!", data.position()); It prints: Position: 16 bytes read! Why isn't the SocketChannel blocking until the buffer is filled? From the ServerSocketChannel.accept() API (Java 7): The socket channel returned by this method,

Scala to Java (functional programming)

眉间皱痕 提交于 2019-12-02 09:22:45
I have been asked to 'translate' some Scala code to Java for a course. However, the requirements of the assignment are that Java 8 and external libraries, such as Functional Java and Totally Lazy , are not allowed . The line in Scala is: charges.groupBy(_.cc).values.map(_.reduce(_ combine _)).toList I have been able to write groupBy and values but .map and _.reduce still elude me. I have looked at the source code of those two libraries as well as the Scala source to try and find something to help me with putting these together but I have not been able to make any headway. GroupBy is

Cannot find main class on Linux - Classpath issue

假装没事ソ 提交于 2019-12-02 09:21:52
I am having some trouble running a few jar's on a linux box. Basically, I am getting an error saying it cannot find the main class of my main jar. The class is defenetly present so it must be a classpath issue. I am not great with linux, so I am looking for some advice as to where I might be missing something. First off, I am setting the classpath in the users bash_profile; adding all the jar's required, seperated by a : delimeter. I then export the classpath. Then, in the shell (ksh) script I use to invoke the main jar, I also st the classpath and call it in the command using -cp so it looks

Extract some content from jar-file downloaded with JWS

余生长醉 提交于 2019-12-02 07:52:19
im trying to extract some files from a jar-file downloaded using java-webstart. below code was used to locate the jar and initiate the FileSystem 1 final ProtectionDomain domain = this.getClass().getProtectionDomain(); 2 final CodeSource source = domain.getCodeSource(); 3 final URL url = source.getLocation(); 4 final URI uri = url.toURI(); 5 Path jarPath = Paths.get(uri); 6 7 FileSystem fs = FileSystems.newFileSystem(jarPath, null); This works fine when the jar-file is on a local disc, but fails at line 5 in the JWS scenario, because the logs says: url=http://localhost:8080/myjarfile.jar java

JVM crashes on Lucene DataInput.readVInt

懵懂的女人 提交于 2019-12-02 07:51:14
问题 My JVM (1.6.0_29) keeps crashing on intensive use when indexing documents with Lucene. I get: # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00002b6b196d767c, pid=26417, tid=1183217984 # # JRE version: 6.0_29-b11 # Java VM: Java HotSpot(TM) 64-Bit Server VM (20.4-b02 mixed mode linux-amd64 compressed oops) # Problematic frame: # J org.apache.lucene.store.DataInput.readVInt()I # # If you would like to submit a bug report, please visit: # http:/

Java, Collection constructor

陌路散爱 提交于 2019-12-02 07:46:33
问题 is there any difference between : TreeMap<String, String> myMap = new TreeMap<>(); and TreeMap<String, String> myMap = new TreeMap<String,String>(); Thanks! 回答1: They are the same in java 7 where the diamond operator <> was introduced. In older versions of java the diamond operator will not work. The diamond operator brings type inference to constructors. Type inference on generic methods is available in java 5 and higher. Prior to java 7, to create a generic class using the compiler's type

Java 7, color of button text when using HTML formatted labels

不打扰是莪最后的温柔 提交于 2019-12-02 04:47:21
I have a custom UI for certain buttons, implemented by subclassing MetalButtonUI. The buttons use HTML-formatted labels. This is a requirement for me, I need to support multiline button labels. For some reason, when my application runs on Java 7 (scientifically update 4, the most current) the text color when the button is disabled is now grey. This does not happen when running on Java 4 or 6. In the HTML for the button label, I can set the font color by using <font color=..> However this value is ignored when the button is disabled. It seems like somewhere, my font color is overridden when the

Use String hashCode() Method? [closed]

与世无争的帅哥 提交于 2019-12-02 04:30:35
From link : http://www.tutorialspoint.com/java/java_string_hashcode.htm Relationship between hashCode and equals method in Java Good hashCode() Implementation But i cant understand about the hashcode . Here's an example: public class StringDemo { public static void main(String args[]){ String strob1="first string"; System.out.println(strob1.hashCode()); } } This simple program give me output: -5468287 Can anyone tell me : How it give me output: -5468287 ? String's hash code is computed as: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the i -th character of

Java, Collection constructor

老子叫甜甜 提交于 2019-12-02 04:06:29
is there any difference between : TreeMap<String, String> myMap = new TreeMap<>(); and TreeMap<String, String> myMap = new TreeMap<String,String>(); Thanks! They are the same in java 7 where the diamond operator <> was introduced. In older versions of java the diamond operator will not work. The diamond operator brings type inference to constructors. Type inference on generic methods is available in java 5 and higher. Prior to java 7, to create a generic class using the compiler's type inference you had to use generic factory methods like static <K,T> Map<K,T> createMap() . First one will only