3:13 pm - Thursday December 17, 1531

EnumMap to find Elements are Equals using Equals() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This program is used to find the elements are equals using equals() method.

Example Program


package com.example;

import java.util.*;

public class EnumMapEquals { // create an enum
	public enum Numbers {
		ONE, TWO, THREE, FOUR, FIVE
	};

	public static void main(String[] args) {
		EnumMap<Numbers, String> map1 = new EnumMap<Numbers, String>(
				Numbers.class);
		EnumMap<Numbers, String> map2 = new EnumMap<Numbers, String>(
				Numbers.class);
		// associate values in map1
		map1.put(Numbers.ONE, "1");
		map1.put(Numbers.TWO, "2");
		map1.put(Numbers.THREE, "3");
		map1.put(Numbers.FOUR, "4");
		// associate values in map2
		map2.put(Numbers.ONE, "1");
		map2.put(Numbers.TWO, "2");
		map2.put(Numbers.THREE, "3");
		map2.put(Numbers.FOUR, "4");
		// print the maps
		System.out.println("map1:" + map1);
		System.out.println("map2:" + map2); // check for equality between maps
		boolean equal = map1.equals(map2); // print the result
		System.out.println("Map1 and map 2 are equal:" + equal);// add one more
																// value in map2
		map2.put(Numbers.FIVE, "5"); // check for equality between maps
		equal = map1.equals(map2); // print the result
		System.out.println("Map1 and map 2 are equal:" + equal);
	}
}

 

Output:

map1:{ONE=1, TWO=2, THREE=3, FOUR=4}
map2:{ONE=1, TWO=2, THREE=3, FOUR=4}
Map1 and map 2 are equal:true
Map1 and map 2 are equal:false

Description

The java.util.EnumMap.equals(Object o) method compares the specified object with this map for equality.Both objects must be maps.

Declaration

Following is the declaration for java.util.EnumMap.equals() method

public boolean equals(Object o)

Parameters

  • o — the object to be compared for equality with this map.

Return Value

This method returns true if the specified object is equal to this map

Exception

  • NA

BACK

Filed in: Java Examples

Comments are closed.