Collection
Programs:
1.
How to convert an array into a collection?
Scanner s=new Scanner(System. in);
System.out.println("How many elements you want ");
int n=s.nextInt();;
String[] name = new String[n];
for(int i = 0; i < n; i++){
name[i] = s.next();
}
List list = Arrays.asList(name);
System.out.println(list);
for(Object li: list){
String str =(String) li;
System.out.print(str + " ");
|
2.
How to convert a collection into an array?
List list = new ArrayList();
list.add("This ");
list.add("is ");
list.add("a ");
list.add("good ");
list.add("program.");
String[] s1 = (String[])list.toArray(new
String[0]);
for(int i=0; i<s1.length; ++i){
String contents = s1[i];
System.out.print(contents)
|
Outpuit: - This is a good program.
3.
How to compare elements in a
collection?
Following example compares the
element of a collection by converting a string into a treeset using Collection.min() and Collection.max() methods of Collection
class
String[] str = {
"Penny", "nickel", "Ram", "Quarter",
"Dollar" };
Set set = new
TreeSet();
for (int i=0;
i<str.length;i++)
set.add(str[i]);
System.out.println(set);
System.out.println(Collections.min(set));
System.out.println(Collections.min(set,
String.CASE_INSENSITIVE_ORDER));
System.out.println(Collections.max(set));
System.out.println(Collections.max(set,
String.CASE_INSENSITIVE_ORDER));
|
4.
How to print a collection?
System.out.println("Tree Map Example!\n");
TreeMap tMap = new
TreeMap();
tMap.put(1,
"Sunday");
tMap.put(2,
"Monday");
tMap.put(3,
"Tuesday");
tMap.put(4,
"Wednesday");
tMap.put(5,
"Thursday");
tMap.put(6,
"Friday");
tMap.put(7,
"Saturday");
System.out.println("Keys of tree map:
" + tMap.keySet());
System.out.println("Values of tree map:
" + tMap.values());
System.out.println("Key: 5 value:
" + tMap.get(5)+ "\n");
System.out.println("First key: " +
tMap.firstKey()
+ " Value: " +
tMap.get(tMap.firstKey()) +
"\n");
System.out.println("Last key: " +
tMap.lastKey()
+ " Value: "+
tMap.get(tMap.lastKey()) + "\n");
System.out.println("Removing first
data: "
+
tMap.remove(tMap.firstKey()));
System.out.println("Now the tree map
Keys: " + tMap.keySet());
System.out.println("Now the tree map
contain: " + tMap.values() +
"\n");
System.out.println("Removing last data:
" + tMap.remove(tMap.lastKey()));
System.out.println("Now the tree map
Keys: " + tMap.keySet());
System.out.println("Now the tree map
contain: " + tMap.values());
|
Output:-
Tree Map Example!
Keys of tree map: [1, 2, 3, 4, 5, 6, 7]
Values of tree map: [Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday]
Key: 5 value: Thursday
First key: 1 Value: Sunday
Last key: 7 Value: Saturday
Removing first data: Sunday
Now the tree map Keys: [2, 3, 4, 5, 6, 7]
Now the tree map contain: [Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday]
Removing last data: Saturday
Now the tree map Keys: [2, 3, 4, 5, 6]
Now the tree map contain: [Monday, Tuesday, Wednesday,
Thursday, Friday]
5.
How to make a collection read-only?
Ans:
List stuff = Arrays.asList(new String[] { "a",
"b" });
List
list = new ArrayList(stuff);
list = Collections.unmodifiableList(list);
try {
list.set(0, "new value");
}
catch (UnsupportedOperationException e) {
}
Set
set = new HashSet(stuff);
set = Collections.unmodifiableSet(set);
Map
map = new HashMap();
map = Collections.unmodifiableMap(map);
System.out.println("Collection is
read-only now.");
|
6.
How to remove a specific
element from a collection?
Collection.remove(str2);
7.
How to reverse a collection?
String[]
coins = { "A", "B", "C", "D",
"E" };
List l
= new ArrayList();
Collections.reverse(l);
liter = l.listIterator();
System.out.println("After
reversal");
while (liter.hasNext())
System.out.println(liter.next());
|
8.
How to shuffle the elements
of a collection?
String[] alpha = { "subbu", "venkat",
"barat",
"madhav",
"malat", "rajay" };
List list = new ArrayList();
for(int i=0;i<alpha.length;i++)
{
list.add(alpha[i]);
}
Collections.shuffle(list);
System.out.println(list);
|
9.
How to get the size of a collection?
String str1 = "Yellow", str2 = "White", str3 =
"Green", str4 = "Blue";
Iterator iterator;
collection.add (str1);
collection.add (str2);
collection.add (str3);
collection.add (str4);
int size = collection.size(); //Output:-:- 4 |
10.
How to iterate through
elements of HashMap?
NOTE:
Here values() method returns values in reverse oreder.
Observe
output of the program
|
HashMap< String, String> hMap =
new HashMap< String, String>(); hMap.put("1", "1st");
hMap.put("2", "2nd");
hMap.put("3", "3rd");
Collection cl = hMap.values();
Iterator itr = cl.iterator();
while (itr.hasNext()) {
System.out.println(itr.next()); } |
Ans: 3rd
2nd
1st
11.
How to use different types of Collections?
12.
How to use enumeration to display contents of HashTable?
v Enumaration Interface having 2 methods:
1. hasMoreElements()
2. nextElement()
v By using elements() method we got the Enemaration Object.
Hashtable ht = new Hashtable();
ht.put("1", "One");
ht.put("2", "Two");
ht.put("3", "Three");
Enumeration e = ht.elements();
While (e.hasMoreElements()){
System.out.println(e.nextElement());
|
Output:-: Three
Two
One
13.
How to get Set view of Keys
from Java Hashtable?
v Using keys()
method to get Enumeration of Keys of the Hashtable.
Enumeration e = ht.keys();
|
14. How to find min & max of a List?
List list = Arrays.asList("one Two
three Four five six
one three Four".split (" "));
System.out.println(list);
System.out.println("max: " +
Collections. max(list));
System.out.println("min:
" + Collections.min(list));
|
15.
How to find a sublist in a List?
Using indexOfSubList() &
lastIndexOfSubList() to check whether the sublist is there in the list or not
& to find the last occurance of the sublist in the list
List list = Arrays.asList("one Two three Four five
six one three Four".split(" "));
System.out.println("List :"+list);
List sublist = Arrays.asList("three Four".split(" ")); System.out.println("SubList :"+sublist);
System.out.println("indexOfSubList: "
+ Collections.indexOfSubList(list, sublist));
System.out.println("lastIndexOfSubList: "
+ Collections.lastIndexOfSubList(list, sublist));
|
Output: List :[one, Two, three, Four, five, six, one, three, Four]
SubList :[three, Four]
indexOfSubList: 2
lastIndexOfSubList: 7
16. How to replace an element in a list?
Using replaceAll() method to replace all the occurance of an element with a different element in a list.
List list = Arrays.asList("one Two three Four five six
one three Four".split(" "));
System.out.println("List :"+list);
Collections.replaceAll(list, "one", "hundread");
System.out.println("replaceAll: " + list);
|
Output:
List: [one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundred, Two, three, Four, five, six, hundred, three, Four]
17. How to rotate elements of the List?
List list = Arrays.asList("one Two three Four five six”. split(" "));
System.out.println("List :"+list);
Collections.rotate(list, 3);
System.out.println("rotate: " + list);
|
Following example uses rotate() method to rotate elements of the
list depending on the 2nd argument of the method.
Output:-
List :[one, Two, three, Four, five, six]
rotate: [Four, five, six, one, Two, three]
No comments:
Post a Comment