Nice articles I've read recently on Java which were given by "Joshua Bloch" and few other famous coders & scientist here for reference -
Must read & Enjoy them!!!
~-Scribbles by Sivananda Hanumanthu
My experiences and learnings on Technology, Leadership, Domains, Life and on various topics as a reference!
What you can expect here, it could be something on Java, J2EE, Databases, or altogether on a newer Programming language, Software Engineering Best Practices, Software Architecture, SOA, REST, Web Services, Micro Services, APIs, Technical Architecture, Design, Programming, Cloud, Application Security, Artificial Intelligence, Machine Learning, Big data and Analytics, Integrations, Middleware, Continuous Delivery, DevOps, Cyber Security, Application Security, QA/QE, Automations, Emerging Technologies, B2B, B2C, ERP, SCM, PLM, FinTech, IoT, RegTech or any other domain, Tips & Traps, News, Books, Life experiences, Notes, latest trends and many more...
Write an efficient function to find the first nonrepeated character in a string. For instance, the first nonrepeated character in “total” is ‘o’ and the first nonrepeated character in “teeter” is ‘r’. Discuss the efficiency of your algorithm. |
Tip | The algorithm described can be improved somewhat by comparing each character with only the characters following it because it has already been compared with the characters preceding it. This would give you a total of (n – 1) + (n – 2)+ … + 1 comparisons. As discussed, this is still O(n2). |
Tip | You can convert a character to an integer in order to use it as an index. If the strings are restricted to ASCII characters, this gives you 128 different possible character values. Unicode characters as used in Java or C#, however, have 65,536 possible values. |
First, build the character count hash table:
For each character
If no value is stored for the character, store 1
Otherwise, increment the value
Second, scan the string:
For each character
Return character if count in hash table is 1
If no characters have count 1, return null
public static Character firstNonRepeated( String str ){
Hashtable charHash = new Hashtable();
int i, length;
Character c;
Integer intgr;
length = str.length();
// Scan str, building hash table
for (i = 0; i < length; i++) {
c = new Character(str.charAt(i));
intgr = (Integer) charHash.get(c);
if (intgr == null) {
charHash.put(c, new Integer(1));
} else {
// Increment count corresponding to c
charHash.put(c, new Integer(intgr.intValue() + 1));
}
}
// Search hashtable in order of str
for (i = 0; i < length; i++) {
c = new Character(str.charAt(i));
if (((Integer)charHash.get(c)).intValue() == 1)
return c;
}
return null;
}
public static Character firstNonRepeated( String str ){
Hashtable charHash = new Hashtable();
int i, length;
Character c;
Object seenOnce = new Object();
Object seenTwice = new Object();
length = str.length();
// Scan str, building hash table
for( i = 0; i < length; i++ ){
c = new Character(str.charAt(i));
Object o = charHash.get(c);
if( o == null ){
charHash.put( c, seenOnce );
} else if( o == seenOnce ){
// Increment count corresponding to c
charHash.put( c, seenTwice );
}
}
// Search hashtable in order of str
for( i = 0; i < length; i++ ){
c = new Character(str.charAt(i));
if( charHash.get(c) == seenOnce ){
return c;
}
}
return null;
}
class Foo {
public static void method() {
System.out.println("in Foo");
}
}
class Bar extends Foo {
public static void method() {
System.out.println("in Bar");
}
}
This compiles and runs just fine. Isn't it an example of a static method overriding another static method? The answer is no - it's an example of a static method hiding another static method. If you try to override a static method, the compiler doesn't actually stop you - it just doesn't do what you think it does.
class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
If you run this, the output isinstanceMethod() in Bar classMethod() in FooWhy do we get instanceMethod from Bar, but classMethod() from Foo? Aren't we using the same instance f to access both of these? Yes we are - but since one is overriding and the other is hiding, we see different behavior. Since instanceMethod() is (drum roll please...) an instance method, in which Bar overrides the method from Foo, at run time the JVM uses the actual class of the instance f to determine which method to run. Although f was declared as a Foo, the actual instance we created was a new Bar(). So at runtime, the JVM finds that f is a Bar instance, and so it calls instanceMethod() in Bar rather than the one in Foo. That's how Java normally works for instance methods.
f.classMethod();where f is an instance of some class, and classMethod() is a class method (i.e. a static method) of that class. This is legal, but it's a bad idea because it creates confusion. The actual instance f is not really important here. Only the declared type of f matters. That is, what class is f declared to be? Since classMethod() is static, the class of f (as determined by the compiler at compile time) is all we need.
f.classMethod();It would be better coding style to write either:
Foo.classMethod();or
Bar.classMethod();That way, it is crystal clear which class method you would like to call. It is also clear that the method you are calling is indeed a class method. o;'oi;'i Barring that, you could always come up with this monstrosity:
f.getClass().getMethod("classMethod", new Class[]).invoke(null, new Object[]);But all this could be avoided by simply not trying to override your static (class) methods. :-)