Friday, 18 March 2016

Different types of FOR loops in Java

package pack1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Forloops {

public static void main(String a[]){
String[] m = {"a","b","c"};
//Type 1

/*for(int i=0;i< m.length;i++){
System.out.println(m[i]);
}*/

//Type 2
/*
for (String n: m){
System.out.println(n);
}*/

//Type 3

/*for(int i=0;i<5;i++){
System.out.printf("%d the value is", i);
}*/

//Type 4 - with collections

/*List<String> x = new ArrayList<String>();
x.add("item 1");
x.add("item 2");
x.add("item 3");
for(String z:x){
System.out.println(z);
}*/

//Type 5 - Collections using iterator

ArrayList al = new ArrayList();
     // add elements to the array list
     al.add("C");
     al.add("A");
     al.add("E");
     al.add("B");
     al.add("D");
     al.add("F");

     // Use iterator to display contents of al
     System.out.print("Original contents of al: ");
     Iterator itr = al.iterator();
     while(itr.hasNext()) {
        Object element = itr.next();
        System.out.print(element + " ");
     }
}

}


User Defined Object/class/dataType and Iteration through FOR loop:

/** User defined class/dataType**/

package pack1;

import java.util.ArrayList;
import java.util.List;

class Person {
public int personId;
public String personName;

public int getPersonId() {
    return personId;
}

public void setPersonId(int personId) {
    this.personId = personId;
}

public String getPersonName() {
    return personName;
}

public void setPersonName(String personName) {
    this.personName = personName;
}}

/** Main Class *****************************************/
public class ArrayLoop {

public static void main(String a[]){
Person p = new Person();   /** setting values for user class **/
p.setPersonId(1);
p.setPersonName("Prabhu");
List<Object> x = new ArrayList<Object> (); /** Initialising a List to add User defined clas*//
x.add(p);    //** Adding User class to List **/
for (Object ob: x){  //** Iterating the list to get user class object **//
System.out.println(ob.getClass());
Person m = (Person)ob;   /** Type casting to user class **//
System.out.println(m.personId);
System.out.println(m.personName);
}
}

}



Output:

class pack1.Person
1
Prabhu

Tuesday, 15 September 2015

Java CollectionsTutorial

What is Java Collections?

The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures.

Almost all collections in Java are derived from the java.util.Collection interface.


The Collection interface is a subinterface of java.lang.Iterable, so any Collection may be the target of a for-each statement.

Java Collection simply means a single unit of objects

Interfaces and Classes Distribution:


The JCF consist of the following types of Interfaces and Classes.




List

A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index.

ArrayList

  • Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Declaration:

ArrayList<String> al  =  new ArrayList<String>();//creating new generic arraylist


Examples:

http://www.javatpoint.com/ArrayList-in-collection-framework


HashMap


HashMap is a Hash table based implementation of the Map interface .

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls

It neither does any kind of sorting to the stored keys and Values.

HashMap maintains key and value pairs and often denoted as HashMap<Key, Value> or HashMap<K, V>.


Example:

package com.java2novice.hashmap;
import java.util.HashMap;
public class MyBasicHashMap {
    public static void main(String a[]){
        HashMap<String, String> hm = new HashMap<String, String>();
        //add key-value pair to hashmap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        //getting value for the given key from hashmap
        System.out.println("Value of second: "+hm.get("second"));
        System.out.println("Is HashMap empty? "+hm.isEmpty());
        hm.remove("third");
        System.out.println(hm);
        System.out.println("Size of the HashMap: "+hm.size());
    }
}


Output:
{second=SECOND INSERTED, third=THIRD INSERTED, first=FIRST INSERTED}
Value of second: SECOND INSERTED
Is HashMap empty? false
{second=SECOND INSERTED, first=FIRST INSERTED}
Size of the HashMap: 2


Thanks to : http://www.java2novice.com/

An excellent tutorial is available on  http://www.java2novice.com/java-collections-and-util/hashmap/basic-operations/

Wednesday, 3 June 2015

Hands on with the Android M

The Android M developer preview launched at Google I/O this year, and while it's clearly in a rough state and aimed at developers, we figured we should catalog whatever differences we could find between it and Android 5.0. The overall UI isn't much different from Lollipop, but Google has used M to tackle a lot of user requests.

We've covered most of the big additions recently, and checked out the in-the-works multi-window functionality in a separate post, but there's a few new things that didn't show up in the official announcements.

The weirdest part of M is the new app drawer, which puts big index letters on the left side of the screen. It reduces the amount of space for icons, and generally looks ugly. We're not fans of the letters. The switch from horizontal pages back to vertical scrolling is a nice improvement, this makes it easy to fling your way to the bottom of a long app list, instead of having to swipe a thousand times. Vertical scrolling is on the widget drawer too, which would often grow to 10+ pages in Lollipop.

Lollipop's "Heads up notifications," which popped up over top of the screen, lacked user controls, but M allows users to set them on a per-app basis, which will make a lot of people happy. Before it was up to developers to decide if they were important or not, and of course, every developer though they were important. It's also now easy to control Android's three different volume sliders (Notifications, media, and alarms), thanks to a new drop down that shows them all at once.

We also ran into a few intriguing options that we can't use yet. There's setup for what looks like Brillo IoT devices, a mysterious "Inactive apps" screen that doesn't seem to do anything, and a new "Tap & Pay" screen that looks like the beginnings of Android Pay.


Source: http://arstechnica.com/gadgets/2015/05/hands-on-with-the-android-m-developer-preview/