Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of entries, how to add a new Entry?

Tags:

java

In Java, I am implementing this:

List<Entry<String, Integer>> listObjects = new ArrayList<Entry<String, Integer>>();

but how can I add a new Entry?

as it does not work with: listObjects.add(new Entry<"abc", 1>());

thanks in advance.

like image 233
olidev Avatar asked May 25 '11 07:05

olidev


People also ask

How do I add an entry to a List in Java?

There are two methods to add elements to the list. add(E e): appends the element at the end of the list. Since List supports Generics, the type of elements that can be added is determined when the list is created. add(int index, E element): inserts the element at the given index.

What is List entry in Java?

List in Java provides the facility to maintain the ordered collection. It contains the index-based methods to insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the null elements in the list.

How do you add integers to a List in Java?

Using Stream.collect() The second method for calculating the sum of a list of integers is by using the collect() terminal operation: List<Integer> integers = Arrays. asList(1, 2, 3, 4, 5); Integer sum = integers.

Can you instantiate a List in Java?

In Java, List is an interface. That is, it cannot be instantiated directly. Instead you can use ArrayList which is an implementation of that interface that uses an array as its backing store (hence the name).


1 Answers

I know it's a pretty older thread but you can do it as follows:

listObjects.add(new java.util.AbstractMap.SimpleEntry<String, Integer>("abc", 1));

It might help someone like me, who was trying to do this recently!

I hope it helps :-)

like image 95
Anam Jabbar Avatar answered Sep 19 '22 11:09

Anam Jabbar