Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to initialize List object in Java?

How to initialize List object in Java?

August 1, 2021 by James Palmer

If you check the API for List you’ll notice it says:
Interface List

Being an interface means it cannot be instantiated (no new List() is possible).
If you check that link, you’ll find some classes that implement List:

All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector

Those can be instantiated. Use their links to know more about them, I.E: to know which fits better your needs.
The 3 most commonly used ones probably are:
List supplierNames1 = new ArrayList();
List supplierNames2 = new LinkedList();
List supplierNames3 = new Vector();

Bonus:
You can also instantiate it with values, in an easier way, using the Arrays class, as follows:
List supplierNames = Arrays.asList(“sup1”, “sup2”, “sup3”);
System.out.println(supplierNames.get(1));

But note you are not allowed to add more elements to that list, as it’s fixed-size.

Can’t instantiate an interface but there are few implementations:
JDK2
List list = Arrays.asList(“one”, “two”, “three”);

JDK7
//diamond operator
List list = new ArrayList<>();
list.add(“one”);
list.add(“two”);
list.add(“three”);

JDK8
List list = Stream.of(“one”, “two”, “three”).collect(Collectors.toList());

JDK9
// creates immutable lists, so you can’t modify such list
List immutableList = List.of(“one”, “two”, “three”);

// if we want mutable list we can copy content of immutable list
// to mutable one for instance via copy-constructor (which creates shallow copy)
List mutableList = new ArrayList<>(List.of(“one”, “two”, “three”));

Plus there are lots of other ways supplied by other libraries like Guava.
List list = Lists.newArrayList(“one”, “two”, “three”);

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in