总览【Java SE】
相关文章链接
【Java应用编程_常用类库汇总1(字符串处理类&&对象克隆&数字操作类库&…)】
【Java应用编程_常用类库汇总2(日期类&正则表达式&国际化程序)】
1,Arrays 数组操作类
demo_Arrays
package cn.classes.demo;
import java.util.Arrays;
class ArraysUtil{//我们自己实现二分查找的方法
private ArraysUtil(){};
/**
* 提供静态方法
* @param data
* @param key
* @return
*/
public static int binarySearch(int data[],int key){
if(data==null){
return -1;
}
return ArraysUtil.binarySearchDefault(data,0,data.length-1 ,key);
}
/**
* 静态方法调用默认的二分查找实现方法
* @param data 要操作的数组
* @param fromIndex 起始索引
* @param toIndex 终点索引
* @param key 查找对象值
* @return 查找结果(没有返回-1,有返回对应索引值)
*/
private static int binarySearchDefault(int data[],int fromIndex ,int toIndex ,int key){
int low=fromIndex;
int high=toIndex;
while(low<=high){
int mid=(low+high)/2;//此段代码在JDK源码中采用二进制移位操作完成,更高效:int mid=(low+high)>>>1;
if(data[mid]==key){
return mid;
}else if(key<data[mid]){
high=mid-1;
}else{
low=mid+1;
}
}
return -1;
}
}
public class Arrays类 {
public static void main(String[] args) {
int dataA[]=new int[]{1,2,3};
int dataB[]=new int[]{2,3,1};
Arrays.sort(dataB);
System.out.println(Arrays.compare(dataA,dataB));//JDK1.9后引入compare方法
System.out.println(Arrays.equals(dataA, dataB));
int dataC[]=new int[10];
Arrays.fill(dataC, 6);
System.out.println(Arrays.toString(dataC));
int data[]=new int[]{1,5,7,3,2,4,6,9};
Arrays.sort(data);
System.out.println(Arrays.toString(data));
System.out.println(Arrays.binarySearch(data,5));
System.out.println(Arrays.binarySearch(data,0));
System.out.println(ArraysUtil.binarySearch(data, 5));
}
}
运行结果
0
true
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
[1, 2, 3, 4, 5, 6, 7, 9]
4
-1
4
Process finished with exit code 0
2,UUID 无重复数据生成类
demo_UUID
package cn.classes.demo;
import java.util.UUID;
public class UUID类 {
public static void main(String[] args) {
UUID uuid =UUID.randomUUID();
System.out.println(uuid);
}
}
运行结果(不唯一)
bd8489a6-25e4-4192-9be4-6c66bde4e687
Process finished with exit code 0
3,Optional 空处理类
demo_Optional
package cn.classes.demo;
import java.util.Optional;
public class Optional类 {
public static void main(String[] args) {
Optional<MessageImple> opt=MessageUtil.getMessage();
if(opt.isPresent()){
IMessageThis msg=opt.get();
MessageUtil.useMessage(msg);
}
}
}
class MessageUtil{
private MessageUtil(){};
public static Optional<MessageImple> getMessage(){
return Optional.of(new MessageImple());
}
public static void useMessage(IMessageThis msg){
System.out.println(msg.getContent());
}
}
interface IMessageThis {
public String getContent();
}
class MessageImple implements IMessageThis{
@Override
public String getContent() {
return "hello";
}
}
运行结果
hello
Process finished with exit code 0
4,ThreadLocal 类
demo_ThreadLocal
package cn.classes.demo;
public class ThreadLocal类 {
public static void main(String[] args) {
Message_TL mess=new Message_TL();//同一对象
new Thread(()->{
mess.setInfo("hello");
Channel.setMessage_TL(mess);
Channel.send();
},"消息发送者A").start();
new Thread(()->{
mess.setInfo("dear");
Channel.setMessage_TL(mess);
Channel.send();
},"消息发送者B").start();
new Thread(()->{
mess.setInfo("world!");
Channel.setMessage_TL(mess);
Channel.send();
},"消息发送者C").start();
}
}
class Channel{
private static final ThreadLocal<Message_TL> THREADLOCAL=new ThreadLocal<Message_TL>();
private Channel (){}
public static void setMessage_TL(Message_TL m){
THREADLOCAL.set(m);
}
public static void send(){
System.out.println("["+Thread.currentThread().getName()+"]"+THREADLOCAL.get().getInfo());
}
}
class Message_TL{
private String info;
public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
}
运行结果
[消息发送者A]dear
[消息发送者C]world!
[消息发送者B]dear
Process finished with exit code 0
5,定时调度类
schedule方法和scheduleAtFixedRate方法的区别:scheduleAtFixedRate具有追赶性,而schedule不具有追赶性。【schedule方法和scheduleAtFixedRate方法的区别】
demo_Timer与TimerTask
package cn.classes.demo;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
class MyTask extends TimerTask{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+",定时任务,当前时间:"+System.currentTimeMillis());
}
}
public class Timer类 {
public static void main(String[] args) {
Timer timer=new Timer();
timer.schedule(new MyTask(),100);//延迟0.1s后执行MyTask线程任务
timer.schedule(new MyTask(), new Date());//在当前时间时执行MyTask线程任务
//timer.schedule(new MyTask(), 1000, 100);//在延迟1s后以0.1s为间隔执行MyTask线程任务
timer.scheduleAtFixedRate(new MyTask(), 1000, 100);//在延迟1s后以0.1s为间隔执行MyTask线程任务
}
}
运行结果(不唯一)
Timer-0,定时任务,当前时间:1592618640042
Timer-0,定时任务,当前时间:1592618640141
Timer-0,定时任务,当前时间:1592618641041
Timer-0,定时任务,当前时间:1592618641142
Timer-0,定时任务,当前时间:1592618641242
...
Process finished with exit code -1
6,Base64 加密与解密
demo_Base64
package cn.classes.demo;
import java.util.Base64;
public class Base64类 {
public static void main(String[] args) {
String str=StringUtil.encode("hello");
System.out.println(str);
System.out.println(StringUtil.decode(str));
}
}
class StringUtil{
private StringUtil(){}
private static final String SALT="加密盐值";
private static final int REPEAT=4;//加密次数
public static String encode(String str){
String temp=str+"{"+SALT+"}";
byte data[]=temp.getBytes();
for (int i = 0; i < REPEAT; i++) {
data=Base64.getEncoder().encode(data);
}
return new String(data);
}
public static String decode(String str){
byte data[]=str.getBytes();
for (int i = 0; i < REPEAT; i++) {
data=Base64.getDecoder().decode(data);
}
return new String(data).replaceAll("\\{.+\\}", "");
}
}
运行结果
V1ZWa1YyTXlTa2hQVkdNeFYxaEdiazVYUlhKU2VsVXhaRlpGTVZkVlRUUmFiRVU1VUZFOVBRPT0=
hello
Process finished with exit code 0
7,比较器
demo_Comparable
package cn.classes.demo;
import java.util.Arrays;
class Person_CP implements Comparable<Person_CP>{
private String name;
private int age;
public Person_CP(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person_CP men){
return this.age-men.age;
}
@Override
public String toString() {
return "Person_CP [name=" + name + ", age=" + age + "]";
}
}
public class Comparable比较器 {
public static void main(String[] args) {
Person_CP per[]=new Person_CP[]{
new Person_CP("小A",28),
new Person_CP("小B",18),
new Person_CP("小C",58),
};
Arrays.sort(per);
System.out.println(Arrays.toString(per));
}
}
运行结果
[Person_CP [name=小B, age=18], Person_CP [name=小A, age=28], Person_CP [name=小C, age=58]]
Process finished with exit code 0
demo_Comparator
package cn.classes.demo;
import java.util.Arrays;
import java.util.Comparator;
class Person_CPOcomparator implements Comparator<Person_CPO>{
@Override
public int compare(Person_CPO o1, Person_CPO o2) {
return o1.getAge()-o2.getAge();
}
}
class Person_CPO {
private String name;
private int age;
public Person_CPO(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person_CP [name=" + name + ", age=" + age + "]";
}
public int getAge() {
return age;
}
}
public class Comparator比较器 {
public static void main(String[] args) {
Person_CPO per[]=new Person_CPO[]{
new Person_CPO("小A",29),
new Person_CPO("小B",19),
new Person_CPO("小C",99),
};
Arrays.sort(per, new Person_CPOcomparator());
System.out.println(Arrays.toString(per));
}
}
运行结果
[Person_CP [name=小B, age=19], Person_CP [name=小A, age=29], Person_CP [name=小C, age=99]]
Process finished with exit code 0
demo_简易二叉树
package cn.classes.demo;
import java.util.Arrays;
class BinaryTree<T extends Comparable<T>>{//设置泛型上限,保存的数据一定是实现了Comparable的类对象
private class Node{//Node的细节操作由本类私有
private Comparable<T> data;//向上转型,方便接收数据;
private Node parent;//保存父节点
private Node left;//保存左子节点
private Node right;//保存右子节点
public Node(Comparable<T> data){
this.data=data;
}
/**
* Node的增加数据操作
* @param newNode 要添加的对象
* 递归添加
*/
public void addNode(Node newNode){
if(newNode.data.compareTo((T) this.data)<=0){
if(this.left==null){
this.left=newNode;
}else{
this.left.addNode(newNode);
}
}else{
if(this.right==null){
this.right=newNode;
}else{
this.right.addNode(newNode);
}
}
}
public void toArraysNode(){
if(this.left!=null){
this.left.toArraysNode();
}
BinaryTree.this.returnDate[BinaryTree.this.foot++]=this.data;
if(this.right!=null){
this.right.toArraysNode();
}
}
}
//--------------------二叉树功能实现---------------------
private Node root;
private int count;
private Object[] returnDate;
private int foot;
/**
* 二叉树类的添加操作
* @param data 要添加的数据
*/
public void add(Comparable<T> data){
if(data==null){
throw new NullPointerException("保存的数据不允许为空");
}
Node newNode=new Node(data);
if(this.root==null){
this.root=newNode;
}else{
this.root.addNode(newNode);
}
this.count++;
}
/**
* 二叉树的数据输出操作
* @return 数据以 object数组输出
*/
public Object[] toArray(){
if(this.count==0){
return null;
}
this.returnDate=new Object[this.count];
this.foot=0;
this.root.toArraysNode();
return this.returnDate;
}
}
class Member_bin implements Comparable<Member_bin>{
private String name;
private int age;
public Member_bin(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Member_bin o) {
return this.age-o.age;
}
@Override
public String toString() {
return "Member [name=" + name + ", age=" + age + "]";
}
}
public class 二叉树实现 {
public static void main(String[] args) {
BinaryTree <Member_bin>tree=new BinaryTree<Member_bin>();
tree.add(new Member_bin("小A",27));
tree.add(new Member_bin("小B",67));
tree.add(new Member_bin("小C",7));
System.out.println(Arrays.toString(tree.toArray()));
}
}
运行结果
[Member [name=小C, age=7], Member [name=小A, age=27], Member [name=小B, age=67]]
Process finished with exit code 0
本文完,欢迎访问或关注我的【Java SE】专栏。
转载:https://blog.csdn.net/qq_17852541/article/details/106852814
查看评论