
Java点滴
FlowingRiver
这个作者很懒,什么都没留下…
展开
-
Java HashMap按照value值排序的方法
HashMap的value值没有排序功能,若要进行较轻松的排序,可改写Comparator接口方法compare进行排序,代码如下:Map<String, Integer> map = new HashMap<String, Integer>();map.put("d", 2);map.put("c", 1);map.put("b", 1);map.put("a", 3);List<Map.Entry<String, Integer>> infoI转载 2021-11-23 16:00:29 · 3863 阅读 · 0 评论 -
An attempt was made to call a method that does not exist.
本地运行没问题的应用,打包后放到tomcat运行出现以下错误:大致意思就是com.google.common.collect.FluentIterable.toList()方法不存在,而FluentIterable这个类可在phoenix-client-4.14.1-HBASE-1.3.jar和guava-20.0.jar中找到。判断是依赖冲突问题。用mvn dependency:tree > tree.log命令查看依赖树。发现是springfox-swagger2用到了guava:20.原创 2021-08-23 18:15:40 · 645 阅读 · 0 评论 -
java 打印数组
常有这样的需求,要查看数组内容,打印出来却是一个如I@19469ea2这样的引用。笨方法就是遍历打印,但可以用Arrays.toString()方法直接转化成字符串打印。例: int[] array = new int[]{1, 2, 3}; System.out.println(Arrays.toString(array));结果:[1, 2, 3]对于多为数组,发现这样调用打印出的还是引用: int[][] array = new int[]原创 2021-03-02 11:00:27 · 283 阅读 · 0 评论 -
java 上传文件到ftp服务器
使用apache的commons-net库。导入依赖 <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.7.2</version> </dependency>Ftp工具类原创 2020-12-09 16:35:15 · 276 阅读 · 1 评论 -
removeIf方法
有这样的需求:从列表中筛选出符合条件的保留,不符合的删除。不能直接遍历删除,可以用迭代器删除,可见这里。但jdk1.8为我们提供了更简便的方式,就是Collection接口提供的default boolean removeIf(Predicate<? super E> filter)方法。举个????:有学生类:class Student{ String name; double score; // 分数 ...}有列表:List<Student> s原创 2020-12-01 11:03:24 · 1261 阅读 · 0 评论 -
非spring组件中使用autowired(避免空指针异常)
有类SpringComponent,属于spring组件:@Componentclass SpringComponent{ void xxMethod();}现有一个需要new的类NonSpringComponent,其中要用到SpringComponent的实例。如果直接@Autowired引入,会报空指针异常。class NonSpringComponent{ @Autowired SpringComponent springComponent; void yyMethod(原创 2020-11-20 16:07:21 · 1407 阅读 · 0 评论 -
java追加写文件
想要在文件中追加内容,使用BufferWriter的append方法写。 bufferedWriter.append(thingsToWrite);发现每次不是在文件后追加内容,而是清空后再写。原因在于没有指定打开文件方式位追加,把FileWriter的append参数指定位true即可,如下:BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));//第二个参数表示以append模式打开文件原创 2020-11-20 14:21:29 · 2790 阅读 · 0 评论 -
手写HashMap
手写个简单的HashMap,加入打印函数,可以查看HashMap内部存储结构。以下为代码:public class MyHashMap<K, V> { int cap = 1 << 4; int size = 0; double doubleFactor = 0.75; @SuppressWarnings({"unchecked"}) Node<K, V>[] array = new Node[cap]; voi原创 2020-09-21 15:22:45 · 239 阅读 · 1 评论 -
java时间增加/减少
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); Date newDate = calendar.getTime(); System.out.println("当前时间:"+sdf.format(n...原创 2020-09-08 09:29:18 · 696 阅读 · 0 评论 -
springboot使用Quartz
引入 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>定时任务新建任务类DailyCalcTask:public class DailyCalcTask e原创 2020-08-21 16:09:10 · 221 阅读 · 0 评论 -
Gson反序列化自定义类列表
有某自定义类Aclass,用Gson反序列化,只需:Gson gson = new Gson();Aclass a = gson.fromJson(jsonString, Aclass.class);而要反序列化得到一个List<Aclass>,则需要:Type type = new TypeToken<List<Aclass>>(){}.getType();Gson gson = new Gson();List<Aclass> aList= g原创 2020-08-10 10:31:21 · 770 阅读 · 0 评论 -
java读取文件内容为字符串
Java 11 - Files.readString()java 11 提供了readString()方法,只需一行就能读取文件内容为字符串。import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path; public class WriteToFile { public static void main(String[] args) throws IOException {翻译 2020-07-30 14:10:09 · 1685 阅读 · 0 评论 -
封装Gson类静态调用
1.引入包 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>2.封装类public class GsonUtil原创 2020-07-29 15:51:08 · 695 阅读 · 0 评论 -
java 与mysql数据类型映射
MySQLJavaCHARStringVARCHARStringLONGVARCHARStringNUMERICjava.math.BigDecimalDECIMALjava.math.BigDecimalBITbooleanTINYINTbyteSMALLINTshortINTEGERintBIGINTlongREALfloatFLOATdoubleDOUBLEdoubleBINAR...原创 2020-07-10 11:29:02 · 978 阅读 · 0 评论 -
springboot jar包转war包
1.POM添加打包方式:<packaging>war</packaging>2.POM添加依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope&g转载 2020-06-17 16:38:51 · 283 阅读 · 0 评论 -
spring静态字段注入
对于用static修饰的静态字段,直接用@Value("${xxx}")无法注入,需要以下几步:1.在类上添加@Component注解2.字段需要有set方法3.在set方法上使用@Value("${xxx}")例子:@Componentpublic class Sender { public static String filePath; @Value("${filePath}") public void setFilePath(String file_path)原创 2020-06-11 09:41:20 · 833 阅读 · 0 评论