Browsing 7239 questions and answers with Jon Skeet

Can hashcodes of short string be same?

I have short Strings (less than 10 characters). I will convert it into int and use it as primary key. (I can't use String primary key because of small problems.) I know the...
Jon Skeet
people
quotationmark

A hash code is 32 bits in size. A char in Java is 16 bits in size. So in theory, all 2-character strings could have different hash codes, although some of those hash codes would have to collide with the hash code of the empty string and... more 9/12/2014 1:07:46 AM

people

Set empty string as the value of a key in properties file

jdbc.password= How can I assign jdbc.password key in my application.properties file an empty string? I understand I can do this programmatically as follows, but I would like...
Jon Skeet
people
quotationmark

Just leaving the value empty on the RHS should be fine: password= Sample code: import java.io.*; import java.util.*; class Test{ public static void main(String [] args) throws Exception { Properties props = new... more 9/12/2014 12:43:36 AM

people

What is the best way to trim a list?

I have a List of strings. Its being generated elsewhere but i will generate it below to help describe this simplified example var list = new...
Jon Skeet
people
quotationmark

I would just write it without any LINQ, to be honest- after all, you're modifying a collection rather than just querying it: void TrimList(List<string> list) { int lastNonEmpty = list.FindLastIndex(x =>... more 9/12/2014 12:32:52 AM

people

Convert Java Byte Array to a UTF 8 Python String

I'm pulling JSON data through a REST API using Requests in Python. Unfortunately, one of the fields contains all sorts of unescaped and control characters that breaks the...
Jon Skeet
people
quotationmark

You're currently printing out the result of calling toString() on the byte[]. That's never a good idea - arrays don't override toString(). You should use the new String(byte[], Charset) constructor: String text = new String(bytes,... more 9/12/2014 12:22:45 AM

people

Meaning of this in specific situation

I'm currently reading about the this keyword and don't understand why is it useful to do things like: this.object = object; (object is a random variable. I just don't...
Jon Skeet
people
quotationmark

It's necessary to specify that you want to assign the value to the field, rather than the parameter or local variable: public void setFoo(Foo foo) { this.foo = foo; ^ ^ | \--- Take the value of the... more 9/11/2014 11:58:24 PM

people

How to convert (Java) files with different encodings to the same?

I'm working on a big java web application in Eclipse, whose files have different encodings: some are in UTF-8, others in Cp1252, yet others are in ISO-8859-1 (with no distinction...
Jon Skeet
people
quotationmark

It's very easy to write code to convert encodings - although I'd expect there are tools to do it anyway. Simply: Create one FileInputStream to the existing file, and wrap it in an InputStreamReader with the appropriate encoding Create... more 9/11/2014 9:14:51 PM

people

How Do I Create an Expression<Func<>> with Type Parameters from a Type Variable

I'd like to write a statement like the following: Expression<Func<AClass, bool>> filter = x => true; Except instead of AClass, I'd like to use a Type variable...
Jon Skeet
people
quotationmark

You need to create the appropriate delegate type, then pass that to the Expression.Lambda method. For example: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; class Test { static void... more 9/11/2014 5:56:45 PM

people

Converting String symbol into char

I am implementing an monoalphabetic substitution algorithm. Keys and values ​​are stored in the HashMap, and as a parameter of the input string using a variable of StringBuffer...
Jon Skeet
people
quotationmark

You can just use charAt(0) to get the first character of a string... just like you're already doing for your key. Note that if you use a parameterized Map.Entry instead of the raw type, you won't need the toString call either: for... more 9/11/2014 4:51:32 PM

people

Shortest way to write immutable struct in C#

Quite often I come across the need for small immutable data structures. Others would probably use Tuples in these cases, but I really dislike that Tuples don't read nicely and...
Jon Skeet
people
quotationmark

As of C# 6, you can write fairly compact struct initializers: public struct Key { public string Name { get; } public int Rating { get; } public Key(string name, int rating) { this.Name = name; this.Rating... more 9/11/2014 12:20:28 PM

people

Why Does This Queryable.Where Call Change the Queryable's Type Parameter?

Code to Reproduce Issue I ran into a situation where an IQueryable.Where<TSource> call was returning an IQueryable<TOther> where TOther != TSource. I put together...
Jon Skeet
people
quotationmark

Why does this happen? Because the Where call is receiving a type argument of ParentQueryElement as TSource. It creates a result based on TSource as a new object... so you end up with something which "knows" about ParentQueryElement... more 9/11/2014 3:52:12 AM

people