Programs to be Implemented in this Experiments
a. Implementation of Simple Array and Vector java Application.
// Java program to Convert Array To Vector
// Using Collections.addAll() method
import java.util.*;
public class array_to_vector {
public static void main(String[] args)
{
String[] arr = { "I", "love", "sun", "and", "moon" };
// create a new vector object of the same type
Vector<String> v = new Vector<String>();
// Use the addAll method of the Collections to add
// all array elements to the vector object
Collections.addAll(v, arr);
System.out.println("The vector is");
// printing vector
System.out.println(v);
}
}
b. Implementation of Array List, Map etc.
// Java Program to Illustrate Duplicate Elements Insertion
// in ArrayList vs HashMap
// Importing utility classes
import java.util.*;
// Main class
class imp {
// Main driver method
public static void main(String args[])
{
// Creating ArrayList of string type
ArrayList<String> list = new ArrayList<String>();
// Adding object in ArrayList
list.add("A");
list.add("B");
// Add duplicates
list.add("A");
list.add("A");
// Invoking ArrayList object
System.out.println("ArrayList: " + list);
// Creating HashMap
HashMap<Integer, String> hm
= new HashMap<Integer, String>();
// Adding object in HashMap
hm.put(1, "A");
hm.put(2, "B");
// Add duplicates key
// Change value if index exist
hm.put(3, "A");
hm.put(3, "A");
// Add duplicates values
// allow duplicates value
hm.put(4, "A");
hm.put(5, "A");
// Invoking HashMap object
System.out.print("HashMap: " + hm);
}
}
c. Implementation of String and String Builder Class etc.
// Java Code to illustrate StringBuilder
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
// Create a StringBuilder object
// using StringBuilder() constructor
StringBuilder str = new StringBuilder();
str.append("GFG");
// print string
System.out.println("String = " + str.toString());
// create a StringBuilder object
// using StringBuilder(CharSequence) constructor
StringBuilder str1
= new StringBuilder("AAAABBBCCCC");
// print string
System.out.println("String1 = " + str1.toString());
// create a StringBuilder object
// using StringBuilder(capacity) constructor
StringBuilder str2 = new StringBuilder(10);
// print string
System.out.println("String2 capacity = "
+ str2.capacity());
// create a StringBuilder object
// using StringBuilder(String) constructor
StringBuilder str3
= new StringBuilder(str1.toString());
// print string
System.out.println("String3 = " + str3.toString());
}
}