我有一个遗留应用程序,它接受一个整数,将其转换为二进制字符串,反转该字符串,然后将位(一个)的位置作为整数列表获取。例如:
6 -> "110" -> "011" -> (2,3)
7 -> "111" -> "111" -> (1,2,3)
8 -> "1000" -> "0001" -> (4)
Run Code Online (Sandbox Code Playgroud)
在没有 String 操作的现代 Java 中,有什么简洁明了的方法来实现这一点?与 String 的转换对我来说似乎很浪费,而且我知道String.reverse()无论如何都没有简单的方法来翻转 String (no )。
使用JColorChooser时,输入的CMYK值会转换为特定的RGB颜色。在RGB侧手动输入该颜色时,CMYK值与以前不同。
以下程序可用于演示我遇到的行为。
import java.awt.*;
import javax.swing.*;
public class ColorChooserProblem {
JFrame f = new JFrame("Testing Color Chooser");
public static void main(String[] args) {
new ColorChooserProblem().start();
}
public void start() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JColorChooser jc1 = new JColorChooser();
JColorChooser jc2 = new JColorChooser();
f.add(jc1, BorderLayout.NORTH);
f.add(jc2, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,当采用另一种方式(即首先选择RGB并重新输入CMYK值)时,所有操作均符合预期。我是否在转换过程中缺少期望的内容,或者这是一个错误?
我在Windows 10上运行Java 10,而我的IDE是Eclipse。
也发布在http://www.javaprogrammingforums.com/java-theory-questions/41836-possible-bug-jcolorchooser.html
List<Integer> listOne= new ArrayList<>();
listOne.add(10);
listOne.add(2);
listOne.add(3);
//Second Array
List<Integer> listTwo= new ArrayList<>();
listTwo.add(3);
listTwo.add(7);
listTwo.add(1);
Run Code Online (Sandbox Code Playgroud)
我想比较两个 List 并指出listOne或listTwo取决于哪个值更大
[10, 2, 3] compare to [3, 7, 1]
if listOne.get(0) > listTwo.get(0) //add one point to listOne
if listOne.get(0) < listTwo.get(0) //add one point to listTwo
Run Code Online (Sandbox Code Playgroud)
这是我测试过的代码
static List<Integer> compareList(List<Integer> a, List<Integer> b) {
ArrayList<Integer> output = new ArrayList<>();
output.add(0, 0);
output.add(1, 0);
int out = output.get(0);
int out2 = output.get(1);
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我有一个清单 Employee
public class Employee {
private String name;
private Integer age;
private Double salary;
private Department department;
}
List<Employee> employeeList = Arrays.asList(
new Employee("Tom Jones", 45, 12000.00,Department.MARKETING),
new Employee("Harry Major", 26, 20000.00, Department.LEGAL),
new Employee("Ethan Hardy", 65, 30000.00, Department.LEGAL),
new Employee("Nancy Smith", 22, 15000.00, Department.MARKETING),
new Employee("Catherine Jones", 21, 18000.00, Department.HR),
new Employee("James Elliot", 58, 24000.00, Department.OPERATIONS),
new Employee("Frank Anthony", 55, 32000.00, Department.MARKETING),
new Employee("Michael Reeves", 40, 45000.00, Department.OPERATIONS));
Run Code Online (Sandbox Code Playgroud)
我想知道Map<Employee, List<Employee>>每个部门的最高工资员工的映射键在哪里,值是该部门的所有员工。我正在尝试 groupingBy 但它为所有员工提供了部门地图。如何将所有最高工资员工作为映射键?
Map<Department,List<Employee>> employeeMap
= employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment));
Run Code Online (Sandbox Code Playgroud) 目前我正在使用 for 循环来执行简单的乘法并对获得的值求和:
BigDecimal serviceOrderTotal = new BigDecimal(0.00);
for (int i = 0; i < orderPresenter.getServiceList().size(); i++) {
System.out.println("orderPresenter.getServiceList().get(i).getServiceValue()");
System.out.println(orderPresenter.getServiceList().get(i).getServiceValue());
System.out.println("orderPresenter.getServiceList().get(i).getServiceQuantity()");
System.out.println(orderPresenter.getServiceList().get(i).getServiceQuantity());
serviceOrderTotalold = serviceOrderTotal.add(orderPresenter.getServiceList().get(i).getServiceValue().multiply(orderPresenter.getServiceList().get(i).getServiceQuantity()));
}
Run Code Online (Sandbox Code Playgroud)
我需要使用流做同样的事情。对于每个 orderPresenter,我需要乘以数量值并返回这些值的总和。
forEach如果我在 中使用 a 来执行此操作stream,则无法返回该值,因为该变量必须是finalforEach 内部使用的原子变量。我最终对此无济于事:
BigDecimal serviceOrderTotal = new BigDecimal(0.00);
serviceOrderTotal = orderPresenter.getServiceList().stream()
.forEach(servicePresenter -> {
System.out.println("orderPresenter.getServiceValue()");
System.out.println(orderPresenter.getServiceValue());
System.out.println("orderPresenter.getServiceQuantity()");
System.out.println(orderPresenter.getServiceQuantity());
serviceOrderTotalold = serviceOrderTotalold.add(orderPresenter.getServiceValue().multiply(orderPresenter.getServiceQuantity()));
});
Run Code Online (Sandbox Code Playgroud) 我目前正在编程避免错误。所以我有两个 LocalDate:从 和 直到,我想检查其中之一是否是过去的。
这是我的方法。但在某个地方似乎有一个错误,因为如果我为过去的“from”选择 LocalDate,我会得到一个错误的返回。
private static boolean isPast(LocalDate from, LocalDate until) {
if (LocalDate.now().isAfter(from) || LocalDate.now().isAfter(until)) {
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我想返回一个由没有出现在任何参数编号中的数字(按升序)组成的字符串。我不知道这应该怎么做,但我想使用 long 作为比较器来找出 IntStream 中丢失的数字。
例如,如果numeros = [1201, 23045],我必须返回“6789”
我的代码:
public static String digitosQueNoEstanEn(List<Long> numeros)
{
long n = 1234567890;
IntStream numStream = numeros.stream()
.map(c -> c.toString())
.flatMapToInt(c -> c.chars())
.distinct();
Run Code Online (Sandbox Code Playgroud) 我下面有一个函数,它有一个输入日期,它将以格式返回下个月的第一个和最后一个日期MM/dd/yyyy。
String string = "01/01/2022";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date dt = sdf .parse(string);
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.MONTH, 1);
String firstDate = sdf.format(c.getTime());
System.out.println("FirstDate:" + firstDate);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
String lastDate = sdf.format(c.getTime());
System.out.println("LastDate:" + lastDate);
Run Code Online (Sandbox Code Playgroud)
上面的内容会给我如下的输出
FirstDate:02/01/2022
LastDate:02/28/2022
Run Code Online (Sandbox Code Playgroud)
如果输入是上个月的第一天,这效果很好,我想要实现的是获取下一个月的 FirstDate 和 LastDate,month即使输入的日期不是该月的第一天,例如01/31/2022给出我下面的输出
FirstDate:02/28/2022
LastDate:03/27/2022
Run Code Online (Sandbox Code Playgroud)
但我仍然希望它能给我第一个
FirstDate:02/01/2022
LastDate:02/28/2022
Run Code Online (Sandbox Code Playgroud) 我试图熟悉 Java 流以及该领域的类似事物,但无法对 type 的数组进行排序int。这是我的代码,它根本不对数组进行排序。
class Scratch {
public static void main(String[] args) {
int [] arr3 = { 9,21,5,8 };
Comparator<Integer> cc = (e1,e2) -> {
return e1.compareTo(e2);
};
Arrays.stream(arr3).boxed().sorted();
System.out.println(Arrays.toString(arr3));
}
}
Run Code Online (Sandbox Code Playgroud)