String类:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
String是一个final类,代表不可变的字符序列。
字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
String对象的字符内容是存储在一个字符数组value[]中的。
String的存储结构:
获取两个字符串中最大相同子串
class Test {
public static void main(String[] args) {
String str1 = "abcdefhijkbcdf", str2 = "abcdf";
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < str2.length(); i++) {
for (int j = i, x = str2.length(); x > i; x--) {
if (str1.contains(str2.substring(i, x))) {
hashMap.put(str2.substring(i, x), str2.substring(i, x).length());
break;
}
}
}
Integer max = Collections.max(hashMap.values());
for (String s : hashMap.keySet()) {
if (hashMap.get(s) == max) {
System.out.println(s);
}
}
}
//方法2
public static List<String> getMaxSameSubString1(String str1, String str2) {
if (str1 != null && str2 != null) {
List<String> list = new ArrayList<String>();
String maxString = (str1.length() > str2.length()) ? str1 : str2;
String minString = (str1.length() > str2.length()) ? str2 : str1;
int len = minString.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
String subString = minString.substring(x, y);
if (maxString.contains(subString)) {
list.add(subString);
}
}
if (list.size() != 0) {
break;
}
}
return list;
}
return null;
}
String:不可变字符序列
StringBuffer:可变字符序列、效率低、线程安全
StringBuilder:可变字符序列、效率高、线程不安全
JAVA比较器
Comparable:
实现Comparable接口,重写compareTo方法
class Goods implements Comparable {
private String name;
private double price;
//按照价格,比较商品的大小
@Override
public int compareTo(Object o) {
if(o instanceof Goods) {
Goods other = (Goods) o;
if (this.price > other.price) {
return 1;
} else if (this.price < other.price) {
return -1;
}
return 0;
}
throw new RuntimeException("输入的数据类型不一致");
}
//构造器、getter、setter、toString()方法略
}
定制比较器:Comparator<>():
class test2 {
public static void main(String[] args) {
Person aa = new Person("AA", 12);
Person bb = new Person("BB", 15);
Person cc = new Person("CC", 12);
ArrayList<Person> list = new ArrayList<>();
list.add(aa);
list.add(bb);
list.add(cc);
for (Person person : list) {
System.out.println(person.getAge()+person.getName());
}
list.sort(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Person && o2 instanceof Person) {
Person a= (Person) o1;
Person b= (Person) o2;
if (a.getAge() != b.getAge()) {
return Integer.compare(a.getAge(), b.getAge());
} else {
return a.getName().compareTo(b.getName());
}
}
throw new RuntimeException("类型不对");
}
});
System.out.println("==========");
for (Person person : list) {
System.out.println(person.getAge()+person.getName());
}
}
}
class 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 void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
}
}
System类
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用
native long currentTimeMillis():
void exit(int status):该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等
void gc():垃圾回收机制只回收JVM堆内存里的对象空间。
String getProperty(String key):
BigDecimal类
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。
时间类:
new SimpleDateFormat()
class TimeTest{
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
System.out.println(date);
Date format = sdf.parse("2020-08-12 12:30:55");
String format1 = sdf.format(format);
System.out.println(format1);
}
}
Calendar类 抽象类
class TimeTest{
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendar.getTime();
System.out.println(date);
calendar.set(Calendar.DAY_OF_MONTH, 9);
System.out.println("当前时间日设置为9号,时间是:" + calendar.getTime());
calendar.add(Calendar.HOUR, 2);
System.out.println("当前时间加2小时后,时间是:" + calendar.getTime());
calendar.add(Calendar.MONTH, -2);
System.out.println("当前日期减2个月后,时间是:" + calendar.getTime());
}
}
JDK8中新日期时间API
LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,类似Calendar,但是它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间
class TimeTest{
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();//2020-08-15
LocalTime localTime = LocalTime.now();//23:44:10.772
LocalDateTime localDateTime = LocalDateTime.now();//2020-08-15T23:44:10.772
//自定义时间,无偏移,Calendar会有偏移
LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2020, 8, 8), LocalTime.of(8, 8, 8));
System.out.println(dateTime);//2020-08-08T08:08:08
int dayOfMonth = dateTime.getDayOfMonth();//8
int year = dateTime.getYear();//2020
Month month = dateTime.getMonth();//AUGUST
int monthValue = dateTime.getMonthValue();//8
LocalDateTime withHour = dateTime.withHour(18);//2020-08-08T18:08:08
LocalDateTime plusDays = dateTime.plusDays(5);//2020-08-13T08:08:08
LocalDateTime minusDays = dateTime.minusDays(5);//2020-08-03T08:08:08
}
}
Instant:
时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。类似getTime();
class TimeTest{
public static void main(String[] args) {
Date date = new Date();//Sun Aug 16 00:06:08 CST 2020\
Instant instant = Instant.now();//返回UTC时间 2020-08-15T16:06:08.491Z 差8小时
OffsetDateTime atOffset = instant.atOffset(ZoneOffset.ofHours(8));//2020-08-15T00:06:08.432+08:00
long l = instant.toEpochMilli();//1597507901277
Instant instant1 = Instant.ofEpochMilli(1597507901277l);//2020-08-15T16:11:41.277Z
}
}
Duration,Period
计算时间日期间隔
class TimeTest{
public static void main(String[] args) {
LocalTime now = LocalTime.now();
LocalTime plusHours = now.plusHours(8);
Duration between1 = Duration.between(now, plusHours);//计算时间间隔
LocalDate localDate = LocalDate.now();
LocalDate plusDays = localDate.plusDays(8);
Period between = Period.between(localDate, plusDays);//计算日期间隔
}
}
DateTimeFormatter
格式化时间 类似SimpleDateFormat()
class TimeTest{
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(LocalDateTime.now());
System.out.println(format);//2020-08-16 00:33:09
TemporalAccessor parse = formatter.parse("2020-08-16 00:32:52");
System.out.println(parse);//{},ISO resolved to 2020-08-16T00:32:52
}
}
枚举类
使用 enum 定义的枚举类默认继承了 java.lang.Enum类,因此不能再继承其他类
枚举类的构造器只能使用 private 权限修饰符
枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾)。列出的实例系统会自动添加 public static final 修饰
必须在枚举类的第一行声明枚举类对象
常用方法:
values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
alueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
toString():返回当前枚举类对象常量的名称
class Test {
public static void main(String[] args) {
System.out.println(Person.lufei.getName());//路飞
Person lufei = Person.valueOf("lufei");
lufei.fight();//使用三挡
Person[] values = Person.values();
System.out.println(Arrays.asList(values));//[lufei, suolong]
String s = Person.suolong.toString();//suolong
}
}
enum Person {
lufei("路飞",18),
suolong("索隆",20);
private final String name;
private final Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
private Person(String name ,Integer age) {
this.name=name;
this.age=age;
}
void fight(){
if (this.name==lufei.getName()){
System.out.println("使用三挡");
}else {
System.out.println("使用三刀流");
}
}
}
使用接口组织枚举
//另一个文件
public interface Fihjt {
enum Lufei implements Fihjt {
见闻色, 霸王色, 武装色, 流樱
}
enum Suolong implements Fihjt {
一刀流, 二刀流, 三刀流
}
}
class test {
public static void main(String[] args) {
for (Fihjt.Lufei val : Fihjt.Lufei.values()) {
System.out.println(val);
}
System.out.println(Fihjt.Suolong.三刀流);
}
}