parent
45ec292663
commit
e40fbd2baf
@ -1,104 +1,115 @@ |
||||
//package com.shkj;
|
||||
//
|
||||
//
|
||||
//import io.swagger.models.auth.In;
|
||||
//import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
//import org.apache.commons.lang3.tuple.Pair;
|
||||
//import org.apache.poi.ss.formula.functions.T;
|
||||
//import org.junit.Test;
|
||||
//
|
||||
//import java.util.*;
|
||||
//import java.util.stream.Collectors;
|
||||
//
|
||||
//public class testB {
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// public class WeightRandomStrategy<k, V extends Number> {
|
||||
// private TreeMap<Double, k> weightMap = new TreeMap<>();
|
||||
// private Map<String, Integer> map =new HashMap();
|
||||
// private Map nmap =new HashMap();
|
||||
// private TreeMap<Double,k> Map = new TreeMap<>();
|
||||
//
|
||||
// public WeightRandomStrategy(List<Pair<k, V>> list) {
|
||||
// for (Pair<k, V> pair : list) {
|
||||
// nmap.put(pair.getKey(),pair.getValue());
|
||||
// map.put(String.valueOf(pair.getKey()),0);
|
||||
// double lastWeight = this.weightMap.size() == 0 ? 0 : this.weightMap.lastKey();
|
||||
// this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public java.lang.String randome(){
|
||||
// Map.putAll(weightMap);
|
||||
// String k = random();
|
||||
// return k;
|
||||
// }
|
||||
//
|
||||
// public String random() {
|
||||
//
|
||||
// Double aDouble = this.Map.lastKey();
|
||||
// double random = Math.random();
|
||||
// double randomWeight = aDouble* random;
|
||||
// SortedMap<Double, k> tailMap = this.Map.tailMap(randomWeight, false);
|
||||
// String k = String.valueOf(this.Map.get(tailMap.firstKey()));
|
||||
// int o = (Integer) this.nmap.get(k);
|
||||
// int o2 = (Integer)map.get(k);
|
||||
// if (o2==o){
|
||||
// System.out.println("========"+k);
|
||||
// Map.remove(tailMap.firstKey());
|
||||
//
|
||||
// if (Map.size()==0){
|
||||
// List collect = map.entrySet().stream().map(java.util.Map.Entry::getKey).collect(Collectors.toList());
|
||||
// collect.stream().forEach(e->{
|
||||
// map.put(String.valueOf(e),0);
|
||||
// });
|
||||
// Map.putAll(weightMap);
|
||||
// }
|
||||
// k=random();
|
||||
// return k;
|
||||
// }
|
||||
// Integer o1 = (Integer) map.get(k);
|
||||
// map.put(k,o1+1);
|
||||
// return k;
|
||||
// }
|
||||
// }
|
||||
// @Test
|
||||
// public void testb(){
|
||||
// List<Pair<String, Integer>> list = new ArrayList<>();
|
||||
// list.add(new ImmutablePair<>("RKu", 2));
|
||||
// list.add(new ImmutablePair<>("Cku", 2));
|
||||
// list.add(new ImmutablePair<>("CKong", 1));
|
||||
// list.add(new ImmutablePair<>("YChang", 1));
|
||||
// list.add(new ImmutablePair<>("HLiu", 1));
|
||||
// list.add(new ImmutablePair<>("JinCK", 1));
|
||||
// list.add(new ImmutablePair<>("TE",3));
|
||||
// WeightRandomStrategy<String, Integer> strategy = new WeightRandomStrategy<>(list);
|
||||
// int a = 0, b = 0,c = 0,d = 0,e = 0,f =0,g = 0;
|
||||
// for (int i = 0; i < 10014; i++) {
|
||||
// String randome = strategy.randome();
|
||||
// System.out.println(randome);
|
||||
// switch (randome){
|
||||
// case "RKu": a++;break;
|
||||
// case "Cku":b++;break;
|
||||
// case "CKong":c++;break;
|
||||
// case "YChang":d++;break;
|
||||
// case "HLiu":e++;break;
|
||||
// case "JinCK":f++;break;
|
||||
// case "TE":g++;break;
|
||||
// }
|
||||
// }
|
||||
// System.out.println("a=" + a + ", b=" + b+", c=" + c+", d=" + d+", e=" + e+", f=" + f+", g=" + g);
|
||||
// System.out.println("a+b=" + (a + b+c+d+e+f+g));
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void test01(){
|
||||
// List list = new ArrayList();
|
||||
// System.out.println(list.size());
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
package com.shkj; |
||||
|
||||
|
||||
import com.shkj.wms.service.ISysAppVersionService; |
||||
import io.swagger.models.auth.In; |
||||
import org.apache.commons.lang3.tuple.ImmutablePair; |
||||
import org.apache.commons.lang3.tuple.Pair; |
||||
import org.apache.poi.ss.formula.functions.T; |
||||
import org.junit.Test; |
||||
|
||||
import java.text.SimpleDateFormat; |
||||
import java.util.*; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class testB { |
||||
|
||||
|
||||
|
||||
|
||||
public class WeightRandomStrategy<k, V extends Number> { |
||||
private TreeMap<Double, k> weightMap = new TreeMap<>(); |
||||
private Map<String, Integer> map =new HashMap(); |
||||
private Map nmap =new HashMap(); |
||||
private TreeMap<Double,k> Map = new TreeMap<>(); |
||||
|
||||
public WeightRandomStrategy(List<Pair<k, V>> list) { |
||||
for (Pair<k, V> pair : list) { |
||||
nmap.put(pair.getKey(),pair.getValue()); |
||||
map.put(String.valueOf(pair.getKey()),0); |
||||
double lastWeight = this.weightMap.size() == 0 ? 0 : this.weightMap.lastKey(); |
||||
this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey()); |
||||
} |
||||
} |
||||
|
||||
public java.lang.String randome(){ |
||||
Map.putAll(weightMap); |
||||
String k = random(); |
||||
return k; |
||||
} |
||||
|
||||
public String random() { |
||||
|
||||
Double aDouble = this.Map.lastKey(); |
||||
double random = Math.random(); |
||||
double randomWeight = aDouble* random; |
||||
SortedMap<Double, k> tailMap = this.Map.tailMap(randomWeight, false); |
||||
String k = String.valueOf(this.Map.get(tailMap.firstKey())); |
||||
int o = (Integer) this.nmap.get(k); |
||||
int o2 = (Integer)map.get(k); |
||||
if (o2==o){ |
||||
System.out.println("========"+k); |
||||
Map.remove(tailMap.firstKey()); |
||||
|
||||
if (Map.size()==0){ |
||||
List collect = map.entrySet().stream().map(java.util.Map.Entry::getKey).collect(Collectors.toList()); |
||||
collect.stream().forEach(e->{ |
||||
map.put(String.valueOf(e),0); |
||||
}); |
||||
Map.putAll(weightMap); |
||||
} |
||||
k=random(); |
||||
return k; |
||||
} |
||||
Integer o1 = (Integer) map.get(k); |
||||
map.put(k,o1+1); |
||||
return k; |
||||
} |
||||
} |
||||
@Test |
||||
public void testb(){ |
||||
List<Pair<String, Integer>> list = new ArrayList<>(); |
||||
list.add(new ImmutablePair<>("RKu", 2)); |
||||
list.add(new ImmutablePair<>("Cku", 2)); |
||||
list.add(new ImmutablePair<>("CKong", 1)); |
||||
list.add(new ImmutablePair<>("YChang", 1)); |
||||
list.add(new ImmutablePair<>("HLiu", 1)); |
||||
list.add(new ImmutablePair<>("JinCK", 1)); |
||||
list.add(new ImmutablePair<>("TE",3)); |
||||
WeightRandomStrategy<String, Integer> strategy = new WeightRandomStrategy<>(list); |
||||
int a = 0, b = 0,c = 0,d = 0,e = 0,f =0,g = 0; |
||||
for (int i = 0; i < 10014; i++) { |
||||
String randome = strategy.randome(); |
||||
System.out.println(randome); |
||||
switch (randome){ |
||||
case "RKu": a++;break; |
||||
case "Cku":b++;break; |
||||
case "CKong":c++;break; |
||||
case "YChang":d++;break; |
||||
case "HLiu":e++;break; |
||||
case "JinCK":f++;break; |
||||
case "TE":g++;break; |
||||
} |
||||
} |
||||
System.out.println("a=" + a + ", b=" + b+", c=" + c+", d=" + d+", e=" + e+", f=" + f+", g=" + g); |
||||
System.out.println("a+b=" + (a + b+c+d+e+f+g)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void test01(){ |
||||
List list = new ArrayList(); |
||||
System.out.println(list.size()); |
||||
} |
||||
|
||||
@Test |
||||
public void tees02(){ |
||||
|
||||
Calendar c = Calendar.getInstance(); |
||||
c.setTime(new Date()); |
||||
c.add(Calendar.DATE, 98); |
||||
System.out.println("获取的新的时间:"+c.getTime()); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
Loading…
Reference in new issue