List of usage examples for java.util Optional isPresent
public boolean isPresent()
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "Static"); Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst(); if (firstZ.isPresent()) { System.out.println(firstZ.get()); } else {//w ww . j av a 2s.c o m System.out.println("No Z's found"); } }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve"); Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst(); System.out.println(firstS.orElse("None found")); Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst(); if (firstZ.isPresent()) { System.out.println(firstZ.get()); } else {//w ww . j a va 2 s . c om System.out.println("No Z's found"); } }
From source file:Main.java
public static void main(String[] args) { Optional<Integer> value = Optional.empty(); System.out.println(value.orElse(42)); System.out.println(value.isPresent()); System.out.println(value.get()); }
From source file:Main.java
public static void main(String[] args) { Optional<String> value = Optional.of("some value"); System.out.println(value.isPresent()); System.out.println(value.get()); String str = null;// w w w . j av a 2s. c o m // Optional.of(str); Optional<Integer> o = Optional.empty(); System.out.println(o.isPresent()); System.out.println(o.orElse(42)); List<Integer> results = new ArrayList<>(); Optional<Integer> second = Optional.of(3); second.ifPresent(results::add); // must operate via side-effects, // unfortunately... System.out.println(results); o = Optional.empty(); System.out.println(o.orElse(42)); o = Optional.of(42); System.out.println(o.get()); o = Optional.empty(); o.get(); }
From source file:Main.java
public static void main(String[] args) { List<Employee> persons = Employee.persons(); // Find any male Optional<Employee> anyMale = persons.stream().filter(Employee::isMale).findAny(); if (anyMale.isPresent()) { System.out.println("Any male: " + anyMale.get()); } else {//w w w.ja va2 s . c o m System.out.println("No male found."); } // Find the first male Optional<Employee> firstMale = persons.stream().filter(Employee::isMale).findFirst(); if (firstMale.isPresent()) { System.out.println("First male: " + anyMale.get()); } else { System.out.println("No male found."); } }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("XML", "HTML", "CSS", "Javascript", "Java", "Static"); Optional<String> longestName = names.stream() .reduce((name1, name2) -> name1.length() >= name2.length() ? name1 : name2); if (longestName.isPresent()) { System.out.println(longestName.get()); } else {// ww w . j a va 2 s . com System.out.println("WTF?"); } }
From source file:Main.java
public static void main(String[] args) { Optional<Employee> person = Employee.persons().stream() .max(Comparator.comparingDouble(Employee::getIncome)); if (person.isPresent()) { System.out.println("Highest earner: " + person.get()); } else {//w w w .ja va 2 s. c o m System.out.println("Could not get the highest earner."); } }
From source file:Main.java
public static void main(String[] args) { Optional<Employee> person = Employee.persons().stream() .reduce((p1, p2) -> p1.getIncome() > p2.getIncome() ? p1 : p2); if (person.isPresent()) { System.out.println("Highest earner: " + person.get()); } else {//from w w w .j ava 2 s .c om System.out.println("Could not get the highest earner."); } }
From source file:com.ethercamp.harmony.Application.java
/** * Does one of:/* ww w . j a va 2 s .com*/ * - start Harmony peer; * - perform action and exit on completion. */ public static void main(String[] args) throws Exception { final List<String> actions = asList("importBlocks"); final Optional<String> foundAction = asList(args).stream().filter(arg -> actions.contains(arg)).findFirst(); if (foundAction.isPresent()) { foundAction.ifPresent(action -> System.out.println("Performing action: " + action)); Start.main(args); // system is expected to exit after action performed } else { if (!SystemProperties.getDefault().blocksLoader().equals("")) { SystemProperties.getDefault().setSyncEnabled(false); SystemProperties.getDefault().setDiscoveryEnabled(false); } ConfigurableApplicationContext context = SpringApplication.run(new Object[] { Application.class }, args); Ethereum ethereum = context.getBean(Ethereum.class); if (!SystemProperties.getDefault().blocksLoader().equals("")) { ethereum.getBlockLoader().loadBlocks(); } } }
From source file:edu.jhu.hlt.concrete.ingesters.gigaword.GigawordGzProcessor.java
public static void main(String... args) { Thread.setDefaultUncaughtExceptionHandler(new LoggedUncaughtExceptionHandler()); if (args.length != 2) { LOGGER.info("This program takes 2 arguments."); LOGGER.info("First: the path to a .gz file that is part of the English Gigaword v5 corpus."); LOGGER.info("Second: the path to the output file (a .tar.gz with communication files)."); LOGGER.info("Example usage:"); LOGGER.info("{} {} {}", GigawordGzProcessor.class.getName(), "/path/to/LDC/sgml/.gz", "/path/to/out.tar.gz"); System.exit(1);// w ww . j a va 2 s. c o m } String inPathStr = args[0]; String outPathStr = args[1]; Path inPath = Paths.get(inPathStr); if (!Files.exists(inPath)) LOGGER.error("Input path {} does not exist. Try again with the right path.", inPath.toString()); Path outPath = Paths.get(outPathStr); Optional<Path> parent = Optional.ofNullable(outPath.getParent()); // lambda does not allow caught exceptions. if (parent.isPresent()) { if (!Files.exists(outPath.getParent())) { LOGGER.info("Attempting to create output directory: {}", outPath.toString()); try { Files.createDirectories(outPath); } catch (IOException e) { LOGGER.error("Caught exception creating output directory.", e); } } } GigawordDocumentConverter conv = new GigawordDocumentConverter(); Iterator<Communication> iter = conv.gzToStringIterator(inPath); try (OutputStream os = Files.newOutputStream(outPath); BufferedOutputStream bos = new BufferedOutputStream(os, 1024 * 8 * 16); GzipCompressorOutputStream gout = new GzipCompressorOutputStream(bos); TarArchiver archiver = new TarArchiver(gout);) { while (iter.hasNext()) { Communication c = iter.next(); LOGGER.info("Adding Communication {} [UUID: {}] to archive.", c.getId(), c.getUuid().getUuidString()); archiver.addEntry(new ArchivableCommunication(c)); } } catch (IOException e) { LOGGER.error("Caught IOException during output.", e); } }