Displaying Content of a HashMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

442 Views

Let us first create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To display the content, just print the HashMap object −System.out.println("Map = "+hm);The following is an example to display content of a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); System.out.println("Map = "+hm); } }OutputMap = {Belt=600, Wallet=700}

Remove Single Element from a HashSet in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

1K+ Views

To remove a single element from a HashSet, use the remove() method.First, create a HashSet −HashSet hs = new HashSet();Now, add elements to the HashSet −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");Let us now remove an element −hs.remove("R");The following is an example to remove a single element from a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the hash set hs.add("R"); ... Read More

Recursive Fibonacci Method in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

12K+ Views

The fibonacci series is a series in which each number is the sum of the previous two numbers. The number at a particular position in the fibonacci series can be obtained using a recursive method.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static long fib(long n) {       if ((n == 0) || (n == 1))          return n;       else          return fib(n - 1) + fib(n - 2);    }    public static void main(String[] args) {       System.out.println("The ... Read More

Change File Attribute to Writable in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

594 Views

The file attribute can be changed to writable using the method java.io.File.setWritable(). This method has a single parameter i.e. a boolean value that if true allows the file to be writable and if false disallows the file to be writable. Also, this method returns true if the operation is successful and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          file.setReadOnly();   ... Read More

Remove Value from HashMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

794 Views

Use the remove() method to remove value from HashMap.First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, remove a value, let’s say with key “Wallet” −Object ob = hm.remove("Wallet");The following is an example to remove a value from HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); ... Read More

Check for an Element in a HashSet in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

2K+ Views

To check whether an element is in a HashSet or not in Java, use the contains() method.Create a HashSet −HashSet hs = new HashSet();Add elements to it −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");To check for an element, for example, S here, use the contains() −hs.contains("S")The following is an example to check for an element in a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the HashSet ... Read More

Add Element to Specified Index of ArrayList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

549 Views

An element can be added to the specified index of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the specific index at which to insert the element in the ArrayList and the element itself. If there is an element already present at the index specified by ArrayList.add() then that element and all subsequent elements shift to the right by one.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List ... Read More

What is Polynomial Code?

George John
Updated on 30-Jul-2019 22:30:24

4K+ Views

A polynomial code is a linear code having a set of valid code words that comprises of polynomials divisible by a shorter fixed polynomial is known as generator polynomial.They are used for error detection and correction during the transmission of data as well as storage of data.Types of Polynomial CodesThe types of polynomial codes are:Cyclic Redundancy CodeBose–Chaudhuri–Hocquenghem (BCH) CodesReed–Solomon CodesRepresentation of Bit Strings with PolynomialsThe code words, which are essentially bit strings, are represented by polynomials whose coefficients are either 0 or 1. A 𝑘 – bit word is represented by a polynomial ranging from 𝑥0 to 𝑥𝑘−1. The order ... Read More

Work with this Keyword in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

366 Views

The this keyword in Java is mainly used to refer to the current instance variable of the class. It can also be used to implicitly invoke the method or to invoke the constructor of the current class.A program that demonstrates the this keyword in Java is given as follows:Example Live Democlass Student {    private int rno;    private String name;    public Student(int rno, String name) {       this.rno = rno;       this.name = name;    }    public void display() {       System.out.println("Roll Number: " + rno);       System.out.println("Name: " + ... Read More

Replace All NULL Values in a Specific Field of a Table in MySQL

George John
Updated on 30-Jul-2019 22:30:24

304 Views

To replace all NULL values in a particular field of a particular table, use UPDATE command with IS NULL property. The syntax is as follows:UPDATE yourTableName SET yourColumnName=”yourValue’ WHERE yourColumnName IS NULL;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table Employee_Information_Table    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Salary int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query to insert record is as ... Read More

Advertisements