8

I have a list here:

List<QueryStrings> queryStrings

and QueryStrings is just a simple class

public class QueryStrings {
    private Integer id;
    private String rel;
    private String impl;
}

I need to put the List into a HashMap where the id will be the key, I am doing it like this right now, looping the List one item at a time:

HashMap<Integer, QueryStrings> queryMap = new HashMap<>();
for (QueryStrings element : queryStrings) {
    Integer thisId = Integer.parseInt(element.getId());
    queryMap.put(thisId, element);
}

Is there a better way to this? I do not want to loop each items, is that possible?

Edit:

Sorry I should not parse the integer, so the code should look like:

HashMap<Integer, QueryStrings> queryMap = new HashMap<>();
for (QueryStrings element : queryStrings) {
    queryMap.put(element.getId(), element);
}
Steven Yong
  • 5,163
  • 6
  • 35
  • 56
  • I believe something like this would work in Java8 `Map queryMap = queryStrings.stream().collect(toMap(element.getId(), element));` – J. Knight Jun 29 '17 at 04:54
  • Why call `parseInt()`? `id` is already of type `Integer`, just use the ID directly. – markspace Jun 29 '17 at 04:54
  • @ScaryWombat id is Interger right ?why we need to parse – soorapadman Jun 29 '17 at 04:56
  • Minus the extra call as noted in the current answer, looping is as "efficient" as any other method. Define "better way". – user2864740 Jun 29 '17 at 04:58
  • What exactly are you looking for? You ask the question, and then edit it to disregard the answers? Do you actually want performance metrics? Or just a quick glance? Optimising for readability? – Ryan Leach Jun 29 '17 at 05:07
  • @RyanTheLeach, sorry for not being clear, I want a better way to fill the map rather than looping each of the items. – Steven Yong Jun 29 '17 at 05:09
  • "better" Maps don't magically fill themselves, a loop *has* to happen somewhere, just a matter of if you do it, or if a method you call does. Outside of creating the map at the same time the list is created, inside the same loop, (which increases complexity of the loop, potentially making it harder for the jvm to optimize btw) you are going to need to define what you actually want improved. – Ryan Leach Jun 29 '17 at 05:12
  • If you are doing this operation many times, across many classes, you could potentially introduce an interface "Identifyable" which has a single abstract method, that returns an ID, then define a generic method, that outputs a map that varies on the type of list inserted. But there is no where near enough context for this to be a reasonable answer at the moment. In which case you have an XY problem. – Ryan Leach Jun 29 '17 at 05:13
  • 2
    This is not worth worrying about. You can populate millions of items into a `HashMap` per second, as long as the hashcodes aren't degenerate. – user207421 Jun 29 '17 at 05:57

1 Answers1

7

Don't parse the int's if it's already an int!

HashMap<Integer, QueryStrings> queryMap = new HashMap<>();
for (QueryStrings element : queryStrings) {
    queryMap.put(element.getId(), element);
}

That said, if it's Java 8 you can have more concise syntax, although the performance differences are minimal.

list.stream().collect(Collectors.toMap(QueryStrings::getId, Function.identity()));
Ryan Leach
  • 4,262
  • 5
  • 34
  • 71