Code Monkey home page Code Monkey logo

javaweek-4's Introduction

JavaWeek-4

1.Write a Java program to join two array lists.

CODE

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList<String>();
        arr.add("hi");
        arr.add("how");
        arr.add("you");
        arr.add("doin'");
        System.out.println("String 1: "+arr);
        ArrayList<String> arr1=new ArrayList<String>();
        arr1.add("Joey");
        arr1.add("doesn't");
        arr1.add("share");
        arr1.add("food!");
        System.out.println("String 2: "+arr1);
        ArrayList<String> arr2=new ArrayList<String>();
        arr2.addAll(arr);
        arr2.addAll(arr1);
        System.out.println("Joined string: "+arr2);
    }
}

OUTPUT

image

2.Write a Java program to retrieve but does not remove, the first element of a linked list.

CODE

import java.util.LinkedList;
public class Main2
{
    public static void main(String[] args)
    {
        LinkedList <String> arr=new LinkedList<String>();
        arr.add("A");
        arr.add("B");
        arr.add("C");
        arr.add("D");
        arr.add("E");
        String x= arr.peek();
        System.out.println("Original: "+arr);
        System.out.println("First element: "+x);
    }
}

OUTPUT

image

3.Write a Java program to check if a particular element exists in a linked list.

CODE

import java.util.LinkedList;
public class Main3
{
    public static void main(String[] args)
    {
        LinkedList <String> arr=new LinkedList<String>();
        arr.add("A");
        arr.add("B");
        arr.add("C");
        arr.add("D");
        arr.add("E");
        System.out.println("Original: "+arr);
        if(arr.contains("A")){
            System.out.println("A - The element exists");
        }
        else{
            System.out.println("A - The element does not exists");
        }
        if(arr.contains("I")){
            System.out.println("I - The element exists");
        }
        else{
            System.out.println("I - The element does not exists");
        }
    }
}

OUTPUT

image

4.Write a Java program to remove all of the elements from a hash set.

CODE

import java.util.HashSet;
public class Main4
{
    public static void main(String[] args)
    {
        HashSet<String> arr=new HashSet<>();
        arr.add("A");
        arr.add("B");
        arr.add("C");
        arr.add("D");
        arr.add("E");
        System.out.println("Original: "+arr);
        arr.clear();
        System.out.println("Removed all the elements from the list: "+arr);
    }
}

OUTPUT

image

5.Write a Java program to compare two hash set.

CODE

import java.util.HashSet;
public class Main5 {
    public static void main(String[] args) {
        HashSet<String> arr=new HashSet<>();
        arr.add("A");
        arr.add("B");
        arr.add("C");
        arr.add("D");
        arr.add("E");
        System.out.println("Set 1: "+arr);
        HashSet<String> arr1=new HashSet<>();
        arr1.add("A");
        arr1.add("L");
        arr1.add("C");
        arr1.add("J");
        arr1.add("E");
        System.out.println("Set 2: "+arr1);
        boolean isEquals = arr.equals(arr1);
        System.out.println("By comparing two set is set1 and set2 equal? : "+isEquals);
    }
}

OUTPUT

image

6.Write a Java program to retrieve and remove the last element of a tree set.

CODE

import java.util.TreeSet;
public class Main7 {
    public static void main(String[] args) {
        TreeSet<String> arr = new TreeSet<>();
        arr.add("A");
        arr.add("B");
        arr.add("C");
        arr.add("D");
        arr.add("E");
        System.out.println("Original:" + arr);
        System.out.println("Removing the last element: "+arr.pollLast());
        System.out.println("Tree set after removing last element:"+arr);
    }
}

OUTPUT

image

7.Write a Java program to convert a Priority Queue elements to a string representation.

CODE

import java.util.PriorityQueue;
public class Main8 {
    public static void main(String[] args) {
        PriorityQueue<String> arr = new PriorityQueue<String>();
        arr.add("Apple");
        arr.add("Bomb");
        arr.add("Cat");
        arr.add("Door");
        arr.add("Eat");
        arr.add("Fan");
        System.out.println("Original:"+arr);
        String x = arr.toString();
        System.out.println("String representation: "+x);

    }
}

OUTPUT

image

8.Write a Java program to get a collection view of the values contained in this map.

CODE

import java.util.HashMap;
public class Main9 {
        public static void main(String args[]){

            HashMap<Integer,String> arr= new HashMap<Integer,String>();
            arr.put(1,"A");
            arr.put(2,"B");
            arr.put(3,"C");
            arr.put(4,"D");
            arr.put(5,"E");
            System.out.println("Collection view: "+ arr.values());
        }
}

OUTPUT

image

9.Write a Java program to get a set view of the keys contained in this map

CODE

import java.util.HashMap;
import java.util.Set;
public class Main10 {
    public static void main(String args[]){

        HashMap<Integer,String> arr= new HashMap<Integer,String>();
        arr.put(1,"A");
        arr.put(2,"B");
        arr.put(3,"C");
        arr.put(4,"D");
        arr.put(5,"E");
        Set x = arr.keySet();
        System.out.println("set view of the keys contained in this map: "+x);

    }
}

OUTPUT

image

10.Write a Java program to get a key-value mapping associated with the least key greater than or equal to the given key. Return null if there is no such key.

CODE

import java.util.*;
public class Main11 {
    public static void main(String args[]) {
        TreeMap < Integer, String > arr = new TreeMap < Integer, String > ();
        arr.put(1,"A");
        arr.put(2,"B");
        arr.put(3,"C");
        arr.put(4,"D");
        arr.put(5,"E");
        System.out.println("Original: " + arr);
        System.out.println("Keys greater than or equal to 1: " + arr.ceilingEntry(1));
        System.out.println("Keys greater than or equal to 3: " + arr.ceilingEntry(3));
        System.out.println("Keys greater than or equal to 6: " + arr.ceilingEntry(6));
    }
}

OUTPUT

image

11.Write a Java program to get the least key greater than or equal to the given key. Returns null if there is no such key.

CODE

import java.util.*;
public class Main12 {
    public static void main(String args[]) {
        TreeMap < Integer, String > arr = new TreeMap < Integer, String > ();
        arr.put(1,"A");
        arr.put(2,"B");
        arr.put(3,"C");
        arr.put(4,"D");
        arr.put(5,"E");
        System.out.println("Original: " + arr);
        System.out.println("Keys greater than or equal to 1: " + arr.ceilingKey(1));
        System.out.println("Keys greater than or equal to 3: " + arr.ceilingKey(3));
        System.out.println("Keys greater than or equal to 6: " + arr.ceilingKey(6));
    }
}

OUTPUT

image

javaweek-4's People

Contributors

keerthikanagarajan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.