我有代码,没有Map,BiFunction和BiConsumer的引用参数化,这个代码导致Java警告.我试图添加此参数化,但这导致编译错误
Map numbers = new HashMap<>();
Map numbers2 = new HashMap();
numbers.put(1, "one");
numbers.put(2, "two");
numbers2.put(3, "three");
numbers2.put(4, "four");
BiFunction func = (k,v) -> v;
BiConsumer cons = (k,v) ->numbers.merge(k,v,func);
numbers2.forEach(cons);
Run Code Online (Sandbox Code Playgroud) 如何用一条线实现这一目标?
我目前正在尝试这样做
示例:
{{"id" :"2", values: ["10","11", "12"]} , {"id" : "3", values : ["23"]}}
Run Code Online (Sandbox Code Playgroud)
至
{{"id" :"2","value": "10"},{"id": "2","value":"11"},
{"id" :"3","value":"23"} , {"id" : "2", "value":"12"}}
Run Code Online (Sandbox Code Playgroud)
我的java代码是
Map<Integer, List<Integer>> attrMap = new HashMap<>();
//getAllData() & item.getValues() both returns List
getAllData().forEach(item - > {
item.getValues().forEach(val - > {
attrMap.computeIfAbsent(item.getId(), (k) - >
new ArrayList < > ()).add(val.getValue());
});
});
Run Code Online (Sandbox Code Playgroud)
我怎么能只做一行?
我想从某个过滤器上的列表中删除对象,并且有多个对象.
list.stream().filter(g->g.getName().equalsIgnoreCase("String")).forEach(result ->{
/* is it possible to get the index of the result here?
.remove(), will iterate through the list again. I don't want that.
*/
list.remove(result);
});
Run Code Online (Sandbox Code Playgroud) 嗨伙计们我有3个收藏,第一个我想要按名称排序,第二个用于电子邮件,然后最后一个我想要按年龄排序.我的问题是它使用集合吗?
如果我在sql中这样做
sort by name,email,age
Run Code Online (Sandbox Code Playgroud)
我知道它的工作正常吗?
我试着在这样的java中做.
Collections.sort(listPeople, new Comparator<People>() {
@Override
public int compare(final People o1, final People o2) {
return o1.getName().compareTo(o2.getName());
}
});
//then
Collections.sort(listPeople, new Comparator<People>() {
@Override
public int compare(final People o1, final People o2) {
return o1.getEmail().compareTo(o2.getEmail());
}
});
//and then
Collections.sort(listPeople, new Comparator<People>() {
@Override
public int compare(final People o1, final People o2) {
return o1.getAge().compareTo(o2.getAge());
}
});
Run Code Online (Sandbox Code Playgroud)
这行得通?或者每个集合覆盖以前的?
我正在尝试使用流来获取int数组的最小值,我正在尝试执行以下操作:
public static int smallestInt(int[] args) {
return Arrays.stream((Arrays.stream(args)
.boxed().toArray( Integer[]::new ))
.mapToInt(Integer::intValue).min().getAsInt;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是最好的方法是什么?
PS:有一个类似的问题,但这里没有sterams 使用Java查找基元数组中的最大/最小值
任何人都可以共享对LocalDateTime排序并从域对象列表中获取最大和最小记录的逻辑。
域对象
private int employeeNumber;
private LocalDateTime updatedDate;
Run Code Online (Sandbox Code Playgroud)
第一记录
1.1,2016-07-09 00:00:00+0000
2.2,2017-10-06 23:25:37+0000
Run Code Online (Sandbox Code Playgroud)
最大输出:2,2017-10-06 23:25:37 + 0000
最小输出:1,2016-07-09 00:00:00 + 0000
我的枚举
public enum ExamStausEnum {
RESULTAWAITED("Result Awaiting"),
PASSED("Passed"),
FAILED("Failed");
private String value;
ExamStausEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
通用迭代器枚举
static <E extends Enum <E>> void foo(Class<E> elemType) {
for (E e : java.util.EnumSet.allOf(elemType)) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
RESULTAWAITED
PASSED
FAILED
Run Code Online (Sandbox Code Playgroud)
如何打印构造函数值?
Result Awaiting
Passed
Failed
Run Code Online (Sandbox Code Playgroud) 我有一个旧的代码需要重新使用,它利用了大约10到15个布尔值,这些布尔值在整个类中都跳舞,就像这样:
if (condition)
{
bool1 = true
}
if (condition)
{
bool2 = true
}
...
Run Code Online (Sandbox Code Playgroud)
然后
if (bool1 == true && bool2 == true && bool3 == false)
{
do something
}
else if (bool1 == true && bool2 == false && bool3 == false)
{
do something
}
...
Run Code Online (Sandbox Code Playgroud)
是否可以避免这种编码方式?有更好的方法来实现这一点吗?也许利用地图?
我想提高可读性和整体性能,因为这段代码长于1,000s行。
反馈后添加更多具体示例:
boolean bool1 = false, bool2 = false, bool3 = false, bool4 = false, bool5 = false,
bool6 = false, bool7 = false, bool8 = false, bool9 …Run Code Online (Sandbox Code Playgroud) 给定以下代码:
@Test
public void test7() {
Map<String, Integer> sortedData = new HashMap<>();
sortedData.put("One", 1);
sortedData.put("Two", 2);
sortedData.put("Three", 3);
Stream<Map.Entry<String, Integer>> stream = sortedData.entrySet().stream();
List<String> sorted = stream
.sorted(Comparator.comparing(Map.Entry::getValue))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
它可以成功编译,但是当我更改时
.sorted(Comparator.comparing(Map.Entry::getValue))
Run Code Online (Sandbox Code Playgroud)
至
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
Run Code Online (Sandbox Code Playgroud)
编译器抱怨 Non static method can't be referenced in static context
我可以想象这是因为getValue不是的静态方法Map.Entry,但是我在这里无法解释问题。
出于好奇,如果在java8风格中使用Comparator,即使用Lambda表达式比常规比较有任何优势,即
按ID排序的一种方法是: -
List sortedAcs = ac
.stream()
.sorted((a, b) -> Long.compare(a.getId(), b.getId())) //sorted by id here
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
其他方法可能是Java 8方式: -
List sortedAcs = ac
.stream()
.sorted(Comparator.comparingLong(AC::getId)) //sorted by id here
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
后一种方法(java-8 method reference)对前一种方法有任何性能上的好处吗?
请帮忙!!!