Getting started with Java LanguageInheritanceStreamsExceptions and exception handlingCollectionsLambda ExpressionsGenericsFile I/OArraysInterfacesMapsStringsInputStreams and OutputStreamsDefault MethodsClasses and ObjectsBasic Control StructuresConcurrent Programming (Threads)Console I/OSingletonsVisibility (controlling access to members of a class)Regular ExpressionsAutoboxingDocumenting Java CodeExecutor, ExecutorService and Thread poolsObject Class Methods and ConstructorJAXBPrimitive Data TypesNetworkingOptionalEnumsHttpURLConnectionAnnotationsAudioDate ClassCalendar and its SubclassesNashorn JavaScript engineJava Native InterfaceRemote Method Invocation (RMI)Iterator and IterableOperatorsAssertingScannerProperties ClassPreferencesReflection APIConstructorsByteBufferSerializationJSON in JavaRandom Number GenerationRecursionPolymorphismStringBuilderReference Data TypesBit ManipulationJava AgentsEncapsulationType ConversionBigIntegerBigDecimalRSA EncryptionVarargs (Variable Argument)ThreadLocalLogging (java.util.logging)Using the static keywordDisassembling and DecompilingResources (on classpath)log4j / log4j2JVM FlagsOracle Official Code StandardCharacter encodingJava Memory ManagementImmutable ObjectsObject CloningAlternative CollectionsListsBufferedWriterLocalTimeSetsComparable and ComparatorJVM Tool InterfaceNested and Inner ClassesApache Commons LangGetters and SettersThe ClasspathBytecode ModificationXML Parsing using the JAXP APIsReference TypesLocalization and InternationalizationJAX-WSXML XPath EvaluationJava Performance TuningParallel programming with Fork/Join frameworkCommon Java PitfallsNon-Access ModifiersJava Compiler - 'javac'XJCProcessInstalling Java (Standard Edition)Command line Argument ProcessingDates and Time (java.time.*)Fluent InterfaceXOM - XML Object ModelJust in Time (JIT) compilerFTP (File Transfer Protocol)Java Native AccessModulesJava Pitfalls - Exception usageJava Pitfalls - Language syntaxServiceLoaderClassloadersObject ReferencesJava Pitfalls - Performance IssuesCreating Images ProgrammaticallyAppletsNIO - NetworkingNew File I/OSecure objectsJava Pitfalls - Threads and ConcurrencySplitting a string into fixed length partsJava Pitfalls - Nulls and NullPointerExceptionSecurityManagerJNDIsuper keywordThe java.util.Objects ClassThe Java Command - 'java' and 'javaw'Atomic TypesJava Floating Point OperationsConverting to and from Stringssun.misc.UnsafeJava Memory ModelJava deploymentJava plugin system implementationsQueues and DequesRuntime CommandsNumberFormatSecurity & CryptographyJava Virtual Machine (JVM)Unit TestingJavaBeanExpressionsLiteralsJava SE 8 FeaturesJava SE 7 FeaturesPackagesCurrency and MoneyConcurrent CollectionsUsing ThreadPoolExecutor in MultiThreaded applications.Java Editions, Versions, Releases and DistributionsDynamic Method DispatchJMXSecurity & CryptographyGenerating Java CodeJShellBenchmarksCollection Factory MethodsMulti-Release JAR FilesStack-Walking APITreeMap and TreeSetSocketsJava SocketsUsing Other Scripting Languages in JavaFunctional InterfacesList vs SET2D Graphics in JavaClass - Java ReflectionDequeue InterfaceEnum MapEnumSet classLocal Inner ClassJava Print ServiceImmutable ClassString TokenizerFileUpload to AWSAppDynamics and TIBCO BusinessWorks Instrumentation for Easy IntegrationReaders and WritersHashtableEnum starting with numberSortedMapWeakHashMapLinkedHashMapStringBufferChoosing CollectionsC++ ComparisonCompletableFuture

Collection Factory Methods

Other topics

List<E> Factory Method Examples

  • List<Integer> immutableEmptyList = List.of();
    • Initializes an empty, immutable List<Integer>.
  • List<Integer> immutableList = List.of(1, 2, 3, 4, 5);
    • Initializes an immutable List<Integer> with five initial elements.
  • List<Integer> mutableList = new ArrayList<>(immutableList);
    • Initializes a mutable List<Integer> from an immutable List<Integer>.

Set<E> Factory Method Examples

  • Set<Integer> immutableEmptySet = Set.of();
    • Initializes an empty, immutable Set<Integer>.
  • Set<Integer> immutableSet = Set.of(1, 2, 3, 4, 5);
    • Initializes an immutable Set<Integer> with five initial elements.
  • Set<Integer> mutableSet = new HashSet<>(immutableSet);
    • Initializes a mutable Set<Integer> from an immutable Set<Integer>.

Map<K, V> Factory Method Examples

  • Map<Integer, Integer> immutableEmptyMap = Map.of();
    • Initializes an empty, immutable Map<Integer, Integer>.
  • Map<Integer, Integer> immutableMap = Map.of(1, 2, 3, 4);
    • Initializes an immutable Map<Integer, Integer> with two initial key-value entries.
  • Map<Integer, Integer> immutableMap = Map.ofEntries(Map.entry(1, 2), Map.entry(3, 4));
    • Initializes an immutable Map<Integer, Integer> with two initial key-value entries.
  • Map<Integer, Integer> mutableMap = new HashMap<>(immutableMap);
    • Initializes a mutable Map<Integer, Integer> from an immutable Map<Integer, Integer>.

Syntax:

  • static <E> List<E> of​()
  • static <E> List<E> of​(E e1)
  • static <E> List<E> of​(E e1, E e2)
  • static <E> List<E> of​(E e1, E e2, ..., E e9, E e10)
  • static <E> List<E> of​(E... elements)
  • static <E> Set<E> of​()
  • static <E> Set<E> of​(E e1)
  • static <E> Set<E> of​(E e1, E e2)
  • static <E> Set<E> of​(E e1, E e2, ..., E e9, E e10)
  • static <E> Set<E> of​(E... elements)
  • static <K,V> Map<K,V> of()
  • static <K,V> Map<K,V> of(K k1, V v1)
  • static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)
  • static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, ..., K k9, V v9, K k10, V v10)
  • static <K,V> Map<K,V> ofEntries​(Map.Entry<? extends K,? extends V>... entries)

Parameters:

Method w/ ParameterDescription
List.of(E e)A generic type that can be a class or interface.
Set.of(E e)A generic type that can be a class or interface.
Map.of(K k, V v)A key-value pair of generic types that can each be a class or interface.
Map.of(Map.Entry<? extends K, ? extends V> entry)A Map.Entry instance where its key can be K or one of its children, and its value can be V or any of its children.

Contributors

Topic Id: 9783

Example Ids: 30133,30134,30135

This site is not affiliated with any of the contributors.