0% found this document useful (0 votes)
10 views3 pages

JVL Exp6

The document outlines Java programs for implementing arrays, vectors, array lists, and hash maps. It demonstrates converting an array to a vector, handling duplicate elements in an array list versus a hash map, and using the StringBuilder class. Each section includes code examples to illustrate the concepts discussed.

Uploaded by

Arfia Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

JVL Exp6

The document outlines Java programs for implementing arrays, vectors, array lists, and hash maps. It demonstrates converting an array to a vector, handling duplicate elements in an array list versus a hash map, and using the StringBuilder class. Each section includes code examples to illustrate the concepts discussed.

Uploaded by

Arfia Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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());
}
}

You might also like