Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Java Tutorial

Java HOMEJava IntroJava Get StartedJava SyntaxJava OutputJava CommentsJava VariablesJava Data TypesJava Type CastingJava OperatorsJava StringsJava MathJava BooleansJava If...ElseJava SwitchJava While LoopJava For LoopJava Break/ContinueJava Arrays

Java Methods

Java MethodsJava Method ChallengeJava Method ParametersJava Method OverloadingJava ScopeJava Recursion

Java Classes

Java OOPJava Classes/ObjectsJava Class AttributesJava Class MethodsJava Class ChallengeJava ConstructorsJava this KeywordJava ModifiersJava EncapsulationJava Packages / APIJava InheritanceJava PolymorphismJava super KeywordJava Inner ClassesJava AbstractionJava InterfaceJava AnonymousJava EnumJava User InputJava Date

Java Errors

Java ErrorsJava DebuggingJava ExceptionsJava Multiple ExceptionsJava try-with-resources

Java File Handling

Java FilesJava Create FilesJava Write FilesJava Read FilesJava Delete Files

Java I/O Streams

Java I/O StreamsJava FileInputStreamJava FileOutputStreamJava BufferedReaderJava BufferedWriter

Java Data Structures

Java Data StructuresJava CollectionsJava ListJava ArrayListJava LinkedListJava List SortingJava SetJava HashSetJava TreeSetJava LinkedHashSetJava MapJava HashMapJava TreeMapJava LinkedHashMapJava IteratorJava Algorithms

Java Advanced

Java Wrapper ClassesJava GenericsJava AnnotationsJava RegExJava ThreadsJava LambdaJava Advanced Sorting

Java Projects

Java Projects

Java How To's

Java How Tos

Java Reference

Java ReferenceJava KeywordsJava String MethodsJava Math MethodsJava Output MethodsJava Arrays MethodsJava ArrayList MethodsJava LinkedList MethodsJava HashMap MethodsJava Scanner MethodsJava File MethodsJava FileInputStreamJava FileOutputStreamJava BufferedReaderJava BufferedWriterJava Iterator MethodsJava Collections MethodsJava System MethodsJava Errors & Exceptions

Java Examples

Java ExamplesJava VideosJava CompilerJava ExercisesJava QuizJava Code ChallengesJava ServerJava SyllabusJava Study PlanJava Interview Q&AJava Certificate


JavaData Structures


Java Data Structures

Data structures are ways to store and organize data so you can use it efficiently.

Anarray is an example of a data structure, which allows multiple elements to be stored in a single variable.

Java includes many other data structures as well, in thejava.util package. Each is used to handle data in different ways.

Some of the most common are:

  • ArrayList
  • HashSet
  • HashMap

Tip: Data structures are like supercharged arrays - more flexible and feature-rich!

We'll explore all of these - and many more - in detail later, but for now, here's a quick introduction to each one.


ArrayList

AnArrayList is a resizable array that can grow as needed.

It allows you to store elements and access them by index.

Example

// Import the ArrayList classimport java.util.ArrayList;public class Main {  public static void main(String[] args) {    ArrayList<String> cars = new ArrayList<String>();    cars.add("Volvo");    cars.add("BMW");    cars.add("Ford");    cars.add("Mazda");    System.out.println(cars);}}

Try it Yourself »


HashSet

AHashSet is a collection where every element is unique - no duplicates are allowed.

Example

// Import the HashSet classimport java.util.HashSet;public class Main {  public static void main(String[] args) {    HashSet<String> cars = new HashSet<String>();    cars.add("Volvo");    cars.add("BMW");    cars.add("Ford");    cars.add("BMW");  // Duplicate    cars.add("Mazda");    System.out.println(cars);  }}

Try it Yourself »

Note: In the example above, even though BMW is added twice it only appears once in the set because every element in a set has to be unique.



HashMap

AHashMap storeskey-value pairs, which are great when you want to store values and find them by a key (like a name or ID):

Example

// Import the HashMap classimport java.util.HashMap;public class Main {  public static void main(String[] args) {    // Create a HashMap object called capitalCities    HashMap<String, String> capitalCities = new HashMap<String, String>();    // Add keys and values (Country, City)    capitalCities.put("England", "London");    capitalCities.put("Germany", "Berlin");    capitalCities.put("Norway", "Oslo");    capitalCities.put("USA", "Washington DC");    System.out.println(capitalCities);  }}

Try it Yourself »


Data Structures Overview

Data StructureStoresKeeps Order?Allows Duplicates?Best For
ArrayListOrdered elementsYesYesAccessing elements by index
HashSetUnique elementsNoNoAvoiding duplicates, fast checks
HashMapKey-value pairsNoYes (keys are unique)Fast lookup by key

Iterators

When learning about data structures, you will often hear about iterators too.

An iterator is a way to loop through elements in a data structure.

It is called an "iterator" because "iterating" is the technical term for looping.

Example

Using an Iterator with ArrayList:

import java.util.ArrayList;import java.util.Iterator;public class Main {  public static void main(String[] args) {    // Create an ArrayList of Strings    ArrayList<String> cars = new ArrayList<String>();    cars.add("Volvo");    cars.add("BMW");    cars.add("Ford");    cars.add("Mazda");    // Get an iterator for the ArrayList    Iterator<String> it = cars.iterator();    // Iterate through the list using the iterator    while(it.hasNext()) {      System.out.println(it.next());    }  }}

Try it Yourself »

Next, let's take a closer look at each data structure in more detail.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp