HashSet vs. ArrayList

When its comes to the behavior of ArrayList and HashSet they are completely different classes. ArrayList ArrayList Does not validate duplicates. get() is O(1) contains() is O(n) but you have fully control over the order of the entries. get add contains next remove(0) iterator.remove ArrayList O(1) O(1) O(n) O(1) O(1) O(1) Not thread safe and to make it thread safe you have to use Collections.synchronizedList(…) HashSet … Read more

Converting Hexadecimal String to Decimal Integer

It looks like there’s an extra space character in your string. You can use trim() to remove leading and trailing whitespaces: Or if you think the presence of a space means there’s something else wrong, you’ll have to look into it yourself, since we don’t have the rest of your code.

How to sort Map values by key in Java?

Short answer Use a TreeMap. This is precisely what it’s for. If this map is passed to you and you cannot determine the type, then you can do the following: This will iterate across the map in natural order of the keys. Longer answer Technically, you can use anything that implements SortedMap, but except for rare cases … Read more

Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

Connection timeouts (assuming a local network and several client machines) typically result from a) some kind of firewall on the way that simply eats the packets without telling the sender things like “No Route to host” b) packet loss due to wrong network configuration or line overload c) too many requests overloading the server d) … Read more

Sort a single String in Java

toCharArray followed by Arrays.sort followed by a String constructor call: EDIT: As tackline points out, this will fail if the string contains surrogate pairs or indeed composite characters (accent + e as separate chars) etc. At that point it gets a lot harder… hopefully you don’t need this 🙂 In addition, this is just ordering by ordinal, without … Read more

“cannot find symbol: method” but the method is declared

This is because although you have an object of SavingsAccount you are using refrence variable of type Account so you can access only those methods that are there in Account class. And you don’t have calculateBalance() method in your Account class. That’s why you are not able to access it and compiler complains that it cannot find a method named calculateBalance as it sees that reference type … Read more

Declaring an unsigned int in Java

Java does not have a datatype for unsigned integers. You can define a long instead of an int if you need to store large values. You can also use a signed integer as if it were unsigned. The benefit of two’s complement representation is that most operations (such as addition, subtraction, multiplication, and left shift) are identical on a binary level for … Read more

DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704

I created local database in DB2 called “TestDB” then I created table called “TestTable“.I found that the table is put under schema name is “yasmin“.I am trying to connect to the DB2 database using JDBC but I got this exception I tried many solutions on the internet Like set schema but unfortunately doesn’t work. This is the JDBC code … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)