Showing posts with label collections. Show all posts
Showing posts with label collections. Show all posts

Monday, 20 February 2012

NavigableMap Example


package com.usr.collections;

import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapExample {
       public static void main(String[] args) {
              NavigableMap<Integer, String> nMap = new TreeMap<Integer, String>();
              nMap.put(1, "sunday");
              nMap.put(2, "monday");
              nMap.put(3, "tuesday");
              nMap.put(4, "wednesday");
              nMap.put(5, "thursday");
              nMap.put(6, "friday");
              nMap.put(7, "saturday");

              System.out.println("data in navigable map:" + nMap);
              // Returns the greatest key strictly less than the given key, or null if
              // there is no such key.
              System.out
                           .println("Retrieving the greatest key strictly less than the given key: "
                                         + nMap.lowerEntry(4));

              // Returns the greatest key strictly less than the given key, or null if
              // there is no such key.
              System.out.println(nMap.lowerKey(4));

              // Returns a key-value mapping associated with the greatest key less
              // than or equal to the given key, or null if there is no such key.
              System.out.println(nMap.floorEntry(3));

              // Returns the greatest key less than or equal to the given key, or null
              // if there is no such key.
              System.out.println(nMap.floorKey(3));

              // Returns a key-value mapping associated with the least key greater
              // than or equal to the given key, or null if there is no such key.
              System.out.println(nMap.ceilingEntry(4));

              // Returns the least key greater than or equal to the given key, or null
              // if there is no such key.
              System.out.println(nMap.ceilingKey(8));

              // Returns a key-value mapping associated with the least key strictly
              // greater than the given key, or null if there is no such key.
              System.out.println(nMap.higherEntry(6));

              // Returns the least key strictly greater than the given key, or null if
              // there is no such key.
              System.out.println(nMap.higherKey(7));

              // Returns a key-value mapping associated with the least key in this
              // map, or null if the map is empty.
              System.out.println("First data:" + nMap.firstEntry());

              // Returns a key-value mapping associated with the greatest key in this
              // map, or null if the map is empty.
              System.out.println("last data:" + nMap.lastEntry());

              // Removes and returns a key-value mapping associated with the least key
              // in this map, or null if the map is empty.
              System.out.println("removing first entry:" + nMap.pollFirstEntry());

              // Removes and returns a key-value mapping associated with the greatest
              // key in this map, or null if the map is empty.
              System.out.println("removing last entry:" + nMap.pollLastEntry());
              // Returns a reverse order view of the mappings contained in this map.
              System.out.println("dispalying data:" + nMap.descendingMap());
              // a reverse order navigable set view of the keys in this map
              System.out.println(nMap.descendingKeySet());
              // Returns a NavigableSet view of the keys contained in this map.
              System.out.println(nMap.navigableKeySet());
              // Returns a view of the portion of this map whose keys range from
              // fromKey to toKey.
              System.out.println(nMap.subMap(2, 5));
              // Returns a view of the portion of this map whose keys are less than
              // (or equal to, if inclusive is true) toKey.
              System.out.println(nMap.headMap(3, true));
              // Returns a view of the portion of this map whose keys are greater than
              // (or equal to, if inclusive is true) fromKey.
              System.out.println(nMap.tailMap(3, false));
       }
}

output
 data in navigable map:{1=sunday, 2=monday, 3=tuesday, 4=wednesday, 5=thursday, 6=friday, 7=saturday}
3=tuesday
3
3=tuesday
3
4=wednesday
null
7=saturday
null
First data:1=sunday
last data:7=saturday
removing first entry:1=sunday
removing last entry:7=saturday
dispalying data:{6=friday, 5=thursday, 4=wednesday, 3=tuesday, 2=monday}
[6, 5, 4, 3, 2]
[2, 3, 4, 5, 6]
{2=monday, 3=tuesday, 4=wednesday}
{2=monday, 3=tuesday}
{4=wednesday, 5=thursday, 6=friday}

Saturday, 18 February 2012

swap list elements example


package com.usr.collections;

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

public class SwapList {
       public static void main(String[] args) {
              List<String> list = new ArrayList<String>();
              addElementsToList(list);
              System.out.println("list elements before swaping:");
              displayList(list);
              System.out.println("......................");
              swapList(list);
              System.out.println("list elements after swaping:");
              displayList(list);
       }

       public static void addElementsToList(List<String> list) {
              list.add("one");
              list.add("two");
              list.add("three");
              list.add("four");
              list.add("five");

       }

       public static void swapList(List<String> list) {
              // public static void swap(List<?> list, int i,int j)
              // Swaps the elements at the specified positions in the specified list.
              // (If the specified positions are equal, invoking this method leaves
              // the list unchanged.)
              Collections.swap(list, 2, 4);

       }

       public static void displayList(List<String> list) {
              for (String temp : list) {
                     System.out.println(temp);
              }

       }

}

output 
list elements before swaping:
one
two
three
four
five
......................
list elements after swaping:
one
two
five
four
three

shuffle list elemnts


package com.usr.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ShuffleList {

       public static void main(String[] args) {
              String[] strArr = { "one", "two", "three", "four", "five", "six" };
              //converting to list
              List<String> strList = Arrays.asList(strArr);
              System.out.println("list elements are:" + strList);
              //public static void shuffle(List<?> list) method shuffles elements of the provided LIst
              Collections.shuffle(strList);
              System.out.println("list elements after shuffling are:" + strList);
       }

}

output 
list elements are:[one, two, three, four, five, six]
list elements after shuffling are:[six, three, five, four, two, one]

get synchronized list from arraylist


package com.usr.collections;

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

public class SynchonizedList {
       public static void main(String[] args) {
              List<String> list = new ArrayList<String>();
              list.add("one");
              list.add("two");
              list.add("three");
              list.add("four");
              list.add("five");
              // static void synchronizedList(List list) method returns a synchronized
              // list from the provided ArrayList
              List<String> synList = Collections.synchronizedList(list);

              System.out.println("sysnchorinzed list elements are:" + synList);
       }
}

output 
sysnchorinzed list elements are:[one, two, three, four, five]

creating unmodifiable list

You can use an unmodifiable wrapper to create a collection that offers read-only access to
others, while allowing read/write access to yourself. You do this simply by giving others a
reference to the unmodifiable wrapper while retaining for yourself a reference to the
original collection.


1.package com.usr.collections;
2.
3.import java.util.Arrays;
4.import java.util.Collections;
5.import java.util.List;
6.
7.public class UnmodifiableList {
8.       public static void main(String[] args) {
9.
10.              String[] strArr = { "sunday", "monday", "tuesday", "wednesday",
                        "thursday", "friday", "saturday" };
11.              List<String> strList = Arrays.asList(strArr);
12.              List<String> unmodifiableList = Collections.unmodifiableList(strList);
13.              // unmodifiableList.add("sunday");
14.              for (String temp : unmodifiableList) {
15.                     System.out.println(temp);
16.              }
17.       }
18.}


In the above program if line 13 is uncommented then we get

Exception in thread "main" java.lang.UnsupportedOperationException
       at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
                                                          at com.usr.collections.UnmodifiableList.main(UnmodifiableList.java:13)

Friday, 17 February 2012

Reverse String Array using collections


package com.usr.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReverseStringArray {

       public static void main(String[] args) {
              String[] strArr = { "sunday", "monday", "tuesday", "wednesday",
                           "thursday", "friday", "saturday" };
              List<String> strList = Arrays.asList(strArr);
              System.out.println("list objets before reverse are:");
              System.out.println(strList);
              System.out.println("...........................");
              Collections.reverse(strList);

              System.out.println("list objects after reverse are:");
              System.out.println("..............................");
              System.out.println(strList);
              // convert to string array
              strArr = strList.toArray(new String[strList.size()]);
              System.out.println("...............................");
              System.out.println("reversed string array elements are:");
              System.out.println(".....................");
              for (String temp : strArr) {
                     System.out.println(temp);
              }

       }

}

OUTPUT
list objets before reverse are:
[sunday, monday, tuesday, wednesday, thursday, friday, saturday]
...........................
list objects after reverse are:
..............................
[saturday, friday, thursday, wednesday, tuesday, monday, sunday]
...............................
reversed string array elements are:
.....................
saturday
friday
thursday
wednesday
tuesday
monday
sunday