|
|
Program: Basic LinkedHashMap Operations.
Description: |
Here you can find example code for basic LinkedHashMap operation. It shows how to
create object for LinkedHashMap, adding elements, getting size, checking empty or not, remove, etc.
|
Code: |
package com.java2novice.linkedhashmap;
import java.util.LinkedHashMap;
public class BasicLinkedHashMap {
public static void main(String a[]){
LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
lhm.put("one", "This is first element");
lhm.put("two", "This is second element");
lhm.put("four", "this element inserted at 3rd position");
System.out.println(lhm);
System.out.println("Getting value for key 'one': "+lhm.get("one"));
System.out.println("Size of the map: "+lhm.size());
System.out.println("Is map empty? "+lhm.isEmpty());
System.out.println("Contains key 'two'? "+lhm.containsKey("two"));
System.out.println("Contains value 'This is first element'? "
+lhm.containsValue("This is first element"));
System.out.println("delete element 'one': "+lhm.remove("one"));
System.out.println(lhm);
}
}
|
|
Output: |
{one=This is first element, two=This is second element, four=this element inserted at 3rd position}
Getting value for key 'one': This is first element
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value 'This is first element'? true
delete element 'one': This is first element
{two=This is second element, four=this element inserted at 3rd position}
|
|
|
|
|
List Of All LinkedHashMap Sample Programs:- LinkedHashMap basic operations
- How to iterate through LinkedHashMap?
- How to check whether the value exists or not in a LinkedHashMap?
- How to delete all entries from LinkedHashMap object?
- How to eliminate duplicate user defined objects as a key from LinkedHashMap?
- How to find user defined objects as a key from LinkedHashMap?
- How to delete user defined objects as a key from LinkedHashMap?
|
|
|
String Vs StringBuffer
We know that String is immutable object. We can not change the value
of a String object once it is initiated. If we try to change the value of
the existing String object then it creates new object rather than changing
the value of the existing object. So incase, we are going to do more
modificatios on String, then use StringBuffer. StringBuffer updates the
existing objects value, rather creating new object.
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek
|