List of common interfaces in Java: To understand common interfaces and their functions, specific code examples are needed
Introduction:
In the Java programming language, an interface is a Very important concept. An interface defines a set of methods that represent the behaviors that a class can implement. Using interfaces can achieve code reuse and modularization, and improve the maintainability and scalability of the code.
This article will introduce commonly used interfaces in Java and their functions. For each interface, we'll illustrate usage with specific code examples.
1. Iterable interface:
The Iterable interface is the root interface in the Java collection framework. It defines a set of methods for traversing collection elements. Classes that implement the Iterable interface can use the foreach statement to iterate over the elements in a collection.
Code example:
public class MyCollection implements Iterable<String> { private List<String> data; public MyCollection() { data = new ArrayList<String>(); data.add("Apple"); data.add("Banana"); data.add("Orange"); } @Override public Iterator<String> iterator() { return data.iterator(); } } public class Main { public static void main(String[] args) { MyCollection collection = new MyCollection(); for (String item : collection) { System.out.println(item); } } }
2. Comparable interface:
The Comparable interface is a generic interface that defines a method for comparing objects. Classes that implement the Comparable interface can perform customized object comparisons for sorting or search operations.
Code example:
public class Person implements Comparable<Person> { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Person person) { return this.age - person.getAge(); } } public class Main { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Alice", 25)); people.add(new Person("Bob", 30)); people.add(new Person("Charlie", 20)); Collections.sort(people); for (Person person : people) { System.out.println(person.getName() + ", " + person.getAge()); } } }
3. Runnable interface:
The Runnable interface is a functional interface that defines a method for describing runnable tasks. Classes that implement the Runnable interface can be executed as thread tasks.
Code example:
public class MyTask implements Runnable { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ": " + i); } } } public class Main { public static void main(String[] args) { Thread t1 = new Thread(new MyTask()); Thread t2 = new Thread(new MyTask()); t1.start(); t2.start(); } }
4. Closeable interface:
The Closeable interface is an interface that implements a resource that can be closed. Classes that implement the Closeable interface can use the try-with-resources statement to automatically close resources.
Code examples:
public class MyResource implements Closeable { public MyResource() { System.out.println("Resource opened"); } public void doSomething() { System.out.println("Doing something"); } @Override public void close() throws IOException { System.out.println("Resource closed"); } } public class Main { public static void main(String[] args) { try (MyResource resource = new MyResource()) { resource.doSomething(); } catch (IOException e) { e.printStackTrace(); } } }
Conclusion:
This article introduces the commonly used interfaces in Java: Iterable interface, Comparable interface, Runnable interface and Closeable interface, and provides corresponding code examples. By understanding the functions and usage of these interfaces, you can more flexibly use interfaces to organize and reuse code in daily Java development. At the same time, through specific code examples, readers can better understand the usage and operating effects of the interface.
The above is the detailed content of Comprehensive list of common Java interfaces: Master common interface functions. For more information, please follow other related articles on the PHP Chinese website!