Question

I'm new to Java, so please bear with me.

I have a HashMap of lists.

static Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();

I'm iterating through each entry of the HashMap and displaying it using:

for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }

Now, the entry.getValue() is where my list is stored, but I only want the first, second, and third entries of these lists. I believe I need to assign the list to it's own object prior to the iteration, but I can't seem to extract the list from the HashMap.

The output must display each entry of the HashMap including it's key, but only the first 3 items of the list.

Was it helpful?

Solution

Just print the subList without modifying your data:

System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, 3));

Assumption, your list has more than 2 elements, else you can use Math.min(3, entry.getValue().size()) (as mentioned in the comments) to avoid IndexOutOfBoundsException.

Hence,

System.out.println(entry.getKey() + " - " + entry.getValue().subList(0, Math.min(3, entry.getValue().size())));

OTHER TIPS

You can do it like this. I have made it elaborate to help you understand better. This code can be made shorter

   Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();
    for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
        Integer integer = entry.getKey();
        List<String> list =  entry.getValue();
        //first item
        String first = list.get(0);
        //second item  
         String second = list.get(1);
        //third item
        String third = list.get(2);
    }

Hope it helps!

The code below should give you what you want.

for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
    List<String> values = entry.getValue();
    StringBuilder builder = new StringBuilder(entry.getKey()).append(" - ");
    // here I assume the values list is never null, and we pick at most the first 3 entries
    builder.append("[")
    for (int i = 0; i < Math.min(3, values.size()); i++) {
        if (i > 0) builder.append(", ");
        builder.append(values.get(i));
    }
    builder.append("[");
    System.out.println(builder.toString());
}

What it does is, for each entry in the map:

  1. create a local variable with the value (a List<String>)
  2. create a StringBuilder for building the output on the entry
  3. iterate over the first 3 items (or less if the list is shorter) while building the output
  4. output the string in the builder

There's certainly a more elegant way to do it if you think about it, this is just a quick basic solution to your problem.

How about something like this:

String result = "";
List<String> strings = entry.getValue();
for (int i = 0; i < strings.size(); i++)
{
    result += strings.get(i);
}

My friend you can do this by

 Map<Integer, List<String>> contactList = new HashMap<Integer, List<String>>();
            for (Map.Entry<Integer, List<String>> entry : contactList.entrySet()) {
                List<String> list =  entry.getValue();
                String firstItem = list.get(0);
                String secondItem = list.get(1);
                String thirdItem = list.get(2);

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top