Questions tagged [java-collections-api]

41 questions
32
votes
8 answers

Sort an ArrayList by primitive boolean type

I want to sort my ArrayList using a boolean type. Basically i want to show entries with true first. Here is my code below: Abc.java public class Abc { int id; bool isClickable; Abc(int i, boolean isCl){ this.id = i; this.isClickable =…
stud91
  • 1,854
  • 6
  • 31
  • 56
29
votes
1 answer

Stack using the Java 8 collection streaming API

I have a method which generates an object each time I execute it, and I need to reverse the order with which I am getting them. So I thought the natural way to do it would be a Stack, since it is LIFO. However, the Java Stack does not seem to play…
jbx
  • 21,365
  • 18
  • 90
  • 144
15
votes
1 answer

Remove all objects from list that does not exist in another list

I have two list List> list1 = new ArrayList>(); List> list2 = new ArrayList>(); HashMap hm = new HashMap(); hm.put("1",…
Amar
  • 421
  • 1
  • 6
  • 17
9
votes
2 answers

Convenience method to initialize mutable Set in Java

Is there a convenience method to initialize a Set equivalent to Collections.singleton, which returns a mutable Set instead of an immutable one?
Rian Schmits
  • 3,096
  • 3
  • 28
  • 44
8
votes
4 answers

Is CollectionUtils.isNotEmpty() better than a null check?

Many advice to use CollectionUtils.isNotEmpty(coll) rather then coll != null in the below use case also. if (CollectionUtils.isNotEmpty(coll)) { for (String str : coll) { } } instead of if (coll != null) { for (String str : coll) { …
Trying
  • 14,004
  • 9
  • 70
  • 110
7
votes
3 answers

Find all possible combination of enums

Is there an efficient way to find all possible combinations between multiple enums in Java? Consider the following three enums - public enum EnumOne { One ("One"), OneMore ("OneMore"); } public enum EnumTwo { Two ("Two"), } public enum…
JUG
  • 693
  • 9
  • 25
6
votes
2 answers

Synchronizing on an object in Java

I am looking for something akin to this syntax even though it doesn't exist. I want to have a method act on a collection, and for the lifetime of the method, ensure that the collection isn't messed with. So that could look like: private void…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
4
votes
1 answer

Why jdk code style uses a variable assignment and read on the same line - eg. (i=2) < max

I noticed that in the jdk source code and more specifically in the collections framework there is a preference in assigning variables right before reading them in expressions. Is it just a simple preference or is it something more important which I…
Kostas
  • 702
  • 3
  • 8
  • 17
3
votes
2 answers

Why does HashMap.get not return a nullable type?

I was a bit surprised that the following example throws a NullPointerException: fun main(args: Array) { val hm = HashMap() hm.put("alice", 42) val x = hm.get("bob") println(x) // BOOM } I thought there are no…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3
votes
2 answers

How to know whether Iterator of a Collection supports remove()?

All Java Collections implement Iterable, so they must provide an Iterator, which specifies an optional method remove(). When remove() is called on the Iterator, it can throw an UnsupportedOperationException. How do I know whether a Collection in the…
Aprel
  • 484
  • 1
  • 5
  • 10
2
votes
2 answers

contains() method in List not working as expected

the api of contains() method says "Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). " I overrode the equals()…
Sarabjeet
  • 264
  • 2
  • 17
2
votes
3 answers

JVM space complexity details: Singly Linked Lists vs Doubly Linked Lists

I have come across a curious situation where students (I'm a teaching assistant in this context) have to implement their own version of a Singly Linked List (SLL) and compare it empirically with the Java standard library implementation of a Doubly…
jjpe
  • 664
  • 4
  • 13
2
votes
4 answers

Why set is sorting added values?

When I start to add value into Set I get sorting elements. Please refer to this example: Set generated = new HashSet(); generated.add(2); generated.add(1); generated.add(0); Here I get sorting Set [0, 1, 2]. I would like…
Vahe Akhsakhalyan
  • 2,140
  • 3
  • 24
  • 38
2
votes
1 answer

A set that contains value pairs that performs checks without accounting for the key/value order

I need a data set that contains pairs of values. And you can check whether a pair exists in such set regardless of the pair's key/value order, that is: If the set contains <1,5> <8,3> And you ask if it contains <5,1>, it will return true because…
Saturn
  • 17,888
  • 49
  • 145
  • 271
1
vote
1 answer

Map and Map sorting

I am trying to resolve sorting a map , which contains huge data(1000K). Is there any efficient way than this to sorting these maps ? below is the code snippet. Map myMap1 = new HashMap(); …
1
2 3