数据结构 队列 集合 链表、数组 字典、关联数组 栈 树 二叉树 完全二叉树 平衡二叉树 二叉查找树(BST) 红黑树 B,B+,B*树 LSM 树 BitSet 常用算法 排序、查找算法 选择排序 冒泡排序 插入排序 快速排序 归并排序 希尔排序 堆排序 计数排序 桶排序 基数排序 二分查找 Java 中的排序工具 布隆过滤器 字符串比较 KMP 算法 深度优先、广度优先 贪心算法 回溯算法 剪枝算法 动态规划 朴素贝叶斯 推荐算法 最小生成树算法 最短路径算法 并发 Java 并发 多线程 线程安全 一致性、事务 事务 ACID 特性 事务的隔离级别 MVCC 锁 Java中的锁和同步类 公平锁 & 非公平锁 悲观锁 乐观锁 & CAS ABA 问题 CopyOnWrite容器 RingBuffer 可重入锁 & 不可重入锁 互斥锁 & 共享锁 死锁 操作系统 计算机原理 CPU 多级缓存 进程 线程 协程 Linux 设计模式 设计模式的六大原则 23种常见设计模式 应用场景 单例模式 […]
View Details转载自:https://blog.csdn.net/iplayvs2008/article/details/41910835 java格式化时间到毫秒: SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:SSS"); String formatStr =formatter.format(new Date(); 附录: java格式化字母表 Symbol Meaning Presentation Example G era designator Text AD y year Number 2009 M month in year Text & Number July & 07 d day in month Number 10 h hour in am/pm (1-12) Number 12 H hour in day (0-23) Number 0 m minute in hour Number 30 s second in minute Number 55 S millisecond Number 978 E day in week Text Tuesday D day in year Number 189 F day of […]
View Details随机数Int的生成 生成无边界的Int
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingIntegerUnbounded() throws Exception { int intUnbounded = new Random().nextInt(); System.out.println(intUnbounded); } |
生成有边界的Int
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingIntegerBounded_withRange() throws Exception { int min = 1; int max = 10; int intBounded = min + ((int) (new Random().nextFloat() * (max - min))); System.out.println(intBounded); } |
包含1而不包含10 使用Apache Common Math来生成有边界的Int
1 2 3 4 5 6 7 8 9 |
@Test public void testRandom_generatingIntegerBounded_withApacheMath() throws Exception { int min = 1; int max = 10; int intBounded = new RandomDataGenerator().nextInt(min, max); System.out.println(intBounded); } |
包含1且包含10 使用Apache Common Lang的工具类来生成有边界的Int
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingIntegerBounded_withApacheLangInclusive() throws Exception { int min = 1; int max = 10; int intBounded = RandomUtils.nextInt(min, max); System.out.println(intBounded); } |
包含1而不包含10 使用TreadLocalRandom来生成有边界的Int
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingIntegerBounded_withThreadLocalRandom() throws Exception { int min = 1; int max = 10; int threadIntBound = ThreadLocalRandom.current().nextInt(min, max); System.out.println(threadIntBound); } |
包含1而不包含10 随机数Long的生成 生成无边界的Long
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingLongUnbounded() throws Exception { long unboundedLong = new Random().nextLong(); System.out.println(unboundedLong); } |
因为Random类使用的种子是48bits,所以nextLong不能返回所有可能的long值,long是64bits。 生成有边界的Long
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingLongBounded_withRange() throws Exception { long min = 1; long max = 10; long rangeLong = min + (((long) (new Random().nextDouble() * (max - min)))); System.out.println(rangeLong); } |
以上只会生成1到10的long类型的随机数 使用Apache Commons Math来生成有边界的Long
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingLongBounded_withApacheMath() throws Exception { long min = 1; long max = 10; long rangeLong = new RandomDataGenerator().nextLong(min, max); System.out.println(rangeLong); } |
此方式主要使用的RandomDataGenerator类提供的生成随机数的方法 使用Apache Commons Lang的工具类来生成有边界的Long
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingLongBounded_withApacheLangInclusive() throws Exception { long min = 1; long max = 10; long longBounded = RandomUtils.nextLong(min, max); System.out.println(longBounded); } |
RandomUtils提供了对java.util.Random的补充 使用ThreadLocalRandom生成有边界的Long
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingLongBounded_withThreadLocalRandom() throws Exception { long min = 1; long max = 10; long threadLongBound = ThreadLocalRandom.current().nextLong(min, max); System.out.println(threadLongBound); } |
随机数Float的生成 生成0.0-1.0之间的Float随机数
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingFloat0To1() throws Exception { float floatUnbounded = new Random().nextFloat(); System.out.println(floatUnbounded); } |
以上只会生成包含0.0而不包括1.0的float类型随机数 生成有边界的Float随机数
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingFloatBounded_withRange() throws Exception { float min = 1f; float max = 10f; float floatBounded = min + new Random().nextFloat() * (max - min); System.out.println(floatBounded); } |
使用Apache Common Math来生成有边界的Float随机数
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingFloatBounded_withApacheMath() throws Exception { float min = 1f; float max = 10f; float randomFloat = new RandomDataGenerator().getRandomGenerator().nextFloat(); float generatedFloat = min + randomFloat * (max - min); System.out.println(generatedFloat); } |
使用Apache Common Lang来生成有边界的Float随机数
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingFloatBounded_withApacheLang() throws Exception { float min = 1f; float max = 10f; float generatedFloat = RandomUtils.nextFloat(min, max); System.out.println(generatedFloat); } |
使用ThreadLocalRandom生成有边界的Float随机数 ThreadLocalRandom类没有提供 随机数Double的生成 生成0.0d-1.0d之间的Double随机数
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingDouble0To1() throws Exception { double generatorDouble = new Random().nextDouble(); System.out.println(generatorDouble); } |
与Float相同,以上方法只会生成包含0.0d而不包含1.0d的随机数 生成带有边界的Double随机数
1 2 3 4 5 6 7 8 9 10 11 12 |
@Test public void testRandom_generatingDoubleBounded_withRange() throws Exception { double min = 1.0; double max = 10.0; double boundedDouble = min + new Random().nextDouble() * (max - min); System.out.println(boundedDouble); assertThat(boundedDouble, greaterThan(min)); assertThat(boundedDouble, lessThan(max)); } |
使用Apache Common Math来生成有边界的Double随机数
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test public void testRandom_generatingDoubleBounded_withApacheMath() throws Exception { double min = 1.0; double max = 10.0; double boundedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble(); double generatorDouble = min + boundedDouble * (max - min); System.out.println(generatorDouble); assertThat(generatorDouble, greaterThan(min)); assertThat(generatorDouble, lessThan(max)); } |
使用Apache Common Lang生成有边界的Double随机数
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingDoubleBounded_withApacheLang() throws Exception { double min = 1.0; double max = 10.0; double generatedDouble = RandomUtils.nextDouble(min, max); System.out.println(generatedDouble); } |
使用ThreadLocalRandom生成有边界的Double随机数
1 2 3 4 5 6 7 8 9 10 |
@Test public void testRandom_generatingDoubleBounded_withThreadLocalRandom() throws Exception { double min = 1.0; double max = 10.0; double generatedDouble = ThreadLocalRandom.current().nextDouble(min, max); System.out.println(generatedDouble); } |
JAVA中有多少可以实现随机数的类或方法? java.util.Random 这个类提供了生成Bytes、Int、Long、Float、Double、Boolean的随机数的方法 java.util.Math.random 方法提供了生成Double随机数的方法,这个方法的内部实现也是调用了java.util.Random的nextDouble方法,只不过它对多线程进行了更好的支持,在多个线程并发时会减少每个随机数生成器的竞争 第三方工具类,如Apache Common Lang库与Apache Common Math库中提供的随机数生成类,真正使用一行代码来实现复杂的随机数生成 java.util.concurrent.ThreadLocalRandom 专为多线程并发使用的随机数生成器,使用的方法为ThreadLocalRandom.current.nextInt(),此类是在JDK1.7中提供的,并且特别适合ForkJoinTask框架,而且在这个类中直接提供了生成有边界的随机数的操作,如public int nextInt(int origin, int bound),这样也可以一行代码来实现复杂的随机数生成了。 最后的总结为单线程中使用java.util.Random类,在多线程中使用java.util.concurrent.ThreadLocalRandom类。 […]
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1); } /** * DecimalFormat转换最简便 */ public void m2() { DecimalFormat df = new DecimalFormat("#.00"); System.out.println(df.format(f)); } /** * String.format打印最简便 */ public void m3() { System.out.println(String.format("%.2f", f)); } public void m4() { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(f)); } public static void main(String[] args) { format f = new format(); f.m1(); f.m2(); f.m3(); f.m4(); } } |
from:https://www.cnblogs.com/chenrenshui/p/6128444.html
View Details
1 2 3 4 5 6 7 8 9 |
List<String> getMatchers(String regex, String source){ Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(source); List<String> list = new ArrayList<>(); while (matcher.find()) { list.add(matcher.group()); } return list; } |
from:https://blog.csdn.net/w305172521/article/details/75330661
View DetailsString -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue(); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf(i); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? 以下是答案: 第一种方法:s=i+""; //会产生两个String对象 第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象 第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常 第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象 ——————————————————————-- 1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); 2). int i = Integer.valueOf(my_str).intValue(); 注: 字串转成 Double, Float, Long 的方法大同小异. 2 如何将整数 int 转换成字串 String ? A. 有叁种方法: 1.) String s = String.valueOf(i); 2.) String s = Integer.toString(i); 3.) String s = "" + i; 注: Double, Float, Long 转成字串的方法大同小异. JAVA数据类型转换 […]
View Details这里使用Random类的nextInt()方法来生成,生成[min,max]区间的随机整数公式: Random rand=new Random(); rand.nextInt(max- min+ 1) + min; 这里封装成方法了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 求[Min,Max]区间之间的随机整数。 * @param Min 最小值 * @param Max 最大值 * @return 一个Min和Max之间的随机整数 */ public static int randomIntMinToMax(int Min, int Max) { //如果相等,直接返回,还生成个屁 if(Min==Max) { return Max; } //如果Min比Max大,交换两个的值,如果不交换下面nextInt()会出错 if(Min>Max) { Min^=Max; Max^=Min; Min^=Max; } Random rand=new Random();//nextInt()不是静态方法,不能直接用类名调用 return rand.nextInt(Max - Min + 1) + Min; } |
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package random; import java.util.Random; public class GetRandom { public static void main(String[] args) { Random rand = new Random(); int Min = 0; int Max = 9; int[] count = new int[Max - Min + 1]; for (int i = 0; i < 10000; i++) { count[randomIntMinToMax(Min, Max)]++; } System.out.println(); char percentCh = '%'; for (int i = 0; i < count.length; i++) { double percent = 100 * (double) count[i] / 10000; System.out.printf("%d生成次数:%-4d,占百分比:%.2f%c\n", i, count[i], percent, percentCh); } } /** * 求[Min,Max]区间之间的随机整数。 * * @param Min * 最小值 * @param Max * 最大值 * @return 一个Min和Max之间的随机整数 */ public static int randomIntMinToMax(int Min, int Max) { // 如果相等,直接返回,还生成个屁 if (Min == Max) { return Max; } // 如果Min比Max大,交换两个的值,如果不交换下面nextInt()会出错 if (Min > Max) { Min ^= Max; Max ^= Min; Min ^= Max; } Random rand = new Random();// nextInt()不是静态方法,不能直接用类名调用 return rand.nextInt(Max - Min + 1) + Min; } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 |
0生成次数:998 ,占百分比:9.98% 1生成次数:962 ,占百分比:9.62% 2生成次数:1005,占百分比:10.05% 3生成次数:1020,占百分比:10.20% 4生成次数:1020,占百分比:10.20% 5生成次数:997 ,占百分比:9.97% 6生成次数:966 ,占百分比:9.66% 7生成次数:1002,占百分比:10.02% 8生成次数:1006,占百分比:10.06% 9生成次数:1024,占百分比:10.24% |
from:https://blog.csdn.net/qq_21808961/article/details/80526231
View Details什么是伪随机数? 1.伪随机数是看似随机实质是固定的周期性序列,也就是有规则的随机。 2.只要这个随机数是由确定算法生成的,那就是伪随机,只能通过不断算法优化,使你的随机数更接近随机。 (随机这个属性和算法本身就是矛盾的) 3.通过真实随机事件取得的随机数才是真随机数。 Java随机数产生原理: Java的随机数产生是通过线性同余公式产生的,也就是说通过一个复杂的算法生成的。 伪随机数的不安全性: Java自带的随机数函数是很容易被黑客破解的,因为黑客可以通过获取一定长度的随机数序列来推出你的seed,然后就可以预测下一个随机数。 不用种子的不随机性会增大的原因: java.Math.Random()实际是在内部调用java.util.Random()的,使用一个和当前系统时间有关的数字作为种子数。两个随机数就很可能相同。 double a = Math.random(); double b = Math.random(); Random r1 = new Random(); r1.nextInt(10); Random r2 = new Random(); r2.nextInt(10); Java中产生随机数的方法有两种: 第一种:Math.random() 第二种:new Random() 一、java.lang.Math.Random: 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0),在该范围内(近似)均匀分布。因为返回值是double类型的,小数点后面可以保留15位小数,所以产生相同的可能性非常小,在这一定程度上是随机数。 二、java.util.Random: Random r1 = new Random(); Random r2 = new Random(); Random r3 = new Random(10); Random r4 = […]
View Details时间有关 +1s 1、获取当前毫秒数
1 |
long t1=System.currentTimeMillis(); |
2、毫秒数转换为时间
1 2 3 |
Date date2=new Date(); date2.setTime(t1); System.err.println(date2); |
3、时间格式化
1 2 3 |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); String fmDate=simpleDateFormat.format(date2); System.err.println(fmDate); |
4、字符串格式时间获取毫秒数
1 2 3 4 |
String sdate = "2018-06-01 06-06-06"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); long time = simpleDateFormat.parse(sdate).getTime(); System.err.println(time); |
5、毫秒数的计算 把两个毫秒数差值传进来就可以看见相差多久 原贴:https://blog.csdn.net/sunshinestation/article/details/4568946
1 2 3 4 5 6 7 8 |
public static String formatDuring(long mss) { long days = mss / (1000 * 60 * 60 * 24); long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); long seconds = (mss % (1000 * 60)) / 1000; return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds "; } |
6、java api提供的方法: 待续 7、时间插入数据库 先转换成yyyy-MM-dd HH:mm:ss这个格式,然后可以以字符串格式插入
1 2 3 |
Date date=new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fmDate=simpleDateFormat.format(date); |
——————— 作者:云驿 来源:CSDN 原文:https://blog.csdn.net/sinat_32238399/article/details/80512452 版权声明:本文为博主原创文章,转载请附上博文链接!
View Details