Skip to main content

Posts

Showing posts with the label code

Using Twitter search API in Java

If you are reading this, I assume you are aware about Twitter APIs. If you are new to this API, I suggest you read the  API documentation  (which is quite good BTW!) This simple program of mine uses Twitter search API to get "positive" tweets for a search string. It aims to retrieve all the search results (tweets) and hence has to make multiple calls to REST API, for which it uses max_id (explained later). Let me give an overview of all the request parameters that we have specified in the URL (Twitter API URL): q=%23SearchString: #SearchString count=100: Number of tweets you want the API to return in one call. include_entities=false: Exclude details of entities in the JSON response. You can set it to true if you want. max_id: Since we want more than 100 tweets, we are invoking the API multiple times and giving the id of last tweet returned in previous invocation as 'max_id' and asking Twitter to give 100 tweets prior to this tweet.  e.g. First call ret...

How does HashMap work in Java? (HashMap internals)

HashMaps are probably one of the most used and definitely one of the least understood Java classes. In this post let's look at the source code of HashMap.java to understand how is data inserted and retrieved from a HashMap. We know that HashMaps work on the principle of 'hashing' and put(key, value) is used to store a key and corresponding value and get(key) is used to retrieve the value for given key. Below image shows how HashMap stores the data. The blue squares represent the 'bucket' or indexes in the array, determined on the basis of hash value for each key . (We will see later, exactly how the hash value is calculated) At each index of the array, is stored a linked list which has nodes of the type 'Map.Entry'. Each node stores the key/value pair. Why is the linked list needed? Because two unequal objects can have same hash value (read more about equals and hashcode here ). Remember that a HashMap will store values as long as different keys ar...

Java Design Patterns : Adapter Pattern

Whenever and wherever you will read about Adapter pattern you will come across example of electric adapters. Be it a 3-pin to 2-pin adapter, round pin to flat pin adapter or AC to DC adapter, its purpose is to act as an intermediary between electrical equipment and the socket. Above is a representation of a 3-pin to 2-pin adapter which exposes a 3-pin socket which takes in a 3 pin plug and gives a 2 pin output. (These 2-pins can be plugged into any 2-pin socket.)  Note that the adapter contains a 3-pin plug inside and behavior of its 3 pins is altered to suit the behavior of the adapter pins which will fit in the 2-pin socket. Now if we interpolate this in terms of Java concepts, the 3-pin socket is an interface and every 3-pin plug implements this interface. Let's call this as 'adaptee' interface. Similarly every 2-pin socket is also an interface which is implemented by every 2-pin plug. Let's call the 2-pin socket (which our adapter is trying to be...

Java Design Patterns: Decorator Pattern

Before talking about Decorator design pattern, think of decorators in real life. Let's consider a very common  example. In an ice cream parlor you can order ice creams in Vanilla, Butter Scotch, Chocolate and Pineapple flavors with or without an assortment of toppings. Here are your choices: Chocolate sauce Strawberry sauce Maple Syrup Fruits and nuts or just a Cherry You can even do a mix and match of toppings according to your liking. So you can have a scoop (or two) of vanilla ice cream with Strawberry syrup topped with fruits and nuts and a cherry.  Each of these toppings is a 'Decorator'. As evident from name itself, Decorators in real life 'decorate' an existing object. They ADD something to an existing object. Decorator design pattern also solves the same problem. If you think of implementing this system, your first instinct would be to create a base class Icecream and make Vanilla , ButterScotch , Chocolate and Pineapple extend it. Co...