Stream是一个数据流,是用来传输数据的,它自己不存储数据。
Stream只能使用一次。
流的创建
查看流中的元素数量
stream.count();
遍历流中的元素
strStream.forEach(System.out::println);
排序,调用sorted方法,生成另一个排好序的流。
使用distinct()方法获取另一个去重之后的流。
public class TestStream {
@Test
public void testPerson() {
List<Person> persons = new ArrayList<>();
List<String> infos = new ArrayList<>();
infos.add("1-qq-12");
infos.add("2-qdq-52");
infos.add("3-qqvfddv-12");
infos.add("4-qqv-22");
// 把流中的所有字符转为person对象
infos.stream().map((info) -> {
String[] i = info.split("-");
return new Person(i[0], i[1], i[2]);
}).forEach((p) -> persons.add(p));
System.out.println(persons);
}
@Test
public void textStream5() {
List<String> list = Arrays.asList("aa", "DD", "CC", "dd");
Predicate<String> p = (str) -> !str.equals(str.toUpperCase());
// 过滤流中元素,生成新的流
list.stream().filter(p).forEach(System.out::println);
// limit限制流中元素的数量
list.stream().limit(3).forEach(System.out::println);
list.stream().skip(2).limit(2).forEach(System.out::println);
}
@Test
public void textStream4() {
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
// 使用distinct去重
// list.stream().distinct().forEach(System.out::println);
// //取出最小元素
// String s=list.stream().min((a,b)->a.compareTo(b)).get();
// System.out.println(s);
Function<String, String> f = (str) -> str.toUpperCase();
list.stream().map(f);
}
@Test
// 排序调用sorted方法
public void textStream3() {
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
Stream<String> stream = list.stream();
// 获取一个排序的流
String s = list.stream().sorted((s1, s2) -> s2.compareTo(s1)).findFirst().get();
stream.sorted().forEach((st) -> {
System.out.print(st);
if (!s.equals(st)) {
System.out.print("-");
}
});
}
@Test
public void textStream2() {
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
Stream<String> strStream = list.stream();// 只能用一次
// 查看Stream中的元素数量
// System.out.println(strStream.count());
// 遍历Stream中的元素
// strStream.forEach(()->{});
Optional<String> optional = strStream.findFirst();// 取出第一个元素
String frist = optional.get();
System.out.println(frist);
System.out.println(list);
}
@Test
public void textStream1() {
// 流的创建,用来传输数据的。自己不存储数据
Stream<String> strStream = Stream.of("aa", "bb", "cc", "dd");
// 使用集合创建流
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
Stream<String> strStream2 = list.stream();
// 创建无限流
Supplier<Integer> s = () -> {
Random ran = new Random();
return ran.nextInt(50000);
};
Stream<Integer> intStream = Stream.generate(s);
}
//
}
2.
JAVA界面设计
public class TestFrame extends JFrame {
static List<Person> ser = new ArrayList<>();
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JLabel label_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 459, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("用户:");
label.setBounds(51, 30, 54, 15);
contentPane.add(label);
textField = new JTextField();
textField.setBounds(104, 27, 104, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("密码");
label_1.setBounds(51, 67, 40, 15);
contentPane.add(label_1);
passwordField = new JPasswordField();
passwordField.setBounds(104, 68, 104, 19);
contentPane.add(passwordField);
label_2 = new JLabel("性别");
label_2.setBounds(49, 101, 54, 15);
contentPane.add(label_2);
ButtonGroup p = new ButtonGroup();
JRadioButton radioButton = new JRadioButton("男");
radioButton.setBounds(104, 97, 40, 23);
contentPane.add(radioButton);
p.add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("女");
radioButton_1.setBounds(146, 97, 45, 23);
contentPane.add(radioButton_1);
p.add(radioButton_1);
JLabel label_3 = new JLabel("爱好");
label_3.setBounds(51, 133, 40, 15);
contentPane.add(label_3);
List<JCheckBox> info = new ArrayList<>();
JCheckBox checkBox = new JCheckBox("听歌");
checkBox.setBounds(105, 129, 54, 23);
contentPane.add(checkBox);
info.add(checkBox);
JCheckBox checkBox_1 = new JCheckBox("游戏");
checkBox_1.setBounds(156, 129, 54, 23);
contentPane.add(checkBox_1);
info.add(checkBox_1);
JCheckBox checkBox_2 = new JCheckBox("看书");
checkBox_2.setBounds(209, 129, 54, 23);
contentPane.add(checkBox_2);
info.add(checkBox_2);
JCheckBox checkBox_3 = new JCheckBox("看片");
checkBox_3.setBounds(264, 129, 54, 23);
contentPane.add(checkBox_3);
info.add(checkBox_3);
JLabel label_5 = new JLabel("出生年月");
label_5.setBounds(48, 173, 65, 15);
contentPane.add(label_5);
JComboBox comboBox = new JComboBox();
for (int i = 1970; i <= 2020; i++) {
comboBox.addItem(i);
}
comboBox.setBounds(112, 170, 79, 21);
contentPane.add(comboBox);
JComboBox comboBox_1 = new JComboBox();
for (int i = 1; i <= 12; i++) {
comboBox_1.addItem(i);
}
comboBox_1.setBounds(201, 170, 49, 21);
contentPane.add(comboBox_1);
JComboBox comboBox_2 = new JComboBox();
for (int i = 1; i <= 31; i++) {
comboBox_2.addItem(i);
}
comboBox_2.setBounds(264, 170, 65, 21);
contentPane.add(comboBox_2);
JLabel label_4 = new JLabel("自我介绍");
label_4.setBounds(44, 224, 65, 15);
contentPane.add(label_4);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(112, 234, 239, 141);
contentPane.add(scrollPane);
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane.setViewportView(textArea);
JButton button = new JButton("提交");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = textField.getText();
String ag = new String(passwordField.getPassword());
String s = null;
Enumeration<AbstractButton> elemeats = p.getElements();
while (elemeats.hasMoreElements()) {
AbstractButton ab = elemeats.nextElement();
JRadioButton jro = (JRadioButton) ab;
if (jro.isSelected()) {
s = jro.getText();
break;
}
}
List<String> myinof = new ArrayList<>();
for (JCheckBox jc : info) {
if (jc.isSelected()) {
myinof.add(jc.getText());
}
}
int S1 = (Integer) comboBox.getSelectedItem();
int S2 = (Integer) comboBox_1.getSelectedItem();
int S3 = (Integer) comboBox_2.getSelectedItem();
LocalDate of = LocalDate.of(S1, S2, S3);
String s4 = textArea.getText();
Person p = new Person(name, ag, s, myinof, of, s4);
System.out.println(p);
}
});
button.setBounds(98, 433, 93, 23);
contentPane.add(button);
}
}