Posts

Showing posts from July, 2014

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Image
 Below is a Stack implementation that uses an internal resizeable array structure.  public class MyStack< T > implements Stack< T > { private static final int CAPACITY = 100 ; private Object[] array ; private int pos = 0 ; public MyStack () { this . array = new Object[ CAPACITY ] ; } @Override public void push ( T item) { if ( pos >= array . length / 2 ) { Object[] newArray = new Object[ pos * 2 ] ; System. arraycopy ( array , 0 , newArray , 0 , array . length ) ; array = newArray ; } array [ pos ++] = item ; } @Override public T pop () { if (isEmpty()) { throw new RuntimeException( "empty stack" ) ; } @SuppressWarnings ( "unchecked" ) T item = ( T ) array [ pos - 1 ] ; pos -= 1 ; return item ; } @Override @SuppressWarnings ( "unchecked" ) public T peek...

Java 8 ile gelen Lambda Expressions özelliğine merhaba diyelim

Lambda ifadeleri C# dilinde yıllardır vardı ve sonunda Java diline de bu özellik eklendi. Peki bu lambda expression özelliği hangi amaçla kullanılıyor? Amacımız şu: Belli bir işi yapma biçimini (fonksiyonaliteyi), bir metoda parametre olarak geçmek istiyoruz. Başka bir deyişle, kodu sanki veriymiş gibi parametre olarak geçmek istiyoruz. Peki eskiden bunu yapamıyormuyduk Java ile? Yapabiliyorduk ama Anonymous class yöntemi ile yapabiliyorduk. Aşağıda vereceğim basit örnekte aradaki farkı görebileceğiz: public class Program {     public static class Person {         private int age;         private String name;         Person(String name, int age) {             this.age = age;             this.name = name;         }         public int getAge() {             return age;   ...

Popular posts from this blog

My Crappy Looking Solution to "Binary Tree Common Ancestor" Problem

Trie Data Structure and Finding Patterns in a Collection of Words

A Graph Application in Java: Using WordNet to Find Outcast Words