Java/Collections Data Structure/LinkedHashMap
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Check if a particular element exists in Java LinkedHashSet
- 2 Check if a particular key exists in Java LinkedHashMap example
- 3 Check if a particular value exists in Java LinkedHashMap example
- 4 Copy all elements of Java LinkedHashSet to an Object Array
- 5 Get Set view of Keys from Java LinkedHashMap
- 6 Get Size of Java LinkedHashMap
- 7 Implementing a Least-Recently-Used (LRU) Cache
- 8 Iterate through the values of Java LinkedHashMap
- 9 Remove all values from Java LinkedHashMap
- 10 Remove value from Java LinkedHashMap
- 11 Simple Java LinkedHashMap
Check if a particular element exists in Java LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println(lhashSet.contains(new Integer("3")));
}
}
Check if a particular key exists in Java LinkedHashMap example
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
boolean blnExists = lHashMap.containsKey("3");
System.out.println(blnExists);
}
}
Check if a particular value exists in Java LinkedHashMap example
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
boolean blnExists = lHashMap.containsValue("Two");
System.out.println(blnExists);
}
}
Copy all elements of Java LinkedHashSet to an Object Array
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
Object[] objArray = lhashSet.toArray();
for (Object inte: objArray){
System.out.println(inte);
}
}
}
Get Set view of Keys from Java LinkedHashMap
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
Set st = lHashMap.keySet();
Iterator itr = st.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
st.remove("2");
boolean blnExists = lHashMap.containsKey("2");
System.out.println(blnExists);
}
}
/*
1
2
3
false
*/
Get Size of Java LinkedHashMap
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
System.out.println(lHashMap.size());
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
System.out.println(lHashMap.size());
Object obj = lHashMap.remove("2");
System.out.println(lHashMap.size());
}
}
Implementing a Least-Recently-Used (LRU) Cache
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] argv) throws Exception {
final int MAX_ENTRIES = 100;
Map cache = new LinkedHashMap(MAX_ENTRIES + 1, .75F, true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_ENTRIES;
}
};
Object key = "key";
Object value = "value";
cache.put(key, value);
Object o = cache.get(key);
if (o == null && !cache.containsKey(key)) {
}
cache = (Map) Collections.synchronizedMap(cache);
}
}
Iterate through the values of Java LinkedHashMap
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
Collection c = lHashMap.values();
Iterator itr = c.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
}
}
Remove all values from Java LinkedHashMap
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
lHashMap.clear();
System.out.println(lHashMap.size());
}
}
Remove value from Java LinkedHashMap
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
lHashMap.put("1", "One");
lHashMap.put("2", "Two");
lHashMap.put("3", "Three");
Object obj = lHashMap.remove("2");
System.out.println(obj + " was Removed");
}
}
Simple Java LinkedHashMap
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String,Integer> lHashMap = new LinkedHashMap<String,Integer>();
lHashMap.put("One", new Integer(1));
lHashMap.put("Two", new Integer(2));
// retrieve value using Object get(Object key) method of Java LinkedHashMap class
Object obj = lHashMap.get("One");
System.out.println(obj);
}
}