1.语法篇
1.Lambda表达式
基本用法:类似于匿名内部类,用来实现接口Interface
基于这个原理又拓展到很多很多用法,
基本语法接口名 方法名(参数1,参数2,…)->{实现方法;};
4. 函数式接口
4.1 什么是函数式接口
==只包含一个抽象方法的接口,就称为函数式接口。==我们可以通过Lambda表达式来创建该接口的实现对象。
我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以用于检测它是否是一个函数式接口,同时javadoc也会包含一条声明,说明这个接口是一个函数式接口。
4.2 自定义函数式接口
按照函数式接口的定义,自定义一个函数式接口,如下:
@FunctionalInterface
public interface MyFuncInterf {
public T getValue(String origin);
}
1
2
3
4
定义一个方法将函数式接口作为方法参数。
public String toLowerString(MyFuncInterf mf,String origin){
return mf.getValue(origin);
}
1
2
3
将Lambda表达式实现的接口作为参数传递。
public void test07(){
String value=toLowerString((str)->{
return str.toLowerCase();
},“ABC”);
System.out.println(value);//结果ABC
}
2.调用Comparator接口实现自定义的类型的排序。
/**
* @作者:胡向挺
* @版本 2022船新版本
*/
public class Sort {
public static void main(String[] args) {
person person1=new person(100);
person person2=new person(300);
person person3=new person(150);
person[]persons=new person[3];
persons[0]=person1;
persons[1]=person2;
persons[2]=person3;
Arrays.sort(persons,new Comparator()//注意comparable 和Comparable的区别
{
@Override
public int compare(Object o1, Object o2) {
person x=(person) o1;
person y=(person)o2;
return (x.getWage()-y.getWage());
}
});
for(int i=0;i<3;i++) {
System.out.print(persons[i]);
}
}
}
class person
{
public int wage;
public person(int wage) {
this.wage = wage;
}
@Override
public String toString() {
return "person{" +
"wage=" + wage +
'}';
}
public int getWage() {
return wage;
}
}
涉及匿名内部类 强制类型转换,多态的知识。
有代表意义、
3.大数的操作
public class Biginteger {
public static void main(String[] args) {
BigInteger bigInteger=new BigInteger("999999999999");
BigInteger a= new BigInteger("3333");
BigInteger c=bigInteger.add(a);//大数相加
BigInteger b=bigInteger.subtract(a);//大数相减
BigInteger d=bigInteger.multiply(a);//大数相乘
BigInteger e=bigInteger.divide(a);//大数相除
System.out.print(e);
}
}
基本的操作 没啥好说的
4.深拷贝和浅拷贝的区别
定义:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。即对象的浅拷贝会对“主”对象进行拷贝,但不会复制主对象里面的对象。”里面的对象“会在原来的对象和它的副本之间共享。
简而言之,浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象。
浅拷贝的代码例子:
public class test1 {
public static void main(String[] args) {
houseman A=new houseman(21,"WSR");
houseman B=new houseman(20,"ZH");
cat a=new cat("HXT","黄色",15,A);
cat b= null;
try {
b = (cat)a.clone();
A.setName("LBWWWW");//浅拷贝
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
a.setName("DYM");//浅拷贝不变的例子
System.out.print(b);
}
}
class cat implements Cloneable
{
public cat(String name, String color, int age, houseman a) {
this.name = name;
this.color = color;
this.age = age;
A = a;
}
private String name;
private String color;
private int age;
private houseman A;
public String getName() {
return name;
}
public String getColor() {
return color;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setColor(String color) {
this.color = color;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "cat{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", age=" + age +
", A=" + A +
'}';
}
@Override
public boolean equals(Object o) {
if (this.getClass() == o) return true;
if (o == null || getClass() != o.getClass()) return false;
cat cat = (cat) o;
return age == cat.age && Objects.equals(name, cat.name) && Objects.equals(color, cat.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color, age);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public houseman getA() {
return A;
}
public void setA(houseman a) {
A = a;
}
}
class houseman
{
int age;
String name;
public houseman(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "houseman{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
}
2.对于深拷贝。我的评价是: 浅拷贝嵌套浅拷贝罢了,考虑对象的对象。
5.重写equlas方法
public class EQUALS {
}
class dog
{
private String color;
private int age;
private String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
dog dog = (dog) o;
return age == dog.age && Objects.equals(color, dog.color) && Objects.equals(name, dog.name);
}
@Override
public int hashCode() {
return Objects.hash(color, age, name);
}
}
class tiger extends dog
{
private String noise;
public boolean equals(Object o)
{
if(!(super.equals(o)))
return false;
tiger tiger1=(tiger) o;
if(Objects.equals(this.noise,tiger1.noise));
return true;
}
}
还要注意重载和重写知识点的问题,Object超类的常见方法。
2.算法篇
1.删除数组相同的元素(双指针思想)
class Solution {
public int removeDuplicates(int[] nums)
{
if(nums.length0||numsnull)
return 0;
int left=0;int right=1;
for(right=1;right<nums.length;right++)
{
if(nums[left]!=nums[right])
left++;
nums[left]=nums[right];
}
}
2.反转字符串(正常交换就行 开辟数组)
class Solution {
public void reverseString(char[] s) {
int length=s.length;
for(int i=0;i<length;i++)
{
char temp=s[length-1];
s[length-1]=s[i];
s[i]=temp;
length–;
}
}
}
3.整数反转
class Solution {
public int reverse(int x) {
long sum=0;
while(x!=0)
{
sum=sum*10+x%10;
x=x/10;
}
return (int)sum==sum?(int)sum:0;
}
}
4.巧妙删除单链表的节点(要删除的节点指向下下一个节点,数值设置成和下一个节点相同。)
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
5.递归求二叉树深度
/**
-
Definition for a binary tree node.
-
public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode() {}
-
TreeNode(int val) { this.val = val; }
-
TreeNode(int val, TreeNode left, TreeNode right) {
-
this.val = val;
-
this.left = left;
-
this.right = right;
-
}
-
}
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int maxleft=maxDepth(root.left);
int maxright=maxDepth(root.right);
if(maxleft>maxright)
return maxleft+1;
else
return maxright+1;}
}
5.类似于归并排序的 合并两个有序数组
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int arr[]=new int[m+n];
int i=0;int j=0;int index=0;
while(i<m&&j<n)
{
if(nums1[i]<=nums2[j])
{
arr[index]=nums1[i];
i++;
index++;
}
if(nums2[j]<nums1[i])
{
arr[index]=nums2[j];
j++;
index++;
}
}
while(i<m)
{
arr[index]=nums1[i];
index++;
i++;
}
while(j<n)
{
arr[index]=nums2[j];
index++;
j++;
}
for( i=0;i<index;i++)
{
nums1[i]=arr[i];
}
}
}
6.动态规划爬楼问题
class Solution {
public int climbStairs(int n) {
int d[]= new int [n+1];
if(n<=1)
return 1;
if(n<3)
return n;
if(n>=3)
{
d[1]=1;d[2]=2;
for(int i=3;i<=n;i++)
{
d[i]=d[i-1]+d[i-2];
}
}
return d[n];
}
}
7.利用随机数乱序数组
class Solution {
private int nums[];
Random random=new Random();
public Solution(int[] nums) {
this.nums=nums;
}
public int[] reset() {
return nums;
}
public int[] shuffle() {
if(nums==null)
return null;
int a[]=nums.clone();
for(int i=0;i<nums.length;i++)
{
int j=random.nextInt(i+1);
if(i!=j)
{
swap(a,i,j);
}
}
return a;
}
public void swap(int a[],int x,int y)
{
int temp=a[x];
a[x]=a[y];
a[y]=temp;
}
}
8.利用集合找 倍数问题
class Solution {
public List<String> fizzBuzz(int n) {
List<String>list=new ArrayList();
for(int i=1;i<=n;i++)
{
if(i%15==0)
{
list.add("FizzBuzz");
continue;
}
if(i%3==0)
{
list.add("Fizz");
}
else if(i%5==0)
{
list.add("Buzz");
}
else
{
list.add(Integer.toString(i));
}
}
return list;
}
}