How do I get an IntStream from a List?

I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); } since the function Integer::intValue is fully compatible with ToIntFunction since it takes an Integer and it returns an int. No autoboxing is performed. I was also looking for an equivalent of Function::identity, … Read more

Limit size of Queue in .NET?

I’ve knocked up a basic version of what I’m looking for, it’s not perfect but it’ll do the job until something better comes along. public class LimitedQueue<T> : Queue<T> { public int Limit { get; set; } public LimitedQueue(int limit) : base(limit) { Limit = limit; } public new void Enqueue(T item) { while (Count … Read more

Collection to Iterable

A Collection is an Iterable. So you can write: public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add(“a string”); Iterable<String> iterable = list; for (String s : iterable) { System.out.println(s); } }

How to create Immutable List in java?

Once your beanList has been initialized, you can do beanList = Collections.unmodifiableList(beanList); to make it unmodifiable. (See Immutable vs Unmodifiable collection) If you have both internal methods that should be able to modify the list, and public methods that should not allow modification, I’d suggest you do // public facing method where clients should not … Read more

Filter the elements of a map based on a subset of its keys without iterating through the entire thing

Just do: map.keySet().retainAll(set); As per the javadoc, the changes in the key set are reflected back in the map. … The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. … Here’s a demo: var map = new HashMap<String, String>(); map.put(“1”, “one”); map.put(“2”, “two”); map.put(“3”, “three”); … Read more

java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source)

Easy work around is just to pass in the List into an ArrayList‘s constructor. For example: String valuesInArray[]={“1″,”2″,”3″,”4”}; List modifiableList = new ArrayList(Arrays.asList(valuesInArray)); System.out.println(modifiableList.remove(“1″) + ” remove flag”); System.out.println(” collcetion “+ modifiableList); Response: true remove flag collcetion [2, 3, 4]

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

You can change the behaviour of ArrayList List<MyType> list = new ArrayList<MyType>() { public boolean add(MyType mt) { super.add(mt); Collections.sort(list, comparator); return true; } }; Note: a PriorityQueue is NOT a List, if you didn’t care what type of collection it was, the simplest would be to use a TreeSet, which is just like a … Read more

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