目次
- はじめに
- java.util パッケージ
- ArrayListの使用方法
- HashMapの便利な操作
- LinkedListとその応用
- java.io パッケージ
- ファイルの読み書き
- バッファリングと例外処理
- java.text パッケージ
- 日付と時刻のフォーマット
- 数値フォーマットのカスタマイズ
- java.util.regex パッケージ
- 正規表現の基本と応用
- パターンとマッチング
- まとめ
1. はじめに このブログでは、Javaの標準ライブラリを効果的に活用する方法について紹介します。
2. java.util パッケージ
ArrayListの使用方法 ArrayListは可変長の配列を提供するクラスで、要素の追加や削除が簡単に行えます。以下はArrayListの基本的な使用例です。
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); for (String fruit : fruits) { System.out.println(fruit); } } }
HashMapの便利な操作 HashMapはキーと値のペアを管理するクラスで、高速なデータアクセスが可能です。以下はHashMapの基本的な操作例です。
import java.util.HashMap; public class HashMapExample { public static void main(String[] args) { HashMap<String, Integer> ageMap = new HashMap<>(); ageMap.put("Alice", 30); ageMap.put("Bob", 25); ageMap.put("Charlie", 40); int aliceAge = ageMap.get("Alice"); System.out.println("Alice's age: " + aliceAge); } }
LinkedListとその応用 LinkedListは要素の追加や削除が高速な双方向リストです。以下はLinkedListの基本的な操作例です。
import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<>(); linkedList.add("Java"); linkedList.add("Python"); linkedList.add("C++"); System.out.println("First element: " + linkedList.getFirst()); } }
3. java.io パッケージ
ファイルの読み書き java.ioパッケージを使ってファイルの読み書きを行う方法を紹介します。
import java.io.*; public class FileReadWriteExample { public static void main(String[] args) { try { File file = new File("example.txt"); FileWriter writer = new FileWriter(file); writer.write("Hello, Java Standard Library!"); writer.close(); FileReader reader = new FileReader(file); int data; while ((data = reader.read()) != -1) { System.out.print((char) data); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
バッファリングと例外処理 ファイルの読み書きでバッファリングと例外処理を行う方法を説明します。
import java.io.*; public class BufferedFileReadWriteExample { public static void main(String[] args) { try { File file = new File("example.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write("Hello, Java Standard Library!"); writer.close(); BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
4. java.text パッケージ
日付と時刻のフォーマット java.textパッケージを使って日付と時刻のフォーマットを行う方法を紹介します。
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date currentDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String formattedDate = sdf.format(currentDate); System.out.println("Current date and time: " + formattedDate); } }
数値フォーマットのカスタマイズ 数値のフォーマットをカスタマイズする方法を説明します。
import java.text.DecimalFormat; public class NumberFormatExample { public static void main(String[] args) { double number = 12345.6789; DecimalFormat df = new DecimalFormat("#,###.00"); String formattedNumber = df.format(number); System.out.println("Formatted number: " + formattedNumber); } }
正規表現の基本と応用 java.util.regexパッケージを使って正規表現の基本と応用を説明します。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main(String[] args) { String text = "The quick brown fox jumps over the lazy dog."; String patternString = "fox"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println("Found at: " + matcher.start()); } } }
パターンとマッチング 正規表現パターンと文字列のマッチングについての例を示します。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternMatchingExample { public static void main(String[] args) { String text = "Java 15, Java 16, Java 17"; String patternString = "Java \\d+"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println("Match: " + matcher.group()); } } }
6. まとめ Java標準ライブラリの多くの便利なツールを活用することで、効率的な開発が可能です。このブログを通じて、Java標準ライブラリの活用方法を理解し、より良いコードを書けることを願っています。