get string value from HashMap depending on key name

Just use Map#get(key) ? Object value = map.get(myCode); Here’s a tutorial about maps, you may find it useful: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html. Edit: you edited your question with the following: I’m expecting to see a String, such as “ABC” or “DEF” as that is what I put in there initially, but if I do a System.out.println() I get … Read more

Returning ‘IList’ vs ‘ICollection’ vs ‘Collection’

ICollection<T> is an interface that exposes collection semantics such as Add(), Remove(), and Count. Collection<T> is a concrete implementation of the ICollection<T> interface. IList<T> is essentially an ICollection<T> with random order-based access. In this case you should decide whether or not your results require list semantics such as order based indexing (then use IList<T>) or … Read more

Difference between Arrays.asList(array) and new ArrayList(Arrays.asList(array))

First, let’s see what this does: Arrays.asList(ia) It takes an array ia and creates a wrapper that implements List<Integer>, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if … Read more

Truncate a list to a given number of elements

Use List.subList: import java.util.*; import static java.lang.Math.min; public class T { public static void main( String args[] ) { List<String> items = Arrays.asList(“1”); List<String> subItems = items.subList(0, min(items.size(), 2)); // Output: [1] System.out.println( subItems ); items = Arrays.asList(“1”, “2”, “3”); subItems = items.subList(0, min(items.size(), 2)); // Output: [1, 2] System.out.println( subItems ); } } You … Read more

ReadOnlyCollection or IEnumerable for exposing member collections?

More modern solution Unless you need the internal collection to be mutable, you could use the System.Collections.Immutable package, change your field type to be an immutable collection, and then expose that directly – assuming Foo itself is immutable, of course. Updated answer to address the question more directly Is there any reason to expose an … Read more

Difference between Java Enumeration and Iterator

Looking at the Java API Specification for the Iterator interface, there is an explanation of the differences between Enumeration: Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved. The bottom line is, both Enumeration and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)