开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 10 天,点击查看活动详情
如果您是 Java 开发人员,那么您一定已经在您的项目中使用过其中的大部分!
Java中经常使用的方法有很多,具体使用最多的方法可以看你的项目需要。下面是一些在 Java 中使用的常用方法和转换的示例。
Object.toString():此方法用于将对象转换为字符串表示形式。它通常在自定义类中被覆盖以提供对象的有意义的字符串表示。
Person p = new Person ( "John" , 30 );
System.out.println(p.toString());
2. parseInt():此方法用于将字符串转换为整数。 它是类的一部分Integer,可以按如下方式使用。
int x = Integer.parseInt("123");
3. valueOf():此方法用于将原始数据类型(如intor )转换为相应包装类(如or )double的对象。它可以按如下方式使用:Integer``Double
Integer y = Integer.valueOf(123); // y is an Integer object with the value 123
Double e = Double.valueOf(3.12); // e is a Double object with the value 3.12
Long l= Long.valueOf(123434); // l is a Long object with the value 123434
Boolean b = Boolean.valueOf(true); // b is a Boolean object with the value true
String s = String.valueOf(29) // s is a String object with value "29"
4. equals():此方法用于比较两个对象的值是否相等。它在Object类中定义,可以在自定义类中重写以提供基于值的相等性而不是基于引用的相等性。
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return p.x == x && p.y == y;
}
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);
Point p3 = new Point(2, 3);
System.out.println(p1.equals(p2)); // true
System.out.println(p1.equals(p3)); // false
}
}
5. Collections.sort():此方法用于对列表或数组的元素进行排序。Collections它在列表类和Arrays数组类中定义。
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(3, 2, 1);
Collections.sort(list);
System.out.println(list); // [1, 2, 3]
}
}
Arrays 中有 sort 方法对数组元素进行排序。
int[] array = { 3, 1, 2 };
Arrays.sort(array); // array is now [1, 2, 3]
6. length():此方法用于获取字符串或数组的长度。对于字符串,它在类中定义String并返回字符串中的字符数。对于数组,它是数组对象的一个属性,返回数组中元素的个数。
String s = "hello";
int len = s.length(); // len is 5
int[] arr = {1, 2, 3};
int len = arr.length; // len is 3
8. trim(): 此方法用于从字符串中删除前导和尾随空格。它在 String 类中定义,并返回一个删除了空格的新字符串。
"hello".substring(1, 3) // "el"
9.get(): 此方法用于从列表或地图中检索元素。它定义在List和Map接口中,其具体实现可以根据列表或映射的实现而有所不同。
List<Integer> list = Arrays.asList(1, 2, 3);
int x = list.get(0); // x is 1
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
int y = map.get("one"); // y is 1
10.format() : 此方法用于使用 printf 样式格式化来格式化字符串。它在String类中定义并采用格式字符串和可变数量的参数。
String s = String.format("%d %s", 123, "hello"); // "123 hello"
11. ArrayList.add()- 此方法允许您将元素添加到 ArrayList。例如:
ArrayList<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Bob");
12. Map.put()- 此方法允许您将键值对添加到 Map。例如:
Map<String, Integer> ages = new HashMap<>();
ages.put("John", 30);
ages.put("Jane", 25);
ages.put("Bob", 35);
- 当前日期
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate currentDate = LocalDate.now(); // current date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy"); //custom format
String formattedDate = currentDate.format(formatter);
System.out.println(formattedDate);
- 将
List转换为Set以去除重复项
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
// Create a List
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
// Create a Set from the List
Set<String> set = new HashSet<>(list);
// Print the Set
System.out.println(set); // Output: [orange, apple, banana]
}
}
- 将
float值转换为int
float f = 3.14f;
int i = (int)f; // i is 3
//or below way
float f = 3.14f;
int i = (int)Math.round(f); // i is 3
//or below way too
float f = 3.14f;
int i = (int)Math.floor(f); // i is 3
int j = (int)Math.ceil(f); // j is 4