public ListTopicsPagedResponse listTopics() throws Exception {
    // [START pubsub_list_topics]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
      ListTopicsRequest listTopicsRequest =
          ListTopicsRequest.newBuilder().setProject(ProjectName.format(projectId)).build();
      ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest);
      Iterable<Topic> topics = response.iterateAll();
      for (Topic topic : topics) {
        // do something with the topic
      }
      return response;
    }
    // [END pubsub_list_topics]
  }
public ListTopicsPagedResponse listTopics ( ) throws Exception { try ( TopicAdminClient topicAdminClient = TopicAdminClient . create ( ) ) { ListTopicsRequest listTopicsRequest = ListTopicsRequest . newBuilder ( ) . setProject ( ProjectName . format ( projectId ) ) . build ( ) ; ListTopicsPagedResponse response = topicAdminClient . listTopics ( listTopicsRequest ) ; Iterable < Topic > topics = response . iterateAll ( ) ; for ( Topic topic : topics ) { } return response ; } }
Example of listing topics.